-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_learning_resnet50_custom_data_byXing.py
310 lines (232 loc) · 12.2 KB
/
transfer_learning_resnet50_custom_data_byXing.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
"""
Created on Thur July 4 9:26:00 2019
@author: [email protected]
# https://github.com/anujshah1003/Transfer-Learning-in-keras---custom-data
"""
import numpy as np
import os
import time
from resnet50 import ResNet50
from keras.preprocessing import image
from keras.layers import GlobalAveragePooling2D, Dense, Dropout,Flatten
from imagenet_utils import preprocess_input, decode_predictions
from keras.layers import Input
from keras.models import Model
from keras.utils import np_utils
from sklearn.utils import shuffle
#from sklearn.cross_validation import train_test_split ## it’s just an old way of doing split
from sklearn.model_selection import train_test_split ## similar way doing split
import matplotlib.pyplot as plt
from keras.utils import plot_model
from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
from keras.models import Model, load_model
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping
#get_ipython().run_line_magic('matplotlib', 'inline')
############# Loading and pre-processing an image #######################
img_path = 'images\elephant.jpg'
#Load the image using load_img() function specifying the target size.
img = image.load_img(img_path, target_size=(224, 224))
#Keras loads the image in PIL format (height, width) which shall be converted into NumPy format (height, width, channels) using image_to_array() function.
x = image.img_to_array(img)
print (x.shape)
#Then the input image shall be converted to a 4-dimensional Tensor (batchsize, height, width, channels) using NumPy’s expand_dims function.
x = np.expand_dims(x, axis=0)
print (x.shape)
#Normalizing the image
#Some models use images with values ranging from 0 to 1 or from -1 to +1 or “caffe” style.
#The input_image is further to be normalized by subtracting the mean of the ImageNet data.
#We don’t need to worry about internal details and we can use the preprocess_input() function from each model to normalize the image.
x = preprocess_input(x)
print('Input image shape:', x.shape)
plt.imshow(x[0])
model =ResNet50(include_top ='True', weights='imagenet')
prediction= model.predict(x)
print('Predicted:', decode_predictions(prediction))
################# Loading BSL training data #############################
PATH = os.getcwd()
print("PATH", PATH)
# Define data path
#data_path = PATH + '/data' #separated
data_path = PATH + '/data_v' #vertical stack over
#data_path = PATH + '/data_h' #horizontal stack over
data_dir_list = os.listdir(data_path)
img_data_list=[]
for dataset in data_dir_list:
img_list=os.listdir(data_path+'/'+ dataset)
print ('\n Loaded the images of dataset-'+'{}\n'.format(dataset))
for img in img_list:
img_path = data_path + '/'+ dataset + '/'+ img
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print('Input image shape:', x.shape)
img_data_list.append(x)
img_data = np.array(img_data_list)
#img_data = img_data.astype('float32')
print (img_data.shape)
img_data=np.rollaxis(img_data,1,0)
print (img_data.shape)
img_data=img_data[0]
print (img_data.shape)
# Plot a image out
#plt.imshow(np.uint8(img_data[0]))
plt.imshow(img_data[0])
#################### Define the number of classes ##########################
num_classes = 2
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')
labels[0:79]=0 # dementia class 0
labels[79:]=1 # healthy class 1
names = ['dementia','healthy']
# convert class labels to on-hot encoding
Y = np_utils.to_categorical(labels, num_classes)
#print("Y", Y)
# Shuffle the dataset
xs,ys = shuffle(img_data,Y, random_state=2) #if I use the same random_state with the same dataset, then I am always guaranteed to have the same shuffle
# Split the dataset
X_train, X_test, y_train, y_test = train_test_split(xs, ys, test_size=0.2, random_state=2)
###############################################################################
# * Custom_resnet_model_1 #
# Train the Model as a Classifier #
# Only the classifier layer (last layer) is trainable, #
# parameters of other layers are freezed. #
# Used with Smaller Datasets #
# Early Stopping is used for avoid Overfitting # #
###############################################################################
image_input = Input(shape=(224, 224, 3))
# Creat ResNet50 Model
# "include_top= True" means include the final dense layers
model_resnet = ResNet50(input_tensor=image_input, include_top=True,weights='imagenet')
# Print Model Layers Details/ Plot a Graph Layout
model_resnet.summary()
plot_model(model_resnet,to_file='C:/Users/User/Documents/Github_Clone/deep-learning-models/ResNet50/ResNet50Model.png')
SVG(model_to_dot(model_resnet).create(prog='dot', format='svg'))
# Get the last layer "avg_pool" out and from there to add/create your own network layers
last_layer = model_resnet.get_layer('avg_pool').output
x= Flatten(name='flatten')(last_layer)
out = Dense(num_classes, activation='softmax', name='softmax2outputs')(x)
custom_resnet_model_1 = Model(inputs=image_input,outputs= out)
# Print custom_resnet_model_1 Layers Details/ Plot a Graph Layout
custom_resnet_model_1.summary()
plot_model(custom_resnet_model_1,to_file='C:/Users/user/Documents/Github_Clone/deep-learning-models/ResNet50/train_accuracy 69.7674% val_accuracy_freeze/XingRestNet50Model_classifier.png')
SVG(model_to_dot(custom_resnet_model_1).create(prog='dot', format='svg'))
# Freeze the parameters of previous layers, except the last layer
for layer in custom_resnet_model_1.layers[:-1]:
layer.trainable = False
#custom_resnet_model_1.layers[-1].trainable
################# Compile the Model ######################################
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
custom_resnet_model_1.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])
################# Train the Model ########################################
monitor = EarlyStopping(monitor='val_loss', min_delta=0,patience=5,verbose=1, mode='auto', baseline =None, restore_best_weights=True)
t=time.time()
hist3 = custom_resnet_model_1.fit(X_train, y_train, batch_size=1, epochs=100, verbose=1, validation_data=(X_test, y_test),callbacks=[monitor])
print('Training time: %s' % (time.time()-t))
train_accuracy3= hist3.history['acc']
print("[INFO] Model_ResNet train_accuracy: {:.4f}%".format(train_accuracy3[-6] * 100))
################# Test the Model #######################################
(loss3, accuracy3) = custom_resnet_model_1.evaluate(X_test, y_test, batch_size=1, verbose=3)
print("[INFO] loss={:.4f}, val_accuracy: {:.4f}%".format(loss3,accuracy3 * 100))
################# Save the Model #######################################
custom_resnet_model_1.save_weights('C:\\Users\\user\\Documents\\Github_Clone\\deep-learning-models\\ResNet50\\train_accuracy 69.7674% val_accuracy\\my_ResNet50_model_weights_classifier.h5')
custom_resnet_model_1.save('C:\\Users\\user\\Documents\\Github_Clone\\deep-learning-models\\ResNet50\\train_accuracy 69.7674% val_accuracy\\my_ResNet50_model_classifier.h5')
###############################################################################
# * Custom_resnet_model_2 #
# Fine Tune the Model #
# Add on personalised dense layers (FC Layers) #
# Only last 6 layers are trainable #
# Used with Larger Datasets #
###############################################################################
# Creat the Model
model = ResNet50(weights='imagenet',include_top=False)
model.summary()
last_layer = model.output
# add a global spatial average pooling layer
x = GlobalAveragePooling2D()(last_layer)
# add fully-connected & dropout layers
x = Dense(512, activation='relu',name='fc-1')(x)
x = Dropout(0.5)(x)
x = Dense(256, activation='relu',name='fc-2')(x)
x = Dropout(0.5)(x)
# a softmax layer for 4 classes
out = Dense(num_classes, activation='softmax',name='softmax2outputs')(x)
# this is the model we will train
custom_resnet_model_2 = Model(inputs=model.input, outputs=out)
custom_resnet_model_2.summary()
for layer in custom_resnet_model_2.layers[:-6]:
layer.trainable = False
custom_resnet_model_2.layers[-1].trainable
# Compile the Model
custom_resnet_model_2.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
# Train the Model
t=time.time()
hist = custom_resnet_model_2.fit(X_train, y_train, batch_size=3, epochs=30, verbose=1, validation_data=(X_test, y_test))
print('Training time: %s' % (t - time.time()))
# Test the Model
(loss, accuracy) = custom_resnet_model_2.evaluate(X_test, y_test, batch_size=3, verbose=1)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))
custom_resnet_model_2.save_weights('C:/Users/liangx/Documents/Github_Clone/deep-learning-models/ResNet50/my_ResNet50_model_weights_finetune.h5')
custom_resnet_model_2.save('C:/Users/liangx/Documents/Github_Clone/deep-learning-models/ResNet50/my_ResNet50_models_finetune.h5')
###############################################################################
# #
# Model as a Feature Extractor #
# #
###############################################################################
model = ResNet50(weights='imagenet',include_top=False)
model.summary()
img_path = 'images\elephant.jpg'
#Load the image using load_img() function specifying the target size.
ima = image.load_img(img_path, target_size=(224, 224))
#Keras loads the image in PIL format (width, height) which shall be converted into NumPy format (height, width, channels) using image_to_array() function.
x = image.img_to_array(ima)
#Then the input image shall be converted to a 4-dimensional Tensor (batchsize, height, width, channels) using NumPy’s expand_dims function.
x = np.expand_dims(x, axis=0)
#Normalizing the image
x = preprocess_input(x)
#Use the model as an image feature extractor
features=model.predict(x)
print('Input image features:', features)
###################### Plot Results #######################################
import matplotlib.pyplot as plt
# visualizing losses and accuracy
hist=hist3
train_loss=hist.history['loss']
val_loss=hist.history['val_loss']
train_acc=hist.history['acc']
val_acc=hist.history['val_acc']
xc=range(8) # epoch number
plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])
plt.figure(2,figsize=(7,5))
plt.plot(xc,train_acc)
plt.plot(xc,val_acc)
plt.xlabel('num of Epochs')
plt.ylabel('accuracy')
plt.title('train_acc vs val_acc')
plt.grid(True)
plt.legend(['train','val'],loc=4)
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])
############## Model Evaluation/Prediction ###################################
#img_path = 'C:/Users/liangx/Documents/Github_Clone/deep-learning-models/data/dementia/1_left2d_big.png'
#img_path = 'C:/Users/liangx/Documents/Github_Clone/deep-learning-models/data/healthy/6_right2d_big.png'
#img_path ='C:/Users/liangx/Documents/Github_Clone/deep-learning-models/data_h/dementia/1_combine_2d_h.png'
img_path ='C:/Users/user/Documents/Github_Clone/deep-learning-models/data_v/dementia/1_combine_2d_v.png'
img_path ='C:/Users/user/Documents/Github_Clone/deep-learning-models/data_v/healthy/81_combine_2d_v.png'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = custom_resnet_model_1.predict(x)
print('Predicted:',preds)