Repository navigation
Expand file tree
/
Copy pathtransferLearningProject.py
More file actions
295 lines (197 loc) · 8.95 KB
/
Copy pathtransferLearningProject.py
File metadata and controls
295 lines (197 loc) · 8.95 KB
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
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
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
218
219
220
221
222
223
224
225
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
# -*- coding: utf-8 -*-
"""transfer-learning-with-xception-for-cifar-10.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1vWReHeh8KoHFHJkuyW66mEaezWyxHy-e
# Apprentissage par transfert avec les réseaux de neurones profonds
"""
!pip install tensorflow
"""# Load Required Modules"""
# Commented out IPython magic to ensure Python compatibility.
import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib inline
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
#Import keras functions
import tensorflow as tf
print(tf.__version__)
from sklearn.model_selection import train_test_split
from keras import Sequential
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.optimizers import SGD,Adam
from keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.applications import VGG19,Xception
from tensorflow.keras.layers import Input,Flatten,Dense,Conv2D,BatchNormalization,Activation,Dropout,GlobalAveragePooling2D,AveragePooling2D,MaxPooling2D,RandomFlip,RandomZoom,RandomRotation
#Import the dataset
from keras.datasets import cifar10
"""# Load CIFAR-10"""
(x_train, y_train), (x_val, y_val)= cifar10.load_data()
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.2, random_state=42)
y_train=to_categorical(y_train)
y_test=to_categorical(y_test)
y_val=to_categorical(y_val)
print((x_train.shape, y_train.shape))
print((x_test.shape, y_test.shape))
print((x_val.shape, y_val.shape))
"""# Xception"""
base_model = Xception(include_top=False, weights='imagenet', input_shape=(224,224,3), classes=y_train.shape[1])
"""Bloque l'entrainement de la couche du model Xception"""
# freeze all layers
base_model.trainable = False
data_augmentation = Sequential(
[RandomFlip("horizontal"),
RandomRotation(0.1),
RandomZoom(0.1)]
)
"""
La résolution d'origine de CIRFAR-10 est 32x32 elle est trop faible pour Xception que necessite au minima 71x71"""
inputs = tf.keras.Input(shape=(32, 32, 3))
x = tf.keras.layers.Lambda(lambda image: tf.image.resize(image, (224,224)))(inputs)
x = data_augmentation(x)
x = tf.keras.applications.xception.preprocess_input(x)
x = base_model(x, training=False)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dropout(0.3)(x)
outputs = tf.keras.layers.Dense(10, activation=('softmax'))(x)
model = tf.keras.Model(inputs, outputs)
#Check the architecture of the final model
model.summary()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
epochs = 10
history = model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=epochs, verbose=1)
model.save('/content/trainedModel.h5')
#model = tf.keras.models.load_model('/content/trainedModel.h5')
loss, accuracy = model.evaluate(x_test,y_test, verbose=2)
print(f"Perte (Loss) sur l'ensemble de test: {loss}")
print(f"Précision (Accuracy) sur l'ensemble de test: {accuracy}")
"""## Cross Validation pour le model par transfert
"""
def plot_history(history,s):
history_frame = pd.DataFrame(history.history)
TempLoss = history_frame.loc[:, ['loss', 'val_loss']].plot()
TempLoss.set_title(s+" - loss & val_loss")
TempAcc = history_frame.loc[:, ['accuracy', 'val_accuracy']].plot()
TempAcc.set_title(s+" - accuracy & val_accuracy pour ")
return
plot_history(history,"transfer learning")
"""# A Simple CNN from scratch
"""
#from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
modelCnn = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dropout(0.3),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
modelCnn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
cnn = modelCnn.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=epochs, verbose=1)
loss, accuracy = modelCnn.evaluate(x_test,y_test, verbose=2)
print(f"Perte (Loss) sur l'ensemble de test: {loss}")
print(f"Précision (Accuracy) sur l'ensemble de test: {accuracy}")
modelCnn.summary()
"""## Cross Validation pour le CNN simple
"""
plot_history(cnn,"CNN")
"""# Confusion Matrix pour le CNN simple
"""
class_names=['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
predictions=modelCnn.predict(x_val)
y_pred_classes = np.argmax(predictions, axis=1)
y_true = np.argmax(y_val, axis=1)
confusion_mtx = y_val.shape[1]*tf.math.confusion_matrix(y_true, y_pred_classes)/y_val.shape[0]
plt.figure(figsize=(12, 9))
c = sns.heatmap(confusion_mtx, annot=True, fmt='g')
c.set(xticklabels=class_names, yticklabels=class_names)
plt.title("CNN - Matrice de confusion")
"""# Confusion Matrix pour le model de transfert"""
predictions=model.predict(x_val)
y_pred_classes = np.argmax(predictions, axis=1)
y_true = np.argmax(y_val, axis=1)
confusion_mtx = y_val.shape[1]*tf.math.confusion_matrix(y_true, y_pred_classes)/y_val.shape[0]
plt.figure(figsize=(12, 9))
c = sns.heatmap(confusion_mtx, annot=True, fmt='g')
c.set(xticklabels=class_names, yticklabels=class_names)
plt.title("Transfer Learning - Matrice de confusion")
"""# Autre Architecture de CNN_2"""
#from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
modelCnn_2 = Sequential([
Conv2D(32, (3, 3), activation='sigmoid', input_shape=(32, 32, 3)),
MaxPooling2D(2, 2),
Flatten(),
Dropout(0.3),
Dense(256, activation='relu'),
Dense(10, activation='softmax')
])
modelCnn_2.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
cnn_2 = modelCnn_2.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=epochs, verbose=1)
loss, accuracy = modelCnn_2.evaluate(x_test,y_test, verbose=2)
print(f"Perte (Loss) sur l'ensemble de test: {loss}")
print(f"Précision (Accuracy) sur l'ensemble de test: {accuracy}")
plot_history(cnn_2,"CNN_2")
predictions=modelCnn_2.predict(x_val)
y_pred_classes = np.argmax(predictions, axis=1)
y_true = np.argmax(y_val, axis=1)
confusion_mtx = y_val.shape[1]*tf.math.confusion_matrix(y_true, y_pred_classes)/y_val.shape[0]
plt.figure(figsize=(12, 9))
c = sns.heatmap(confusion_mtx, annot=True, fmt='g')
c.set(xticklabels=class_names, yticklabels=class_names)
plt.title("CNN_2 - Matrice de confusion")
"""# Autre Architecture de CNN_3"""
#from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
modelCnn_3 = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dropout(0.3),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
modelCnn_3.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
cnn_3 = modelCnn_3.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=epochs, verbose=1)
loss, accuracy = modelCnn_3.evaluate(x_test,y_test, verbose=2)
print(f"Perte (Loss) sur l'ensemble de test: {loss}")
print(f"Précision (Accuracy) sur l'ensemble de test: {accuracy}")
plot_history(cnn_3,"CNN_3")
predictions=modelCnn_3.predict(x_val)
y_pred_classes = np.argmax(predictions, axis=1)
y_true = np.argmax(y_val, axis=1)
confusion_mtx = y_val.shape[1]*tf.math.confusion_matrix(y_true, y_pred_classes)/y_val.shape[0]
plt.figure(figsize=(12, 9))
c = sns.heatmap(confusion_mtx, annot=True, fmt='g')
c.set(xticklabels=class_names, yticklabels=class_names)
plt.title("CNN_3 - Matrice de confusion")
"""# Autre architecture CNN_4"""
#from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
modelCnn_4 = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
MaxPooling2D(2, 2),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(2, 2),
Flatten(),
Dropout(0.3),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
modelCnn_4.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
cnn_4 = modelCnn_4.fit(x_train, y_train, validation_data=(x_val, y_val), batch_size=64, epochs=epochs, verbose=1)
loss, accuracy = modelCnn_4.evaluate(x_test,y_test, verbose=2)
print(f"Perte (Loss) sur l'ensemble de test: {loss}")
print(f"Précision (Accuracy) sur l'ensemble de test: {accuracy}")
plot_history(cnn_4,"CNN_4")
predictions=modelCnn_4.predict(x_val)
y_pred_classes = np.argmax(predictions, axis=1)
y_true = np.argmax(y_val, axis=1)
confusion_mtx = y_val.shape[1]*tf.math.confusion_matrix(y_true, y_pred_classes)/y_val.shape[0]
plt.figure(figsize=(12, 9))
c = sns.heatmap(confusion_mtx, annot=True, fmt='g')
c.set(xticklabels=class_names, yticklabels=class_names)
plt.title("CNN_4 - Matrice de confusion")