Image classification project with MLP and CNN
I. Chargement et mise en forme des données
In [ ]:
Copied!
# Import libraries and modules
# Import libraries and modules
import numpy as np
import time
np.random.seed(123) # for reproducibility
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import tensorflow as tf
from tensorflow.keras import Sequential, Model, Input
from tensorflow.keras.layers import Conv2DTranspose, Reshape, Dense, Activation, Flatten, Dropout, Convolution2D, MaxPooling2D, Input
#from utilitaire import affiche
##################################################
# I - Load pre-shuffled MNIST data train and test sets
##################################################
from tensorflow.keras.datasets.mnist import load_data
from matplotlib import pyplot
# Import libraries and modules
# Import libraries and modules
import numpy as np
import time
np.random.seed(123) # for reproducibility
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import tensorflow as tf
from tensorflow.keras import Sequential, Model, Input
from tensorflow.keras.layers import Conv2DTranspose, Reshape, Dense, Activation, Flatten, Dropout, Convolution2D, MaxPooling2D, Input
#from utilitaire import affiche
##################################################
# I - Load pre-shuffled MNIST data train and test sets
##################################################
from tensorflow.keras.datasets.mnist import load_data
from matplotlib import pyplot
In [ ]:
Copied!
def affiche(history):
# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
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 affiche(history):
# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
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()
In [ ]:
Copied!
# load dataset
(X_train, y_train), (X_test, y_test) = load_data()
X_train, pipo, y_train, pipo = train_test_split(X_train, y_train, test_size=0.9)
X_test, pipo, y_test, pipo = train_test_split(X_test, y_test, test_size=0.9)
# load dataset
(X_train, y_train), (X_test, y_test) = load_data()
X_train, pipo, y_train, pipo = train_test_split(X_train, y_train, test_size=0.9)
X_test, pipo, y_test, pipo = train_test_split(X_test, y_test, test_size=0.9)
In [ ]:
Copied!
# afficher les images
for i in range(200):
plt.subplot(10,20,i+1)
plt.imshow(X_train[i,:].reshape([28,28]), cmap='gray')
plt.axis('off')
plt.show()
# afficher les images
for i in range(200):
plt.subplot(10,20,i+1)
plt.imshow(X_train[i,:].reshape([28,28]), cmap='gray')
plt.axis('off')
plt.show()
In [ ]:
Copied!
# Preprocess input data
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)
X_test = X_test.reshape(X_test.shape[0], X_train.shape[1], X_train.shape[2], 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
# Preprocess class labels
Y_train = tf.keras.utils.to_categorical(y_train, 10)
Y_test = tf.keras.utils.to_categorical(y_test, 10)
X_test.shape
# Preprocess input data
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1)
X_test = X_test.reshape(X_test.shape[0], X_train.shape[1], X_train.shape[2], 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
# Preprocess class labels
Y_train = tf.keras.utils.to_categorical(y_train, 10)
Y_test = tf.keras.utils.to_categorical(y_test, 10)
X_test.shape
Out[ ]:
(1000, 28, 28, 1)
Questions
- Que réalise-t-il ? Justifier chaque ligne.
- Combien y a-t-il d’images dans la base de test ? Dans la base d’apprentissage ? -Quelle est la taille des images ? Combien y a-t-il de classes ?
- En quoi consiste le pré-traitement des données d’entrées ? Pourquoi le réalise-t-on ?
- A quoi sert la fonction tf.keras.utils.to_categorical ?
- Quelle est la taille de y_train ? de Y_train ? Commenter
In [ ]:
Copied!
print("nombre d'images en train : ", len(X_train))
print("nombre d'images en test : ", len(X_test))
print("la taille des images est : ", len(X_train[0]),'x',len(X_train[0][0]),"pixels")
print("Nombre de de classes : ", Y_test.shape[1])
print("nombre d'images en train : ", len(X_train))
print("nombre d'images en test : ", len(X_test))
print("la taille des images est : ", len(X_train[0]),'x',len(X_train[0][0]),"pixels")
print("Nombre de de classes : ", Y_test.shape[1])
nombre d'images en train : 6000 nombre d'images en test : 1000 la taille des images est : 28 x 28 pixels Nombre de de classes : 10
II. Régression logistique (Classification) avec des couches dense
II.1. définition du réseau
In [ ]:
Copied!
inputs = Input(shape=(28,28,1))
x = inputs
x=Flatten()(x)
i= Dense(256, activation='relu')(x)
outputs = Dense(10, activation='softmax')(i)
model = Model(inputs, outputs)
model.summary()
inputs = Input(shape=(28,28,1))
x = inputs
x=Flatten()(x)
i= Dense(256, activation='relu')(x)
outputs = Dense(10, activation='softmax')(i)
model = Model(inputs, outputs)
model.summary()
Model: "model" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 28, 28, 1)] 0 flatten (Flatten) (None, 784) 0 dense (Dense) (None, 256) 200960 dense_1 (Dense) (None, 10) 2570 ================================================================= Total params: 203,530 Trainable params: 203,530 Non-trainable params: 0 _________________________________________________________________
II.2. Apprentissage
On utilise l’optimiseur SGD (Stochastic gradient descent optimizer) avec ses paramètres par défaut :
In [ ]:
Copied!
lr= 0.8
batch_size=256
epochs=15
sgd1= tf.keras.optimizers.SGD(learning_rate=lr,momentum=0.8)
model.compile(loss='categorical_crossentropy', optimizer=sgd1, metrics=['accuracy'])
tps1 = time.perf_counter()
history =model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs,
verbose=1,validation_data=(X_test, Y_test))
tps2 = time.perf_counter()
affiche(history) #donnee en annexe
print('lr=',lr,'batch_size=',batch_size, 'epochs=',epochs)
print('Temps d apprentissage',tps2 - tps1)
lr= 0.8
batch_size=256
epochs=15
sgd1= tf.keras.optimizers.SGD(learning_rate=lr,momentum=0.8)
model.compile(loss='categorical_crossentropy', optimizer=sgd1, metrics=['accuracy'])
tps1 = time.perf_counter()
history =model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs,
verbose=1,validation_data=(X_test, Y_test))
tps2 = time.perf_counter()
affiche(history) #donnee en annexe
print('lr=',lr,'batch_size=',batch_size, 'epochs=',epochs)
print('Temps d apprentissage',tps2 - tps1)
Epoch 1/15 24/24 [==============================] - 5s 12ms/step - loss: 1.1757 - accuracy: 0.6437 - val_loss: 0.3654 - val_accuracy: 0.8900 Epoch 2/15 24/24 [==============================] - 0s 5ms/step - loss: 0.2998 - accuracy: 0.9087 - val_loss: 0.2472 - val_accuracy: 0.9220 Epoch 3/15 24/24 [==============================] - 0s 5ms/step - loss: 0.1756 - accuracy: 0.9462 - val_loss: 0.2476 - val_accuracy: 0.9170 Epoch 4/15 24/24 [==============================] - 0s 5ms/step - loss: 0.1255 - accuracy: 0.9593 - val_loss: 0.1759 - val_accuracy: 0.9440 Epoch 5/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0728 - accuracy: 0.9792 - val_loss: 0.1815 - val_accuracy: 0.9470 Epoch 6/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0644 - accuracy: 0.9790 - val_loss: 0.1695 - val_accuracy: 0.9470 Epoch 7/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0406 - accuracy: 0.9883 - val_loss: 0.1845 - val_accuracy: 0.9410 Epoch 8/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0237 - accuracy: 0.9955 - val_loss: 0.1795 - val_accuracy: 0.9430 Epoch 9/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0182 - accuracy: 0.9957 - val_loss: 0.1708 - val_accuracy: 0.9490 Epoch 10/15 24/24 [==============================] - 0s 4ms/step - loss: 0.0115 - accuracy: 0.9982 - val_loss: 0.1720 - val_accuracy: 0.9480 Epoch 11/15 24/24 [==============================] - 0s 4ms/step - loss: 0.0065 - accuracy: 0.9997 - val_loss: 0.1749 - val_accuracy: 0.9550 Epoch 12/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0052 - accuracy: 0.9995 - val_loss: 0.1930 - val_accuracy: 0.9490 Epoch 13/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0040 - accuracy: 1.0000 - val_loss: 0.1761 - val_accuracy: 0.9530 Epoch 14/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0031 - accuracy: 1.0000 - val_loss: 0.1789 - val_accuracy: 0.9540 Epoch 15/15 24/24 [==============================] - 0s 5ms/step - loss: 0.0026 - accuracy: 1.0000 - val_loss: 0.1808 - val_accuracy: 0.9520
lr= 0.8 batch_size= 256 epochs= 15 Temps d apprentissage 11.146458800000005
II.3. Evaluation du modèle
In [ ]:
Copied!
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1]*100)
y_pred = model.predict(X_test)
y_pred = y_pred.argmax(axis=-1)
print('Confusion Matrix')
print(confusion_matrix(y_test, y_pred))
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1]*100)
y_pred = model.predict(X_test)
y_pred = y_pred.argmax(axis=-1)
print('Confusion Matrix')
print(confusion_matrix(y_test, y_pred))
Test loss: 0.18075866997241974 Test accuracy: 95.20000219345093 32/32 [==============================] - 0s 2ms/step Confusion Matrix [[101 0 0 0 0 1 0 1 0 0] [ 0 100 0 0 0 0 0 0 1 0] [ 1 1 80 0 0 0 2 4 1 0] [ 0 0 0 100 0 3 0 0 2 1] [ 0 0 0 0 82 0 0 1 0 2] [ 0 0 0 3 0 85 0 0 1 0] [ 1 0 0 0 1 0 92 0 0 0] [ 0 1 0 2 0 0 0 112 0 1] [ 1 0 0 2 1 2 1 1 106 1] [ 0 0 0 1 3 0 0 3 1 94]]
IV. CNN
In [ ]:
Copied!
inputs = Input(shape=(28,28,1))
x = inputs
x = Convolution2D(32, kernel_size = (3, 3), activation='relu' )(x)
x = Convolution2D(64, kernel_size = (3, 3), activation='relu' )(x)
x = MaxPooling2D((3, 3))(x)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
outputs= Dense(10, activation='softmax')(x)
model = Model(inputs, outputs)
model.summary()
inputs = Input(shape=(28,28,1))
x = inputs
x = Convolution2D(32, kernel_size = (3, 3), activation='relu' )(x)
x = Convolution2D(64, kernel_size = (3, 3), activation='relu' )(x)
x = MaxPooling2D((3, 3))(x)
x = Flatten()(x)
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
outputs= Dense(10, activation='softmax')(x)
model = Model(inputs, outputs)
model.summary()
Model: "model_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_2 (InputLayer) [(None, 28, 28, 1)] 0 conv2d (Conv2D) (None, 26, 26, 32) 320 conv2d_1 (Conv2D) (None, 24, 24, 64) 18496 max_pooling2d (MaxPooling2D (None, 8, 8, 64) 0 ) flatten_1 (Flatten) (None, 4096) 0 dense_2 (Dense) (None, 256) 1048832 dropout (Dropout) (None, 256) 0 dense_3 (Dense) (None, 10) 2570 ================================================================= Total params: 1,070,218 Trainable params: 1,070,218 Non-trainable params: 0 _________________________________________________________________
In [ ]:
Copied!
lr= 0.13
batch_size=256
epochs=26
sgd1= tf.keras.optimizers.SGD(learning_rate=lr,momentum=0.9)
model.compile(loss='categorical_crossentropy', optimizer=sgd1, metrics=['accuracy'])
tps1 = time.perf_counter()
history =model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs,
verbose=1,validation_data=(X_test, Y_test))
tps2 = time.perf_counter()
affiche(history) #donnee en annexe
print('lr=',lr,'batch_size=',batch_size, 'epochs=',epochs)
print('Temps d apprentissage',tps2 - tps1)
lr= 0.13
batch_size=256
epochs=26
sgd1= tf.keras.optimizers.SGD(learning_rate=lr,momentum=0.9)
model.compile(loss='categorical_crossentropy', optimizer=sgd1, metrics=['accuracy'])
tps1 = time.perf_counter()
history =model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs,
verbose=1,validation_data=(X_test, Y_test))
tps2 = time.perf_counter()
affiche(history) #donnee en annexe
print('lr=',lr,'batch_size=',batch_size, 'epochs=',epochs)
print('Temps d apprentissage',tps2 - tps1)
Epoch 1/26 24/24 [==============================] - 7s 28ms/step - loss: 1.8238 - accuracy: 0.4302 - val_loss: 0.8396 - val_accuracy: 0.7670 Epoch 2/26 24/24 [==============================] - 0s 15ms/step - loss: 0.5287 - accuracy: 0.8325 - val_loss: 0.2180 - val_accuracy: 0.9430 Epoch 3/26 24/24 [==============================] - 0s 13ms/step - loss: 0.2965 - accuracy: 0.9083 - val_loss: 0.1211 - val_accuracy: 0.9560 Epoch 4/26 24/24 [==============================] - 0s 13ms/step - loss: 0.1817 - accuracy: 0.9482 - val_loss: 0.1032 - val_accuracy: 0.9680 Epoch 5/26 24/24 [==============================] - 0s 13ms/step - loss: 0.1325 - accuracy: 0.9617 - val_loss: 0.1050 - val_accuracy: 0.9680 Epoch 6/26 24/24 [==============================] - 0s 13ms/step - loss: 0.1124 - accuracy: 0.9658 - val_loss: 0.0847 - val_accuracy: 0.9730 Epoch 7/26 24/24 [==============================] - 0s 12ms/step - loss: 0.0969 - accuracy: 0.9678 - val_loss: 0.0669 - val_accuracy: 0.9780 Epoch 8/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0882 - accuracy: 0.9710 - val_loss: 0.0827 - val_accuracy: 0.9710 Epoch 9/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0731 - accuracy: 0.9763 - val_loss: 0.0485 - val_accuracy: 0.9830 Epoch 10/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0546 - accuracy: 0.9845 - val_loss: 0.0545 - val_accuracy: 0.9820 Epoch 11/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0594 - accuracy: 0.9808 - val_loss: 0.0691 - val_accuracy: 0.9770 Epoch 12/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0483 - accuracy: 0.9840 - val_loss: 0.0445 - val_accuracy: 0.9830 Epoch 13/26 24/24 [==============================] - 0s 12ms/step - loss: 0.0365 - accuracy: 0.9873 - val_loss: 0.0643 - val_accuracy: 0.9810 Epoch 14/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0425 - accuracy: 0.9867 - val_loss: 0.0712 - val_accuracy: 0.9760 Epoch 15/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0467 - accuracy: 0.9840 - val_loss: 0.0582 - val_accuracy: 0.9820 Epoch 16/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0376 - accuracy: 0.9863 - val_loss: 0.0585 - val_accuracy: 0.9800 Epoch 17/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0337 - accuracy: 0.9887 - val_loss: 0.0618 - val_accuracy: 0.9800 Epoch 18/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0349 - accuracy: 0.9887 - val_loss: 0.0598 - val_accuracy: 0.9810 Epoch 19/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0194 - accuracy: 0.9937 - val_loss: 0.0618 - val_accuracy: 0.9830 Epoch 20/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0220 - accuracy: 0.9927 - val_loss: 0.0650 - val_accuracy: 0.9840 Epoch 21/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0276 - accuracy: 0.9900 - val_loss: 0.0764 - val_accuracy: 0.9750 Epoch 22/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0282 - accuracy: 0.9898 - val_loss: 0.0679 - val_accuracy: 0.9810 Epoch 23/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0171 - accuracy: 0.9947 - val_loss: 0.0908 - val_accuracy: 0.9800 Epoch 24/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0195 - accuracy: 0.9937 - val_loss: 0.0752 - val_accuracy: 0.9780 Epoch 25/26 24/24 [==============================] - 0s 13ms/step - loss: 0.0217 - accuracy: 0.9923 - val_loss: 0.0769 - val_accuracy: 0.9800 Epoch 26/26 24/24 [==============================] - 0s 16ms/step - loss: 0.0179 - accuracy: 0.9942 - val_loss: 0.0695 - val_accuracy: 0.9790
lr= 0.13 batch_size= 256 epochs= 26 Temps d apprentissage 15.477421541000012
In [ ]:
Copied!