-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_learning_vgg.py
549 lines (421 loc) · 18.5 KB
/
transfer_learning_vgg.py
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
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
322
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
402
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#import libraries
from numpy.random import seed
seed(101)
import pandas as pd
import numpy as np
import tensorflow as tf
import keras
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint
import os
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import itertools
import shutil
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.metrics import plot_confusion_matrix
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation,Dense,Flatten,BatchNormalization,Conv2D,MaxPool2D
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import itertools
import os
import shutil
import random
import glob
import matplotlib.pyplot as plt
import warnings
from tensorflow.keras.optimizers import Adam
import tensorflow as tf
warnings.simplefilter(action='ignore',category=FutureWarning)
#organize data into train,valid,test
import os
os.chdir('C:\\Users\\itsios\\Desktop\\dissertation\\data\\trainn')
if os.path.isdir('train\\Covid') is False:
os.makedirs('train\\Covid')
os.makedirs('train\\Non-Covid')
os.makedirs('valid\\Covid')
os.makedirs('valid\\Non-Covid')
os.makedirs('test\\Covid')
os.makedirs('test\\Non-Covid')
for c in random.sample(glob.glob('Covid*'),500):
shutil.move(c,'train\\Covid')
for c in random.sample(glob.glob('Non-Covid*'),500):
shutil.move(c,'train\\Non-Covid')
for c in random.sample(glob.glob('Covid*'),100):
shutil.move(c,'valid\\Covid')
for c in random.sample(glob.glob('Non-Covid*'),100):
shutil.move(c,'valid\\Non-Covid')
for c in random.sample(glob.glob('Covid*'),50):
shutil.move(c,'test\\Covid')
for c in random.sample(glob.glob('Non-Covid*'),50):
shutil.move(c,'test\\Non-Covid')
train_path='C:\\Users\\itsios\\Desktop\\dissertation\\data\\trainn\\train'
valid_path='C:\\Users\itsios\\Desktop\\dissertation\\data\\trainn\\valid'
test_path='C:\\Users\itsios\\Desktop\\dissertation\\data\\trainn\\test'
train_batches=ImageDataGenerator(preprocessing_function=keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_path,target_size=(224,224),classes=['Covid','Non-Covid'],batch_size=10)
valid_batches=ImageDataGenerator(preprocessing_function=keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=valid_path,target_size=(224,224),classes=['Covid','Non-Covid'],batch_size=10)
test_batches=ImageDataGenerator(preprocessing_function=keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_path,target_size=(224,224),classes=['Covid','Non-Covid'],batch_size=10,shuffle=False)
# import the vgg16 architecture
vgg16_model=keras.applications.vgg16.VGG16()
vgg16_model.summary()
# put out the last layer with the predictions
model= tf.keras.Sequential()
#or
model= keras.Sequential()
#freeze the last layer of vgg16 which has 1000 labels
for layer in vgg16_model.layers[:-1]:
model.add(layer)
#freazing the layers
for layer in model.layers:
layer.trainable=False
#covid or non-covid
model.add(Dense(units=2,activation='softmax'))
#run the next command if you want to load the greyscale weights
checkpoint_path = "C:\\Users\\itsios\\Desktop\\dissertation\\checkpoints\\model.ckpt-1495066"
model.load_weights(checkpoint_path)
#now our model has only 2 classes instead of 1000 of
#vgg16 and the trainable parameters reduced and
#are only for the last fcl
model.summary()
#train the fine -tuned vgg16 model
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
#fit our model
history=model.fit(x=train_batches,validation_data=valid_batches,epochs=3,verbose=2)
#predict
predictions=model.predict(x=test_batches,verbose=0)
model.summary()
#check the classes
test_batches.classes
test_batches.class_indices
#confusion matrix
cm=confusion_matrix(y_true=test_batches.classes,y_pred=np.argmax(predictions,axis=-1))
print(cm)
#fine-tuning#
# Unfreeze the base model
model.trainable = True
# It's important to recompile your model after you make any changes
# to the `trainable` attribute of any inner layer, so that your changes
# are take into account
model.compile(optimizer=keras.optimizers.Adam(1e-5),loss='categorical_crossentropy',metrics=['accuracy'])
# Train end-to-end. Be careful to stop before you overfit!
history1=model.fit(x=train_batches,validation_data=valid_batches,epochs=30,verbose=2)
#to improve the results we can do DATA AUGMENTATION
#MORE AGGRESIVE DROPOUT
#L1 AND L2 REGULARIZATION
#FINE TUNING ONE MORE CONVOLUTIONAL BLOCK
#get the layers output (tensors)
flatten=model.layers[18].output
fc_layer=model.layers[20].output
#extract the weights of the fc2 layer and save it
maybe=model.layers[21].get_weights()
maybe2=maybe[0]
#
import tensorflow as tf
vgg16 = tf.keras.applications.VGG16()
#flatten layer
flatten_output = tf.keras.backend.function(model.input, model.get_layer('flatten').output)
#fully-connected2 layer
fc2_output = tf.keras.backend.function(model.input, model.get_layer('fc2').output)
image=train_batches[1]
result = flatten_output(image)
print('Flatten layer outputs:', result)
print('Shape:', result.shape)
######################################################## I did not use this method ########################################
#import image
rot=tf.keras.preprocessing.image.load_img('/content/drive/My Drive/trainn/train/Covid/Covid (1001).png',target_size=(224,224))
#import pil
from PIL import Image
#transform image to numpy array
img = tf.keras.preprocessing.image.array_to_img(rot)
array = tf.keras.preprocessing.image.img_to_array(img)
data_np = np.asarray(array, np.float32)
#tranfrorm numpy to tensor
data_tf = tf.convert_to_tensor(data_np, np.float32)
#prepare image to fit the output
new_image = tf.expand_dims(data_tf,0)
#fully connected layer output
result_fc2=fc2_output(new_image)
print('FC2 layer outputs:', result_fc2)
print('Shape:', result_fc2.shape)
################################################ FINISH I did not use this method ######################################################
################################################# initial trials ,better go to line 215 to see the 2 methods ################
import tensorflow as tf
vgg16 = tf.keras.applications.VGG16()
flatten_output = tf.keras.backend.function(model.input, model.get_layer('flatten').output)
fc2_output = tf.keras.backend.function(model.input, model.get_layer('fc2').output)
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Covid/*.png")]
y=[]
for i in range(1,11):
gray=cv2.resize(images[i],(224,224))
y.append(gray)
list=tf.convert_to_tensor(y)
list2=fc2_output(list)
import pandas as pd
test1=pd.DataFrame(list2)
############################## FINISH ################### initial trials ,better go to line 215 to see the 2 methods ################
###########################################################15/7/2020 ( unfreeze the 5 final layer and train them with our images)
############################ FIRST APPROACH #######################################
# let's freeze the feature extractor layers and train with the train_batches the flatten,fc and prediction layer
vgg16_model=keras.applications.vgg16.VGG16()
model= Sequential()
for layer in vgg16_model.layers[:-5]:
model.add(layer)
for layer in model.layers:
layer.trainable=False
# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.add(MaxPool2D())
model.add(Flatten())
model.add(Dense(units=4096,activation='relu'))
model.add(Dense(units=4096,activation='relu'))
model.add(Dense(units=2,activation='softmax'))
#train the fine -tuned vgg16 model
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
#fit our model
model.fit(x=train_batches,validation_data=valid_batches,epochs=20,verbose=2)
##### extract the fc2 layer ("dense6)
fc2_output = tf.keras.backend.function(model.input, model.get_layer('dense_6').output)
# put the images from train set to extract the features ( pleon ta flatten, fc2, and predict) exoun ekpaideutei me tis fwto moy
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Covid/*.png")]
y=[]
for i in range(1,200):
gray=cv2.resize(images[i],(224,224))
y.append(gray)
list_cov=tf.convert_to_tensor(y)
list11=fc2_output(list_cov)
pd_list_cov=pd.DataFrame(list11)
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Non-Covid/*.png")]
y1=[]
for i in range(1,200):
gray1=cv2.resize(images[i],(224,224))
y1.append(gray1)
list_ncov=tf.convert_to_tensor(y1)
list1=fc2_output(list_ncov)
pd_list_ncov=pd.DataFrame(list1)
##two ways either
freezer=pd.concat([pd_list_cov, pd_list_cov])
#or
greez = pd_list_cov.append(pd_list_cov)
#how to save on colab
from google.colab import files
greez.to_csv('greez.csv')
files.download('greez.csv')
############################################ FINISH 1ST APPROACH 3#########################################
####################################### SECOND APPROACH to extract features ###########################
########################################## train only the last layer with our dataset ################
#from tensorflow import keras
#from tensorflow.keras.models import Sequential
#from tensorflow.keras.layers import Activation,Dense,Flatten,BatchNormalization,Conv2D,MaxPool2D
#from tensorflow.keras.metrics import categorical_crossentropy
#from tensorflow.keras.preprocessing.image import ImageDataGenerator
vgg16_model=keras.applications.vgg16.VGG16()
model= keras.Sequential()
for layer in vgg16_model.layers[:-1]:
model.add(layer)
for layer in model.layers:
layer.trainable=False
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.add(Dense(units=2,activation='softmax'))
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
history=model.fit(x=train_batches,validation_data=valid_batches,epochs=20,verbose=2)
fc2_output = tf.keras.backend.function(model.input, model.get_layer('fc2').output)
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Covid/*.png")]
y=[]
for i in range(1,100):
gray=cv2.resize(images[i],(224,224))
y.append(gray)
list_cov_k=tf.convert_to_tensor(y)
list11_k=fc2_output(list_cov_k)
pd_list_cov_k=pd.DataFrame(list11_k)
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Covid/*.png")]
y=[]
for i in range(100,199):
gray=cv2.resize(images[i],(224,224))
y.append(gray)
list_cov_l=tf.convert_to_tensor(y)
list11_l=fc2_output(list_cov_l)
pd_list_cov_l=pd.DataFrame(list11_l)
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Covid/*.png")]
y=[]
for i in range(199,300):
gray=cv2.resize(images[i],(224,224))
y.append(gray)
list_cov_m=tf.convert_to_tensor(y)
list11_m=fc2_output(list_cov_m)
pd_list_cov_m=pd.DataFrame(list11_m)
pd_list_cov=pd.concat([pd_list_cov_k, pd_list_cov_l,pd_list_cov_m])
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Non-Covid/*.png")]
y1=[]
for i in range(1,100):
gray1=cv2.resize(images[i],(224,224))
y1.append(gray1)
list_ncov_k=tf.convert_to_tensor(y1)
list1_k=fc2_output(list_ncov_k)
pd_list_ncov_k=pd.DataFrame(list1_k)
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Non-Covid/*.png")]
y1=[]
for i in range(100,199):
gray1=cv2.resize(images[i],(224,224))
y1.append(gray1)
list_ncov_l=tf.convert_to_tensor(y1)
list1_l=fc2_output(list_ncov_l)
pd_list_ncov_l=pd.DataFrame(list1_l)
import cv2
images = [cv2.imread(file) for file in glob.glob("/content/drive/My Drive/trainn/train/Non-Covid/*.png")]
y1=[]
for i in range(199,300):
gray1=cv2.resize(images[i],(224,224))
y1.append(gray1)
list_ncov_m=tf.convert_to_tensor(y1)
list1_m=fc2_output(list_ncov_m)
pd_list_ncov_m=pd.DataFrame(list1_m)
pd_list_ncov=pd.concat([pd_list_ncov_k, pd_list_ncov_l,pd_list_ncov_m])
transfer_all_layers_fc2=pd.concat([pd_list_cov, pd_list_cov])
from google.colab import files
transfer_all_layers_fc2.to_csv('transfer_all_layers_fc2.csv')
files.download('transfer_all_layers_fc2.csv')
#plot the performance metrics
history=model.fit(x=train_batches,validation_data=valid_batches,epochs=15,verbose=2)
test_loss, test_acc = model.evaluate(test_batches)
# 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', 'val_accuracy'], 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', 'validation'], loc='upper left')
plt.show()
##################################### new exp ############################## it is saved as 'dokimi' at colab #########
# implement dropout to the pretrained with imagenet model which has learnt with covid dataset the five last layers
vgg16_model=keras.applications.vgg16.VGG16()
model= Sequential()
for layer in vgg16_model.layers[:-5]:
model.add(layer)
for layer in model.layers:
layer.trainable=False
# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])
dropout1 = tf.keras.layers.Dropout(0.5)
dropout2 = tf.keras.layers.Dropout(0.5)
model.add(MaxPool2D())
model.add(Flatten())
model.add(Dense(units=4096,activation='relu'))
model.add(dropout1)
model.add(Dense(units=4096,activation='relu'))
model.add(dropout2)
model.add(Dense(units=2,activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])
history=model.fit(x=train_batches,validation_data=valid_batches,epochs=15,verbose=2)
test_loss, test_acc = model.evaluate(test_batches)
# 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', 'val_accuracy'], 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', 'validation'], loc='upper left')
plt.show()
################################# finish new exp ############################################
#################################### calculate metrics ###########
#extract predict classes y of test set ######
yhat_classes = model.predict_classes(test_batches, verbose=0)
#extract actual classes y of test set ######
y_actual=test_batches.classes
# accuracy: (tp + tn) / (p + n)
from sklearn.metrics import precision_score, recall_score, accuracy_score, classification_report,f1_score
from sklearn.metrics import roc_auc_score , classification_report
accuracy = accuracy_score(y_actual, yhat_classes)
print('Accuracy: %f' % accuracy)
# precision tp / (tp + fp)
precision = precision_score(y_actual, yhat_classes)
print('Precision: %f' % precision)
# recall: tp / (tp + fn)
recall = recall_score(y_actual, yhat_classes)
print('Recall: %f' % recall)
# f1: 2 tp / (2 tp + fp + fn)
f1 = f1_score(y_actual, yhat_classes)
print('F1 score: %f' % f1)
#plot the roc curve
from sklearn.metrics import precision_score, recall_score, accuracy_score, classification_report,f1_score
from sklearn.metrics import roc_auc_score , classification_report
from sklearn.metrics import roc_curve,roc_auc_score
auc=roc_auc_score(y_actual, yhat_classes)
print('AUC: %.2f' % auc)
def plot_roc_curve(fpr, tpr):
plt.plot(fpr, tpr, color='orange', label='ROC')
plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend()
plt.savefig("roc curve mushroom.png")
plt.show()
fpr,tpr,thresholds=roc_curve(y_actual,yhat_classes)
plot_roc_curve(fpr,tpr)
## PLOT THE CONFUSION MATRIX ##
from sklearn.metrics import confusion_matrix
import seaborn as sns
cm = confusion_matrix(y_actual,yhat_classes)
f, ax = plt.subplots(figsize =(5,5))
sns.heatmap(cm,annot = True,linewidths=0.5,linecolor="red",fmt = ".0f",ax=ax)
plt.xlabel("y_pred_lr")
plt.ylabel("y_true_lr")
plt.savefig("confusion matrix mushrooms")
plt.show()
# new dataset compare metrics#
test_path_2='/content/drive/My Drive/test_2'
test_batches_2=ImageDataGenerator(preprocessing_function=keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_path_2,target_size=(224,224),classes=['Covid','Non-Covid'],batch_size=10,shuffle=False)
#run it later
vgg16_model=keras.applications.vgg16.VGG16()
model= Sequential()
for layer in vgg16_model.layers[:-5]:
model.add(layer)
for layer in model.layers:
layer.trainable=False
model.compile(optimizer='adam', loss='categorical_crossentropy')
model.add(MaxPool2D())
model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPool2D())
model.add(Flatten())
model.add(Dense(units=4096,activation="relu"))
model.add(Dense(units=4096,activation="relu"))
model.add(Dense(units=2, activation="softmax"))
model.summary()