-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferLearning.py
More file actions
364 lines (268 loc) · 11.2 KB
/
TransferLearning.py
File metadata and controls
364 lines (268 loc) · 11.2 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
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
# setup
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import os
import PIL
import PIL.Image
import pathlib
from math import ceil, floor
def Task1(folder_name):
'''
Download dataset from a folder directory.
Params:
- folder_name : relative folder directory
Returns:
- full_path directory , total number of images
'''
data_dir = pathlib.Path(folder_name).absolute()
image_count = len(list(data_dir.glob('*/*.jpg')))
return data_dir, image_count
def Task2():
'''
Download pretrained mobilenetv2
Returns:
- mobilenetv2 model
'''
base_model = tf.keras.applications.MobileNetV2()
return base_model
def Task3(base_model):
'''
Task3 - removing the last layer in netV2 and replace with custom dense layer
Params:
- base model of mobilenetv2
Returns:
- a new model with frozen weights having a fresh output layer
'''
#remove the output layer from the network
frozen_layers = tf.keras.Model(base_model.input,base_model.layers[-2].output)
#freeze weights of remaining layers
frozen_layers.trainable = False
#change pixel from [0,255] into [-1,1]
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input
# average spatial dimensions to one vector
# global_average_layer = tf.keras.layers.GlobalAveragePooling2D() # already included in the frozen layer
# output layer with 5 classes
prediction_layer = tf.keras.layers.Dense(5)
# regularization to reduce overfitting
# dropout_layer = tf.keras.layers.Dropout(0.2) # decided not to use
inputs = tf.keras.Input(shape=IMG_SHAPE)
x = preprocess_input(inputs)
x = frozen_layers(x, training=False)
outputs = prediction_layer(x) # TODO?: "activation:softmax"
model = tf.keras.Model(inputs, outputs)
return model
def split_dataset(dataset,split_amount):
'''
AUX function - split dataset in split_amount batch size
Params:
- dataset - dataset to be splitted
- split_amount - batch size to split
Returns:
- left_set will take split_amount of the dataset
- right_set will take remaning amount.
(if left_set takes everything from dataset, right_set will be empty)
'''
batchsize = len(dataset)
left_set = dataset.take( split_amount )
right_set = dataset.skip( split_amount )
return left_set ,right_set
def Task4(data_dir,train_perc=0.75,val_perc = 0.15, test_perc = 0.1 , rand_seed = 123):
'''
Preparing Dataset to training,validation and testing set.
Params:
data_dir - full datadirectory
train_perc,val_perc,test_perc - amount of dataset , sum of them must be 1
rand_seed - random seed
Returns:
train set, validation set , test set
class_names - list containing class names
'''
if(train_perc + val_perc + test_perc != 1):
raise Exception ("Dataset spltting went wrong, check DIRECTORY or Percent")
dataset = tf.keras.utils.image_dataset_from_directory(
data_dir,
shuffle = True,
seed=rand_seed,
image_size=IMG_SIZE,
batch_size=BATCH_SIZE)
class_names = dataset.class_names
total_size = len(dataset)
train_size = floor(total_size * train_perc)
remain_size = total_size - train_size
val_size = floor(remain_size * val_perc /(val_perc + test_perc))
train_dataset,val_dataset = split_dataset(dataset, train_size )
val_dataset, test_dataset = split_dataset(val_dataset,val_size)
print(class_names)
print("Training Batch Size : " , len(train_dataset))
print("Validation Batch Size : " , len(val_dataset))
print("Testing Batch Size : " , len(test_dataset))
return train_dataset,val_dataset,test_dataset,class_names
def Task5_compile(model,learning_rate , momentum):
'''
Compiling a model using SGD optimizer
Params:
learning_rate , momentum
Returns:
model - a compiled model using above parameters
'''
optimizer =tf.keras.optimizers.SGD(learning_rate=learning_rate,momentum=momentum,nesterov=False)
model.compile(optimizer = optimizer,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
return model
def Task5_train(model, train_ds,val_ds,epoch = 10):
'''
Fitting a model with using given train dataset and validation dataset in given epoch
Params:
train_ds - training dataset
val_ds - validation dataset
epoch - iterations times need to perform
Returns
history - a full log contained acc and loss.
'''
history = model.fit(train_ds,
epochs=epoch,
validation_data=val_ds)
return history
def Task5_train(model, train_ds,val_ds,epoch = 10):
'''
Fitting a model with using given train dataset and validation dataset in given epoch
Params:
train_ds - training dataset
val_ds - validation dataset
epoch - iterations times need to perform
Returns
history - a full log contained acc and loss.
'''
history = model.fit(train_ds,
epochs=epoch,
validation_data=val_ds)
return history
def Task6(history):
'''
Plotting a given history
Params:
history - a full log contained acc and loss
'''
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
learning_rate = str(history.model.optimizer.learning_rate.numpy())
momentum = str (history.model.optimizer.momentum.numpy())
fig = plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.suptitle(f'Learning Rate = {learning_rate} , Momentum = {momentum}',fontsize = 14 , y = 1.05)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy',fontsize = 8)
plt.ylim([0.2,1.0])
plt.xlabel('Epoch',fontsize = 8)
plt.title('Training and Validation Accuracy',fontsize = 8)
plt.subplot(1, 2, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('SparseCategorical CrossEntropy',fontsize = 8)
plt.ylim([0,2.0])
plt.title('Training and Validation Loss',fontsize = 8)
plt.xlabel('Epoch',fontsize = 8)
plt.show()
def TestIndividualImage(model,filename):
test_img = PIL.Image.open(filename).convert('RGB')
test_img = test_img.resize((224,224))
pix = np.array(test_img)
plt.imshow(pix)
f_data = np.expand_dims(pix,0)
y_pred = model.predict(f_data)
print("PREDICTED" , class_names[np.argmax(y_pred)])
def Model_F():
#include_top false -> we need to define our own imgshape.
base_model = tf.keras.applications.MobileNetV2()
# base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
# include_top=False,
# weights='imagenet')
#remove the output layer from the network
frozen_layers = tf.keras.Model(base_model.input,base_model.layers[-2].output)
#change pixel from [0,255] into [-1,1]
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input
inputs = tf.keras.Input(shape=IMG_SHAPE)
x = preprocess_input(inputs)
outputs = frozen_layers(x)
model = tf.keras.Model(inputs, outputs)
return model
def Model_N():
# dropout_layer = tf.keras.layers.Dropout(0.2)
prediction_layer = tf.keras.layers.Dense(5)
shape = (1280)
inputs = tf.keras.Input(shape=shape)
# x = dropout_layer(inputs)
outputs = prediction_layer(inputs)
model = tf.keras.Model(inputs, outputs)
return model
def Task9(model_f,train_ds,val_ds,test_ds):
'''
Preparation of dataset for accelerated model
params:
model_f - F function
train_ds,val_ds,test_ds - original dataset (x)
returns
three sets of F(x)
'''
train_batches = list(train_ds.unbatch().as_numpy_iterator())
new_train_y = [y for x,y in train_batches]
new_train_x = [x for x,y in train_batches]
new_train_x = model_f.predict(np.array(new_train_x))
val_batches = list(val_ds.unbatch().as_numpy_iterator())
new_val_y = [y for x,y in val_batches]
new_val_x= [x for x,y in val_batches]
new_val_x = model_f.predict(np.array(new_val_x))
test_batches = list(test_ds.unbatch().as_numpy_iterator())
new_test_y = [y for x,y in test_batches]
new_test_x = [x for x,y in test_batches]
new_test_x = model_f.predict(np.array(new_test_x))
new_train_ds = tf.data.Dataset.from_tensor_slices((new_train_x,new_train_y)).batch(32)
new_val_ds = tf.data.Dataset.from_tensor_slices((new_val_x,new_val_y)).batch(32)
new_test_ds = tf.data.Dataset.from_tensor_slices((new_test_x,new_test_y)).batch(32)
return new_train_ds,new_val_ds,new_test_ds
# *************** GLOBAL VARIABLES ( TWEAK FOR SETTING) *********************** #
BATCH_SIZE = 32
#default for v2 is 224,224
IMG_SIZE = (224, 224)
IMG_SHAPE = IMG_SIZE + (3,) # RGB
DATASET_DIRECTORY = 'small_flower_dataset'
# **************************************************************************************** #
if __name__ == "__main__":
# SEE "GLOBAL VARIABLES" IF YOU WANT TO TWEAK SETTINGS
full_dir , count = Task1(DATASET_DIRECTORY)
mobile_net_v2_model = Task2()
train_ds,val_ds,test_ds,class_names = Task4(data_dir = full_dir,
train_perc = 0.75,
val_perc = 0.15 ,
test_perc = 0.1)
# **** normal transfer learning - change following section for different learning_rate and momentumm *** #
# Task 3,5,6,7,8
learning_rate = 0.01
momentum = 0.0
epoch = 15
custom_model = Task3(mobile_net_v2_model)
transfer_learning_model = Task5_compile(model = custom_model,learning_rate = learning_rate,momentum = momentum)
transfer_learning_history = Task5_train(model = transfer_learning_model,train_ds = train_ds,val_ds = val_ds , epoch = epoch)
Task6(transfer_learning_history)
transfer_learning_model.evaluate(test_ds)
# EXTRA (if you want to predict an image using the model) #
# TestIndividualImage( model = transfer_learning_model, filename = 'a.jpg')
model_f = Model_F()
new_train_ds,new_val_ds,new_test_ds = Task9(model_f=model_f,train_ds=train_ds,val_ds=val_ds,test_ds=test_ds)
model_n = Model_N()
# **** accelerated learning - change following section for different learning_rate and momentumm *** #
# Task 10
learning_rate = 0.1
momentum = 0.6
epoch = 15
accelerated_learning_model = Task5_compile(model = model_n,learning_rate = learning_rate,momentum = momentum)
accelerated_learning_history = Task5_train(model = accelerated_learning_model,train_ds = new_train_ds,val_ds = new_val_ds , epoch = epoch)
Task6(accelerated_learning_history)
accelerated_learning_model.evaluate(new_test_ds)