Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img width=\"800px\" src=\"../fidle/img/00-Fidle-header-01.svg\"></img>\n",
"\n",
"# <!-- TITLE --> [VAE6] - Variational AutoEncoder (VAE) with CelebA (small)\n",
"<!-- DESC --> Episode 6 : Variational AutoEncoder (VAE) with CelebA (small res.)\n",
"<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->\n",
"\n",
"## Objectives :\n",
" - Build and train a VAE model with a large dataset in **small resolution(>70 GB)**\n",
" - Understanding a more advanced programming model with **data generator**\n",
"The [CelebFaces Attributes Dataset (CelebA)](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html) contains about 200,000 images (202599,218,178,3). \n",
"\n",
"## What we're going to do :\n",
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"\n",
" - Defining a VAE model\n",
" - Build the model\n",
" - Train it\n",
" - Follow the learning process with Tensorboard\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1 - Setup environment\n",
"### 1.1 - Python stuff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"import os,sys\n",
"from importlib import reload\n",
"\n",
"import modules.vae\n",
"import modules.data_generator\n",
"\n",
"reload(modules.data_generator)\n",
"reload(modules.vae)\n",
"\n",
"from modules.vae import VariationalAutoencoder\n",
"from modules.data_generator import DataGenerator\n",
"\n",
"sys.path.append('..')\n",
"import fidle.pwk as ooo\n",
"reload(ooo)\n",
"\n",
"ooo.init()\n",
"\n",
"VariationalAutoencoder.about()\n",
"DataGenerator.about()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 - The good place"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"place, dataset_dir = ooo.good_place( { 'GRICAD' : f'{os.getenv(\"SCRATCH_DIR\",\"\")}/PROJECTS/pr-fidle/datasets/celeba',\n",
" 'IDRIS' : f'{os.getenv(\"WORK\",\"\")}/datasets/celeba',\n",
" 'HOME' : f'{os.getenv(\"HOME\",\"\")}/datasets/celeba'} )\n",
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"\n",
"# ---- train/test datasets\n",
"\n",
"train_dir = f'{dataset_dir}/clusters.train'\n",
"test_dir = f'{dataset_dir}/clusters.test'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2 - DataGenerator and validation data\n",
"Ok, everything's perfect, now let's instantiate our generator for the entire dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"data_gen = DataGenerator(train_dir, 32, k_size=1)\n",
"x_test = np.load(f'{test_dir}/images-000.npy')\n",
"\n",
"print(f'Data generator : {len(data_gen)} batchs of {data_gen.batch_size} images, or {data_gen.dataset_size} images')\n",
"print(f'x_test : {len(x_test)} images')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3 - Get VAE model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tag = f'CelebA.006-S.{os.getenv(\"SLURM_JOB_ID\",\"unknown\")}'\n",
"\n",
"input_shape = (128, 128, 3)\n",
"z_dim = 200\n",
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"\n",
"encoder= [ {'type':'Conv2D', 'filters':32, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'relu'},\n",
" {'type':'Dropout', 'rate':0.25},\n",
" {'type':'Conv2D', 'filters':64, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'relu'},\n",
" {'type':'Dropout', 'rate':0.25},\n",
" {'type':'Conv2D', 'filters':64, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'relu'},\n",
" {'type':'Dropout', 'rate':0.25},\n",
" {'type':'Conv2D', 'filters':64, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'relu'},\n",
" {'type':'Dropout', 'rate':0.25},\n",
" ]\n",
"\n",
"decoder= [ {'type':'Conv2DTranspose', 'filters':64, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'relu'},\n",
" {'type':'Dropout', 'rate':0.25},\n",
" {'type':'Conv2DTranspose', 'filters':64, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'relu'},\n",
" {'type':'Dropout', 'rate':0.25},\n",
" {'type':'Conv2DTranspose', 'filters':32, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'relu'},\n",
" {'type':'Dropout', 'rate':0.25},\n",
" {'type':'Conv2DTranspose', 'filters':3, 'kernel_size':(3,3), 'strides':2, 'padding':'same', 'activation':'sigmoid'}\n",
" ]\n",
"\n",
"vae = modules.vae.VariationalAutoencoder(input_shape = input_shape, \n",
" encoder_layers = encoder, \n",
" decoder_layers = decoder,\n",
" z_dim = z_dim, \n",
" verbose = verbose,\n",
" run_tag = tag)\n",
"vae.save(model=None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4 - Compile it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"optimizer = tf.keras.optimizers.Adam(1e-4)\n",
"# optimizer = 'adam'\n",
"r_loss_factor = 10000\n",
"\n",
"vae.compile(optimizer, r_loss_factor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5 - Train\n",
"For 10 epochs, adam optimizer : \n",
"- Run time at IDRIS : 1299.77 sec. - 0:21:39\n",
"- Run time at GRICAD : 2092.77 sec. - 0:34:52"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"epochs = 10\n",
"initial_epoch = 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vae.train(data_generator = data_gen,\n",
" x_test = x_test,\n",
" epochs = epochs,\n",
" initial_epoch = initial_epoch\n",
" )"
]
},
{
"source": [
"---\n",
"<img width=\"80px\" src=\"../fidle/img/00-Fidle-logo-01.svg\"></img>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
}
},
"nbformat": 4,
"nbformat_minor": 4
}