Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img width=\"800px\" src=\"../fidle/img/00-Fidle-header-01.svg\"></img>\n",
"\n",
"# <!-- TITLE --> [VAE7] - Variational AutoEncoder (VAE) with CelebA (medium)\n",
"<!-- DESC --> VAE with a more fun and realistic dataset - medium resolution and batchable\n",
"<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->\n",
"\n",
"## Objectives :\n",
" - Build and train a VAE model with a large dataset in small resolution(>140 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",
"\n",
" - Defining a VAE model\n",
" - Build the model\n",
" - Train it\n",
" - Follow the learning process with Tensorboard"
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
]
},
{
"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",
"\n",
"# ---- train/test datasets\n",
"\n",
"train_dir = f'{dataset_dir}/clusters-M.train'\n",
"test_dir = f'{dataset_dir}/clusters-M.test'"
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
]
},
{
"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.007-M.{os.getenv(\"SLURM_JOB_ID\",\"unknown\")}'\n",
"input_shape = (192, 160, 3)\n",
125
126
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
"z_dim = 200\n",
"verbose = 0\n",
"\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",
"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": [
"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",
" )"
]
},
{
"---\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
}