-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFine_tuning_VGG16_to_CatDog.py
183 lines (138 loc) · 5.81 KB
/
Fine_tuning_VGG16_to_CatDog.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
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 6 15:17:10 2022
@author: hk01
"""
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.optimizers import Adam
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
warnings.simplefilter(action='ignore', category=FutureWarning)
#%matplotlib inline
# Organize data into train, valid, test dirs
os.chdir('data/dogs-vs-cats')
if os.path.isdir('train/dog') is False:
os.makedirs('train/dog')
os.makedirs('train/cat')
os.makedirs('valid/dog')
os.makedirs('valid/cat')
os.makedirs('test/dog')
os.makedirs('test/cat')
for i in random.sample(glob.glob('cat*'), 500):
shutil.move(i, 'train/cat')
for i in random.sample(glob.glob('dog*'), 500):
shutil.move(i, 'train/dog')
for i in random.sample(glob.glob('cat*'), 100):
shutil.move(i, 'valid/cat')
for i in random.sample(glob.glob('dog*'), 100):
shutil.move(i, 'valid/dog')
for i in random.sample(glob.glob('cat*'), 50):
shutil.move(i, 'test/cat')
for i in random.sample(glob.glob('dog*'), 50):
shutil.move(i, 'test/dog')
os.chdir('../../')
# GPU settings
#physical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(physical_devices))
#tf.config.experimental.set_memory_growth(physical_devices[0], True)
train_path = 'data/dogs-vs-cats/train'
valid_path = 'data/dogs-vs-cats/valid'
test_path = 'data/dogs-vs-cats/test'
# preprocessing
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=valid_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=test_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10, shuffle=False)
#plotting some samples
imgs, labels = next(train_batches)
def plotImages(images_arr):
fig, axes = plt.subplots(1, 10, figsize=(20,20))
axes = axes.flatten()
for img, ax in zip( images_arr, axes):
ax.imshow(img)
ax.axis('off')
plt.tight_layout()
plt.show()
plotImages(imgs)
print(labels)
# trianing the CNN
model = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding = 'same', input_shape=(224,224,3)),
MaxPool2D(pool_size=(2, 2), strides=2),
Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same'),
MaxPool2D(pool_size=(2, 2), strides=2),
Flatten(),
Dense(units=2, activation='softmax')
])
model.summary()
model.compile(optimizer=Adam(learning_rate=0.0001), loss ='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=train_batches, validation_data=valid_batches, epochs =10, steps_per_epoch=100, validation_steps=20, verbose = 2)
# testing on new data
test_imgs,test_labels=next(test_batches)
plotImages(test_imgs)
print(test_labels)
Predictions=model.predict(x=test_batches,verbose=0)
rounded_predcitions= np.round(Predictions)
rounded_predcitions= np.argmax(rounded_predcitions,axis=-1)
# Confusion matrix
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
ConfusionMatrixDisplay.from_predictions(test_batches.classes,rounded_predcitions)
# Fine-tuning vgg16
# download and saving vgg16 model
vgg16_model = tf.keras.applications.vgg16.VGG16()
# save only model weights
# import os.path
# if os.path.isfile('models/vgg16_model.h5') is False:
# vgg16_model.save('models/vgg16_model.h5')
vgg16_model.load('models/vgg16_model.h5')
vgg16_model.summary()
# what type of model this is
type(vgg16_model)
# generate a substitute model to put all the layers except for the last in
model = Sequential()
for layer in vgg16_model.layers[:-1]:
model.add(layer)
# make all model layers untrailable
for layer in model.layers:
layer.trainable = False
# Add a new last model layer with 2 nodes
model.add(Dense(units=2, activation='softmax'))
model.summary()
# Fine-tuning/training the new model based on vgg16
model.compile(optimizer=Adam(learning_rate=0.0001), loss ='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=train_batches,
steps_per_epoch=len(train_batches),
validation_data=valid_batches,
validation_steps=len(valid_batches),
epochs=5,
verbose=2
)
# Testing the new fine-tuned model to check inference
Predictions=model.predict(x=test_batches,verbose=0)
rounded_predcitions= np.round(Predictions)
rounded_predcitions= np.argmax(rounded_predcitions,axis=-1)
# Confusion matrix
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
ConfusionMatrixDisplay.from_predictions(test_batches.classes,rounded_predcitions)