Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img width=\"800px\" src=\"../fidle/img/header.svg\"></img>\n",
"\n",
"# <!-- TITLE --> [K3GTSRB3] - Training monitoring\n",
"<!-- DESC --> Episode 3 : Monitoring, analysis and check points during a training session, using Keras3\n",
"<!-- AUTHOR : Jean-Luc Parouty (CNRS/SIMaP) -->\n",
"\n",
"## Objectives :\n",
" - **Understand** what happens during the **training** process\n",
" - Implement **monitoring**, **backup** and **recovery** solutions\n",
" \n",
"The German Traffic Sign Recognition Benchmark (GTSRB) is a dataset with more than 50,000 photos of road signs from about 40 classes. \n",
"The final aim is to recognise them ! \n",
"Description is available there : http://benchmark.ini.rub.de/?section=gtsrb&subsection=dataset\n",
"\n",
"## What we're going to do :\n",
"\n",
" - Monitoring and understanding our model training \n",
" - Add recovery points\n",
" - Analyze the results \n",
" - Restore and run recovery points \n",
"\n",
"## Step 1 - Import and init\n",
"### 1.1 - Python stuffs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ['KERAS_BACKEND'] = 'torch'\n",
"\n",
"import keras\n",
"\n",
"import numpy as np\n",
"import os, random\n",
"\n",
"import fidle\n",
"\n",
"import modules.my_loader as my_loader\n",
"import modules.my_models as my_models\n",
"import modules.my_tools as my_tools\n",
"from modules.my_TensorboardCallback import TensorboardCallback\n",
"\n",
"\n",
"# Init Fidle environment\n",
"run_id, run_dir, datasets_dir = fidle.init('K3GTSRB3')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 - Parameters\n",
"`scale` is the proportion of the dataset that will be used during the training. (1 mean 100%) \n",
"- A 20% 24x24 L dataset, 10 epochs, 20% dataset, need 1'30 on a CPU laptop. (Accuracy=91.4)\\\n",
"- A 20% 48x48 RGB dataset, 10 epochs, 20% dataset, need 6'30s on a CPU laptop. (Accuracy=91.5)\n",
"\n",
"`model_name` is the model name from modules.my_models : \n",
"- model_01 for 24x24 ou 48x48 images\n",
"- model_02 for 48x48 images\n",
"\n",
"`fit_verbosity` is the verbosity during training : \n",
"- 0 = silent, 1 = progress bar, 2 = one line per epoch"
73
74
75
76
77
78
79
80
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
122
123
124
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
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
210
211
212
213
214
215
216
217
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"enhanced_dir = './data'\n",
"# enhanced_dir = f'{datasets_dir}/GTSRB/enhanced'\n",
"\n",
"model_name = 'model_01'\n",
"dataset_name = 'set-24x24-L'\n",
"batch_size = 64\n",
"epochs = 10\n",
"scale = 1\n",
"fit_verbosity = 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Override parameters (batch mode) - Just forget this cell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fidle.override('enhanced_dir', 'model_name', 'dataset_name', 'batch_size', 'epochs', 'scale', 'fit_verbosity')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2 - Load dataset\n",
"Dataset is one of the saved dataset..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x_train,y_train,x_test,y_test, x_meta,y_meta = my_loader.read_dataset(enhanced_dir, dataset_name, scale)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3 - Have a look to the dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"x_train : \", x_train.shape)\n",
"print(\"y_train : \", y_train.shape)\n",
"print(\"x_test : \", x_test.shape)\n",
"print(\"y_test : \", y_test.shape)\n",
"\n",
"fidle.scrawler.images(x_train, y_train, range(24), columns=8, x_size=1, y_size=1, save_as='02-dataset-small')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4 - Get a model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(n,lx,ly,lz) = x_train.shape\n",
"\n",
"model = my_models.get_model( model_name, lx,ly,lz )\n",
"model.summary()\n",
"\n",
"model.compile(optimizer='adam',\n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5 - Prepare callbacks \n",
"We will add 2 callbacks : \n",
"\n",
"**TensorBoard** \n",
"Training logs, which can be visualised using [Tensorboard tool](https://www.tensorflow.org/tensorboard). \n",
"\n",
"**Model backup** \n",
" It is possible to save the model each xx epoch or at each improvement. \n",
" The model can be saved completely or partially (weight). \n",
" See [Keras documentation](https://keras.io/api/callbacks/)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fidle.utils.mkdir(run_dir + '/models')\n",
"fidle.utils.mkdir(run_dir + '/logs')\n",
"\n",
"# ---- Callback for tensorboard (This one is homemade !)\n",
"#\n",
"tenseorboard_callback = TensorboardCallback(\n",
" log_dir=run_dir + \"/logs/tb_\" + fidle.Chrono.tag_now())\n",
"\n",
"# ---- Callback to save best model\n",
"#\n",
"bestmodel_callback = keras.callbacks.ModelCheckpoint( \n",
" filepath= run_dir + \"/models/best-model.keras\",\n",
" monitor='val_accuracy', \n",
" mode='max', \n",
" save_best_only=True)\n",
"\n",
"# ---- Callback to save model from each epochs\n",
"#\n",
"savemodel_callback = keras.callbacks.ModelCheckpoint(\n",
" filepath= run_dir + \"/models/{epoch:02d}.keras\",\n",
" save_freq=\"epoch\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6 - Train the model\n",
"To access logs with tensorboad :\n",
"- Under **Docker**, from a terminal launched via the jupyterlab launcher, use the following command:<br>\n",
"```tensorboard --logdir <path-to-logs> --host 0.0.0.0```\n",
"- If you're not using Docker, from a terminal :<br>\n",
"```tensorboard --logdir <path-to-logs>``` \n",
"\n",
"**Note:** One tensorboard instance can be used simultaneously."
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Train it :** \n",
"Note: The training curve is visible in real time with Tensorboard (see step 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chrono=fidle.Chrono()\n",
"chrono.start()\n",
"\n",
"# ---- Shuffle train data\n",
"x_train,y_train=fidle.utils.shuffle_np_dataset(x_train,y_train)\n",
"\n",
"# ---- Train\n",
"# Note: To be faster in our example, we can take only 2000 values\n",
"#\n",
"history = model.fit( x_train, y_train,\n",
" batch_size=batch_size,\n",
" epochs=epochs,\n",
" verbose=fit_verbosity,\n",
" validation_data=(x_test, y_test),\n",
" callbacks=[tenseorboard_callback, bestmodel_callback, savemodel_callback] )\n",
"\n",
"model.save(f'{run_dir}/models/last-model.keras')\n",
"\n",
"chrono.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Evaluate it :**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"max_val_accuracy = max(history.history[\"val_accuracy\"])\n",
"print(\"Max validation accuracy is : {:.4f}\".format(max_val_accuracy))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"score = model.evaluate(x_test, y_test, verbose=0)\n",
"\n",
"print('Test loss : {:5.4f}'.format(score[0]))\n",
"print('Test accuracy : {:5.4f}'.format(score[1]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7 - History\n",
"The return of model.fit() returns us the learning history"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fidle.scrawler.history(history, save_as='03-history')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8 - Evaluation and confusion"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_sigmoid = model.predict(x_test, verbose=fit_verbosity)\n",
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"y_pred = np.argmax(y_sigmoid, axis=-1)\n",
"\n",
"fidle.scrawler.confusion_matrix(y_test,y_pred,range(43), figsize=(12, 12),normalize=False, save_as='04-confusion-matrix')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9 - Restore and evaluate\n",
"#### List saved models :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !ls -1rt \"$run_dir\"/models/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Restore a model :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"loaded_model = keras.models.load_model(f'{run_dir}/models/best-model.keras')\n",
"# loaded_model.summary()\n",
"print(\"Loaded.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Evaluate it :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"score = loaded_model.evaluate(x_test, y_test, verbose=0)\n",
"\n",
"print('Test loss : {:5.4f}'.format(score[0]))\n",
"print('Test accuracy : {:5.4f}'.format(score[1]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Make a prediction :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ---- Pick a random image\n",
"#\n",
"i = random.randint(1,len(x_test))\n",
"x,y = x_test[i], y_test[i]\n",
"\n",
"# ---- Do prediction\n",
"#\n",
"prediction = loaded_model.predict( np.array([x]), verbose=fit_verbosity )\n",
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"\n",
"# ---- Show result\n",
"\n",
"my_tools.show_prediction( prediction, x, y, x_meta )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fidle.end()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 10 - To go further ;-)\n",
"What you can do:\n",
"- Try differents models\n",
"- Use a subset of the dataset\n",
"- Try different datasets\n",
"- Try to recognize exotic signs !\n",
"- Test different hyperparameters (epochs, batch size, optimization, etc.\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"<img width=\"80px\" src=\"../fidle/img/logo-paysage.svg\"></img>"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.2 ('fidle-env')",
"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",
"version": "3.9.2"
},
"vscode": {
"interpreter": {
"hash": "b3929042cc22c1274d74e3e946c52b845b57cb6d84f2d591ffe0519b38e4896d"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}