Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • daconcea/fidle
  • bossardl/fidle
  • Julie.Remenant/fidle
  • abijolao/fidle
  • monsimau/fidle
  • karkars/fidle
  • guilgautier/fidle
  • cailletr/fidle
  • talks/fidle
9 results
Show changes
Commits on Source (4)
......@@ -4,3 +4,4 @@ __pycache__
*/__pycache__/*
/run/**
*/data/*
!/GTSRB/data/dataset.tar.gz
source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:markdown id: tags:
German Traffic Sign Recognition Benchmark (GTSRB)
=================================================
---
Introduction au Deep Learning (IDLE) - S. Aria, E. Maldonado, JL. Parouty - CNRS/SARI/DEVLOG - 2020
## Episode 2 : First Convolutions
Our main steps:
- Read dataset
- Build a model
- Train the model
- Model evaluation
## 1/ Import and init
%% Cell type:code id: tags:
``` python
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.callbacks import TensorBoard
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import time
import idle.pwk as ooo
ooo.init()
```
%% Output
IDLE 2020 - Practical Work Module
Version : 0.1.1
Run time : Sunday 5 January 2020, 16:37:26
Matplotlib style : idle/talk.mplstyle
TensorFlow version : 2.0.0
Keras version : 2.2.4-tf
%% Cell type:markdown id: tags:
## 2/ Reload dataset
Dataset is one of the saved dataset: RGB25, RGB35, L25, L35, etc.
First of all, we're going to use the dataset : **L25**
%% Cell type:code id: tags:
``` python
%%time
dataset ='L25'
img_lx = 25
img_ly = 25
img_lz = 1
# ---- Read dataset
x_train = np.load('./data/{}/x_train.npy'.format(dataset))
y_train = np.load('./data/{}/y_train.npy'.format(dataset))
x_test = np.load('./data/{}/x_test.npy'.format(dataset))
y_test = np.load('./data/{}/y_test.npy'.format(dataset))
# ---- Reshape data
x_train = x_train.reshape( x_train.shape[0], img_lx, img_ly, img_lz)
x_test = x_test.reshape( x_test.shape[0], img_lx, img_ly, img_lz)
input_shape = (img_lx, img_ly, img_lz)
```
%% Output
CPU times: user 0 ns, sys: 328 ms, total: 328 ms
Wall time: 502 ms
%% Cell type:markdown id: tags:
## 3/ Have a look to the dataset
Note: Data must be reshape for matplotlib
%% Cell type:code id: tags:
``` python
print("x_train : ", x_train.shape)
print("y_train : ", y_train.shape)
print("x_test : ", x_test.shape)
print("y_test : ", y_test.shape)
if img_lz>1:
ooo.plot_images(x_train.reshape(-1,img_lx,img_ly,img_lz), y_train, range(6), columns=3, x_size=4, y_size=3)
ooo.plot_images(x_train.reshape(-1,img_lx,img_ly,img_lz), y_train, range(36), columns=12, x_size=1, y_size=1)
else:
ooo.plot_images(x_train.reshape(-1,img_lx,img_ly), y_train, range(6), columns=6, x_size=2, y_size=2)
ooo.plot_images(x_train.reshape(-1,img_lx,img_ly), y_train, range(36), columns=12, x_size=1, y_size=1)
```
%% Output
x_train : (39209, 25, 25, 1)
y_train : (39209,)
x_test : (12630, 25, 25, 1)
y_test : (12630,)
%% Cell type:markdown id: tags:
## 4/ Create model
%% Cell type:code id: tags:
``` python
batch_size = 64
num_classes = 43
epochs = 5
```
%% Cell type:code id: tags:
``` python
model = keras.models.Sequential()
model.add( keras.layers.Conv2D(96, (3,3), activation='relu', input_shape=(img_lx, img_ly, img_lz)))
model.add( keras.layers.MaxPooling2D((2, 2)))
model.add( keras.layers.Conv2D(192, (3, 3), activation='relu'))
model.add( keras.layers.MaxPooling2D((2, 2)))
model.add( keras.layers.Flatten())
model.add( keras.layers.Dense(3072, activation='relu'))
model.add( keras.layers.Dense(500, activation='relu'))
model.add( keras.layers.Dense(500, activation='relu'))
model.add( keras.layers.Dense(43, activation='softmax'))
model.summary()
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
```
%% Output
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 23, 23, 96) 960
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 11, 11, 96) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 9, 9, 192) 166080
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 4, 4, 192) 0
_________________________________________________________________
flatten (Flatten) (None, 3072) 0
_________________________________________________________________
dense (Dense) (None, 3072) 9440256
_________________________________________________________________
dense_1 (Dense) (None, 500) 1536500
_________________________________________________________________
dense_2 (Dense) (None, 500) 250500
_________________________________________________________________
dense_3 (Dense) (None, 43) 21543
=================================================================
Total params: 11,415,839
Trainable params: 11,415,839
Non-trainable params: 0
_________________________________________________________________
%% Cell type:markdown id: tags:
## 5/ Run model
%% Cell type:code id: tags:
``` python
%%time
history = model.fit( x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
```
%% Output
Train on 39209 samples, validate on 12630 samples
Epoch 1/5
39209/39209 [==============================] - 56s 1ms/sample - loss: 0.2945 - accuracy: 0.9090 - val_loss: 0.3993 - val_accuracy: 0.9068
Epoch 2/5
39209/39209 [==============================] - 57s 1ms/sample - loss: 0.0701 - accuracy: 0.9795 - val_loss: 0.4959 - val_accuracy: 0.9074
Epoch 3/5
39209/39209 [==============================] - 62s 2ms/sample - loss: 0.0549 - accuracy: 0.9840 - val_loss: 0.3177 - val_accuracy: 0.9311
Epoch 4/5
39209/39209 [==============================] - 75s 2ms/sample - loss: 0.0370 - accuracy: 0.9891 - val_loss: 0.2901 - val_accuracy: 0.9417
Epoch 5/5
39209/39209 [==============================] - 61s 2ms/sample - loss: 0.0367 - accuracy: 0.9888 - val_loss: 0.2629 - val_accuracy: 0.9382
CPU times: user 27min 34s, sys: 5min 39s, total: 33min 14s
Wall time: 5min 10s
%% Cell type:markdown id: tags:
## 6/ Evaluation
%% Cell type:code id: tags:
``` python
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss : {:5.4f}'.format(score[0]))
print('Test accuracy : {:5.4f}'.format(score[1]))
```
%% Output
Test loss : 0.2265
Test accuracy : 0.9461
%% Cell type:markdown id: tags:
**Results :**
25LHE : 93.4%
35LHE : 95 % avec Convo. 192 3x3 ou pas (->10')
25L : 93.6 %
25RGB : 92.6 %
25RGB : 95.15 avec prof. 192
%% Cell type:code id: tags:
``` python
```
source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:markdown id: tags:
German Traffic Sign Recognition Benchmark (GTSRB)
=================================================
---
Introduction au Deep Learning (IDLE)
S. Aria, E. Maldonado, JL. Parouty
CNRS/SARI/DEVLOG - 2020
Objectives of this practical work
---------------------------------
Traffic sign classification with **CNN**, using Tensorflow and **Keras**
Prerequisite
------------
Environment, with the following packages :
- Python 3.6
- numpy
- Matplotlib
- Tensorflow 2.0
- scikit-image
You can create it from the `environment.yml` file :
```
# conda env create -f environment.yml
```
About the dataset
-----------------
Name : [German Traffic Sign Recognition Benchmark (GTSRB)](http://benchmark.ini.rub.de/?section=gtsrb)
Available [here](https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/published-archive.html)
or on **[kaggle](https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign)**
A nice example from : [Alex Staravoitau](https://navoshta.com/traffic-signs-classification/)
In few words :
- Images : Variable dimensions, rgb
- Train set : 39209 images
- Test set : 12630 images
- Classes : 0 to 42
Episodes
--------
**[01 - Preparation of data](01-Preparation-of-data.ipynb)**
- Understanding the dataset
- Preparing and formatting data
- Organize and backup data
**[02 - First convolutions](02-First-convolutions.ipynb)**
- Read dataset
- Build a model
- Train the model
- Model evaluation
%% Cell type:code id: tags:
``` python
```
German Traffic Sign Recognition Benchmark (GTSRB)
=================================================
---
pjluc 2019 - CNRS/DEVLOG - Formation Deep Learning
1/ Objectives
----------
Traffic sign classification with **CNN**, using Tensorflow and **Keras**
2/ About the dataset
-----------------
Name : [German Traffic Sign Recognition Benchmark (GTSRB)](http://benchmark.ini.rub.de/?section=gtsrb)
Available [here](https://sid.erda.dk/public/archives/daaeac0d7ce1152aea9b61d9f1e19370/published-archive.html)
or on **[kaggle](https://www.kaggle.com/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign)**
A nice example from : [Alex Staravoitau](https://navoshta.com/traffic-signs-classification/)
In few words :
- Images : Variable dimensions, rgb
- Train set : 39209 images
- Test set : 12630 images
- Classes : 0 to 42
3/ Episodes
--------
01 - Dataset preparation
- Undestand the data
02 - First convolutions
\ No newline at end of file
File added
VERSION='0.1a'
\ No newline at end of file
# ==================================================================
# ____ _ _ _ __ __ _
# | _ \ _ __ __ _ ___| |_(_) ___ __ _| | \ \ / /__ _ __| | __
# | |_) | '__/ _` |/ __| __| |/ __/ _` | | \ \ /\ / / _ \| '__| |/ /
# | __/| | | (_| | (__| |_| | (_| (_| | | \ V V / (_) | | | <
# |_| |_| \__,_|\___|\__|_|\___\__,_|_| \_/\_/ \___/|_| |_|\_\
# module pwk
# ==================================================================
# A simple module to host some common functions for practical work
# pjluc 2019
import os
import glob
from datetime import datetime
import itertools
import datetime
import math
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib
import matplotlib.pyplot as plt
VERSION='0.1.1'
# -------------------------------------------------------------
# init_all
# -------------------------------------------------------------
#
def init(mplstyle='idle/talk.mplstyle'):
global VERSION
# ---- matplotlib
matplotlib.style.use(mplstyle)
# ---- Hello world
now = datetime.datetime.now()
print('IDLE 2020 - Practical Work Module')
print(' Version :', VERSION)
print(' Run time : {}'.format(now.strftime("%A %-d %B %Y, %H:%M:%S")))
print(' Matplotlib style :', mplstyle)
print(' TensorFlow version :',tf.__version__)
print(' Keras version :',tf.keras.__version__)
# -------------------------------------------------------------
# init_folder
# -------------------------------------------------------------
#
def init_folder(path):
os.makedirs(path, mode=0o750, exist_ok=True)
def get_directory_size(path):
"""
Return the directory size, but only 1 level
args:
path : directory path
return:
size in Mo
"""
size=0
for f in os.listdir(path):
if os.path.isfile(path+'/'+f):
size+=os.path.getsize(path+'/'+f)
return size/(1024*1024)
# -------------------------------------------------------------
# shuffle_dataset
# -------------------------------------------------------------
#
def shuffle_np_dataset(x, y):
assert (len(x) == len(y)), "x and y must have same size"
p = np.random.permutation(len(x))
return x[p], y[p]
def update_progress(what,i,imax):
bar_length = 40
if (i%int(imax/bar_length))!=0 and i<imax:
return
progress = float(i/imax)
block = int(round(bar_length * progress))
endofline = '\r' if progress<1 else '\n'
text = "{:16s} [{}] {:>5.1f}% of {}".format( what, "#"*block+"-"*(bar_length-block), progress*100, imax)
print(text, end=endofline)
# -------------------------------------------------------------
# show_images
# -------------------------------------------------------------
#
def plot_images(x,y, indices, columns=12, x_size=1, y_size=1, colorbar=False, y_pred=None, cm='binary'):
"""
Show some images in a grid, with legends
args:
X: images
y: real classes
indices: indices of images to show
columns: number of columns (12)
x_size,y_size: figure size
colorbar: show colorbar (False)
y_pred: predicted classes (None)
cm: Matplotlib olor map
returns:
nothing
"""
rows = math.ceil(len(indices)/columns)
fig=plt.figure(figsize=(columns*x_size, rows*(y_size+0.35)))
n=1
errors=0
if np.any(y_pred)==None:
y_pred=y
for i in indices:
axs=fig.add_subplot(rows, columns, n)
n+=1
img=axs.imshow(x[i],cmap = cm, interpolation='lanczos')
img=axs.imshow(x[i],cmap = cm)
axs.spines['right'].set_visible(True)
axs.spines['left'].set_visible(True)
axs.spines['top'].set_visible(True)
axs.spines['bottom'].set_visible(True)
axs.set_yticks([])
axs.set_xticks([])
if y[i]!=y_pred[i]:
axs.set_xlabel('{} ({})'.format(y_pred[i],y[i]))
axs.xaxis.label.set_color('red')
errors+=1
else:
axs.set_xlabel(y[i])
if colorbar:
fig.colorbar(img,orientation="vertical", shrink=0.65)
plt.show()
# -------------------------------------------------------------
# show_history
# -------------------------------------------------------------
#
def plot_history(history, figsize=(8,6)):
"""
Show history
args:
history: history
save_as: filename to save or None
"""
# Accuracy
plt.figure(figsize=figsize)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
if save_as!=None:
save_fig(save_as+'-acc', svg=False)
plt.show()
# Loss values
plt.figure(figsize=figsize)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
def plot_confusion_matrix(cm,
target_names,
title='Confusion matrix',
figsize=(8,6),
cmap=None,
normalize=True):
"""
given a sklearn confusion matrix (cm), make a nice plot
Args:
cm: confusion matrix from sklearn.metrics.confusion_matrix
target_names: given classification classes such as [0, 1, 2]
the class names, for example: ['high', 'medium', 'low']
title: the text to display at the top of the matrix
cmap: color map
normalize: False : plot raw numbers, True: plot proportions
save_as: If not None, filename to save
Citiation:
http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
accuracy = np.trace(cm) / float(np.sum(cm))
misclass = 1 - accuracy
if (figsize[0]==figsize[1]):
aspect='equal'
else:
aspect='auto'
if cmap is None:
cmap = plt.get_cmap('Blues')
plt.figure(figsize=figsize)
plt.imshow(cm, interpolation='nearest', cmap=cmap, aspect=aspect)
plt.title(title)
plt.colorbar()
if target_names is not None:
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 1.5 if normalize else cm.max() / 2
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(cm[i, j]),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
# plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.show()
# See : https://matplotlib.org/users/customizing.html
axes.titlesize : 24
axes.labelsize : 20
axes.edgecolor : dimgrey
axes.labelcolor : dimgrey
axes.linewidth : 2
axes.grid : False
axes.prop_cycle : cycler('color', ['steelblue', 'tomato', '2ca02c', 'd62728', '9467bd', '8c564b', 'e377c2', '7f7f7f', 'bcbd22', '17becf'])
lines.linewidth : 3
lines.markersize : 10
xtick.color : black
xtick.labelsize : 18
ytick.color : black
ytick.labelsize : 18
axes.spines.left : True
axes.spines.bottom : True
axes.spines.top : False
axes.spines.right : False
savefig.dpi : 300 # figure dots per inch or 'figure'
savefig.facecolor : white # figure facecolor when saving
savefig.edgecolor : white # figure edgecolor when saving
savefig.format : svg
savefig.bbox : tight
savefig.pad_inches : 0.1
savefig.transparent : True
savefig.jpeg_quality: 95
name: deeplearning2
channels:
- defaults
dependencies:
- _libgcc_mutex=0.1=main
- _tflow_select=2.3.0=mkl
- absl-py=0.8.1=py36_0
- astor=0.8.0=py36_0
- attrs=19.3.0=py_0
- backcall=0.1.0=py36_0
- blas=1.0=mkl
- bleach=3.1.0=py36_0
- c-ares=1.15.0=h7b6447c_1001
- ca-certificates=2019.11.27=0
- certifi=2019.11.28=py36_0
- cloudpickle=1.2.2=py_0
- cycler=0.10.0=py36_0
- cytoolz=0.10.1=py36h7b6447c_0
- dask-core=2.9.0=py_0
- dbus=1.13.12=h746ee38_0
- decorator=4.4.1=py_0
- defusedxml=0.6.0=py_0
- entrypoints=0.3=py36_0
- expat=2.2.6=he6710b0_0
- fontconfig=2.13.0=h9420a91_0
- freetype=2.9.1=h8a8886c_1
- gast=0.2.2=py36_0
- glib=2.63.1=h5a9c865_0
- gmp=6.1.2=h6c8ec71_1
- google-pasta=0.1.8=py_0
- grpcio=1.16.1=py36hf8bcb03_1
- gst-plugins-base=1.14.0=hbbd80ab_1
- gstreamer=1.14.0=hb453b48_1
- h5py=2.9.0=py36h7918eee_0
- hdf5=1.10.4=hb1b8bf9_0
- icu=58.2=h9c2bf20_1
- imageio=2.6.1=py36_0
- importlib_metadata=1.3.0=py36_0
- intel-openmp=2019.4=243
- ipykernel=5.1.3=py36h39e3cac_0
- ipython=7.10.2=py36h39e3cac_0
- ipython_genutils=0.2.0=py36_0
- jedi=0.15.1=py36_0
- jinja2=2.10.3=py_0
- jpeg=9b=h024ee3a_2
- json5=0.8.5=py_0
- jsonschema=3.2.0=py36_0
- jupyter_client=5.3.4=py36_0
- jupyter_core=4.6.1=py36_0
- jupyterlab=1.2.4=pyhf63ae98_0
- jupyterlab_server=1.0.6=py_0
- keras-applications=1.0.8=py_0
- keras-preprocessing=1.1.0=py_1
- kiwisolver=1.1.0=py36he6710b0_0
- libedit=3.1.20181209=hc058e9b_0
- libffi=3.2.1=hd88cf55_4
- libgcc-ng=9.1.0=hdf63c60_0
- libgfortran-ng=7.3.0=hdf63c60_0
- libpng=1.6.37=hbc83047_0
- libprotobuf=3.11.2=hd408876_0
- libsodium=1.0.16=h1bed415_0
- libstdcxx-ng=9.1.0=hdf63c60_0
- libtiff=4.1.0=h2733197_0
- libuuid=1.0.3=h1bed415_2
- libxcb=1.13=h1bed415_1
- libxml2=2.9.9=hea5a465_1
- markdown=3.1.1=py36_0
- markupsafe=1.1.1=py36h7b6447c_0
- matplotlib=3.1.1=py36h5429711_0
- mistune=0.8.4=py36h7b6447c_0
- mkl=2019.4=243
- mkl-service=2.3.0=py36he904b0f_0
- mkl_fft=1.0.15=py36ha843d7b_0
- mkl_random=1.1.0=py36hd6b4f25_0
- more-itertools=8.0.2=py_0
- nbconvert=5.6.1=py36_0
- nbformat=4.4.0=py36_0
- ncurses=6.1=he6710b0_1
- networkx=2.4=py_0
- notebook=6.0.2=py36_0
- numpy=1.17.4=py36hc1035e2_0
- numpy-base=1.17.4=py36hde5b4d6_0
- olefile=0.46=py36_0
- openssl=1.1.1d=h7b6447c_3
- opt_einsum=3.1.0=py_0
- pandoc=2.2.3.2=0
- pandocfilters=1.4.2=py36_1
- parso=0.5.2=py_0
- pcre=8.43=he6710b0_0
- pexpect=4.7.0=py36_0
- pickleshare=0.7.5=py36_0
- pillow=6.2.1=py36h34e0f95_0
- pip=19.3.1=py36_0
- prometheus_client=0.7.1=py_0
- prompt_toolkit=3.0.2=py_0
- protobuf=3.11.2=py36he6710b0_0
- ptyprocess=0.6.0=py36_0
- pygments=2.5.2=py_0
- pyparsing=2.4.5=py_0
- pyqt=5.9.2=py36h05f1152_2
- pyrsistent=0.15.6=py36h7b6447c_0
- python=3.6.9=h265db76_0
- python-dateutil=2.8.1=py_0
- pytz=2019.3=py_0
- pywavelets=1.1.1=py36h7b6447c_0
- pyzmq=18.1.0=py36he6710b0_0
- qt=5.9.7=h5867ecd_1
- readline=7.0=h7b6447c_5
- scikit-image=0.15.0=py36he6710b0_0
- scipy=1.3.2=py36h7c811a0_0
- send2trash=1.5.0=py36_0
- setuptools=42.0.2=py36_0
- sip=4.19.8=py36hf484d3e_0
- six=1.13.0=py36_0
- sqlite=3.30.1=h7b6447c_0
- tensorboard=2.0.0=pyhb38c66f_1
- tensorflow=2.0.0=mkl_py36hef7ec59_0
- tensorflow-base=2.0.0=mkl_py36h9204916_0
- tensorflow-estimator=2.0.0=pyh2649769_0
- termcolor=1.1.0=py36_1
- terminado=0.8.3=py36_0
- testpath=0.4.4=py_0
- tk=8.6.8=hbc83047_0
- toolz=0.10.0=py_0
- tornado=6.0.3=py36h7b6447c_0
- traitlets=4.3.3=py36_0
- wcwidth=0.1.7=py36_0
- webencodings=0.5.1=py36_1
- werkzeug=0.16.0=py_0
- wheel=0.33.6=py36_0
- wrapt=1.11.2=py36h7b6447c_0
- xz=5.2.4=h14c3975_4
- zeromq=4.3.1=he6710b0_3
- zipp=0.6.0=py_0
- zlib=1.2.11=h7b6447c_3
- zstd=1.3.7=h0b5b093_0
prefix: /home/pjluc/anaconda3/envs/deeplearning2