forked from GeorgeSeif/Transfer-Learning-Suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
336 lines (270 loc) · 14.3 KB
/
main.py
File metadata and controls
336 lines (270 loc) · 14.3 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
from __future__ import print_function
# Networks
from keras.preprocessing import image
#from keras.applications.resnet26 import ResNet26
from keras.applications.resnet50 import ResNet50
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
from keras.applications.inception_v3 import InceptionV3
from keras.applications.xception import Xception
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.mobilenet import MobileNet
from keras.applications.mobilenet_v2 import MobileNetV2
from keras.applications.densenet import DenseNet121, DenseNet169, DenseNet201
from keras.applications.nasnet import NASNetLarge, NASNetMobile
from keras.preprocessing.image import ImageDataGenerator
# Layers
from keras.layers import Dense, Activation, Flatten, Dropout
from keras import backend as K
# Other
import sklearn
import tensorflow as tf
from keras import optimizers
from keras import losses
from keras import regularizers
from keras.optimizers import SGD, Adam
from keras.models import Sequential, Model
from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, TensorBoard, LambdaCallback
from keras.models import load_model
#from tensorboard.plugins.beholder import Beholder
# Utils
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random, glob
import os, sys, csv
import cv2
import time, datetime
# Files
import utils
# For boolean input from the command line
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
# Command line args
parser = argparse.ArgumentParser()
parser.add_argument('--num_epochs', type=int, default=20, help='Number of epochs to train for')
parser.add_argument('--mode', type=str, default="train", help='Select "train", or "predict" mode. \
Note that for prediction mode you have to specify an image to run the model on.')
parser.add_argument('--image', type=str, default=None, help='The image you want to predict on. Only valid in "predict" mode.')
parser.add_argument('--continue_training', type=str2bool, default=False, help='Whether to continue training from a checkpoint')
parser.add_argument('--dataset', type=str, default="dataset_w_2class", help='Dataset you are using.')
parser.add_argument('--resize_height', type=int, default=224, help='Height of cropped input image to network')
parser.add_argument('--resize_width', type=int, default=224, help='Width of cropped input image to network')
parser.add_argument('--batch_size', type=int, default=32, help='Number of images in each batch')
parser.add_argument('--dropout', type=float, default=0.5, help='Dropout ratio')
parser.add_argument('--h_flip', type=str2bool, default=False, help='Whether to randomly flip the image horizontally for data augmentation')
parser.add_argument('--v_flip', type=str2bool, default=False, help='Whether to randomly flip the image vertically for data augmentation')
parser.add_argument('--rotation', type=float, default=0.0, help='Whether to randomly rotate the image for data augmentation')
parser.add_argument('--zoom', type=float, default=0.0, help='Whether to randomly zoom in for data augmentation')
parser.add_argument('--shear', type=float, default=0.0, help='Whether to randomly shear in for data augmentation')
parser.add_argument('--model', type=str, default="MobileNet", help='Your pre-trained classification model of choice')
args = parser.parse_args()
# Global settings
BATCH_SIZE = args.batch_size
WIDTH = args.resize_width
HEIGHT = args.resize_height
FC_LAYERS = [1024, 1024]
TRAIN_DIR = args.dataset + "/train/"
VAL_DIR = args.dataset + "/val/"
TEST_DIR = args.dataset + "/test/"
preprocessing_function = None
base_model = None
# Prepare the model
if args.model == "VGG16":
from keras.applications.vgg16 import preprocess_input
preprocessing_function = preprocess_input
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "VGG19":
from keras.applications.vgg19 import preprocess_input
preprocessing_function = preprocess_input
base_model = VGG19(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "ResNet50":
from keras.applications.resnet50 import preprocess_input
preprocessing_function = preprocess_input
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "InceptionV3":
from keras.applications.inception_v3 import preprocess_input
preprocessing_function = preprocess_input
base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "Xception":
from keras.applications.xception import preprocess_input
preprocessing_function = preprocess_input
base_model = Xception(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "InceptionResNetV2":
from keras.applications.inceptionresnetv2 import preprocess_input
preprocessing_function = preprocess_input
base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "MobileNet":
from keras.applications.mobilenet import preprocess_input
preprocessing_function = preprocess_input
base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "MobileNetV2":
from keras.applications.mobilenet_v2 import preprocess_input
preprocessing_function = preprocess_input
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet121":
from keras.applications.densenet import preprocess_input
preprocessing_function = preprocess_input
base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet169":
from keras.applications.densenet import preprocess_input
preprocessing_function = preprocess_input
base_model = DenseNet169(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "DenseNet201":
from keras.applications.densenet import preprocess_input
preprocessing_function = preprocess_input
base_model = DenseNet201(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "NASNetLarge":
from keras.applications.nasnet import preprocess_input
preprocessing_function = preprocess_input
base_model = NASNetLarge(weights='imagenet', include_top=True, input_shape=(HEIGHT, WIDTH, 3))
elif args.model == "NASNetMobile":
from keras.applications.nasnet import preprocess_input
preprocessing_function = preprocess_input
base_model = NASNetMobile(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3))
else:
ValueError("The model you requested is not supported in Keras")
if args.mode == "train":
print("\n***** Begin training *****")
print("Dataset -->", args.dataset)
print("Model -->", args.model)
print("Resize Height -->", args.resize_height)
print("Resize Width -->", args.resize_width)
print("Num Epochs -->", args.num_epochs)
print("Batch Size -->", args.batch_size)
print("Data Augmentation:")
print("\tVertical Flip -->", args.v_flip)
print("\tHorizontal Flip -->", args.h_flip)
print("\tRotation -->", args.rotation)
print("\tZooming -->", args.zoom)
print("\tShear -->", args.shear)
print("")
# Create directories if needed
if not os.path.isdir("checkpoints"):
os.makedirs("checkpoints")
# Prepare data generators
train_datagen = ImageDataGenerator(
preprocessing_function=preprocessing_function,
rotation_range=args.rotation,
shear_range=args.shear,
zoom_range=args.zoom,
horizontal_flip=args.h_flip,
vertical_flip=args.v_flip
)
val_datagen = ImageDataGenerator(preprocessing_function=preprocessing_function)
test_datagen = ImageDataGenerator(preprocessing_function=preprocessing_function)
train_generator = train_datagen.flow_from_directory(TRAIN_DIR, target_size=(HEIGHT, WIDTH), batch_size=BATCH_SIZE, save_to_dir="./logs/aug/")
validation_generator = val_datagen.flow_from_directory(VAL_DIR, target_size=(HEIGHT, WIDTH), batch_size=int(BATCH_SIZE/2))
test_generator = test_datagen.flow_from_directory(TEST_DIR, target_size=(HEIGHT, WIDTH), batch_size=None)
# Save the list of classes for prediction mode later
class_list = utils.get_subfolders(TRAIN_DIR)
utils.save_class_list(class_list, model_name=args.model, dataset_name=args.dataset)
finetune_model = utils.build_finetune_model(base_model, dropout=args.dropout, fc_layers=FC_LAYERS, num_classes=len(class_list))
_today = datetime.datetime.today().strftime('%d-%m-%Y')
if "2" in args.dataset:
class_string = "2_class_"
else:
class_string = "3_class_"
if args.continue_training:
finetune_model.load_weights("./checkpoints/" + class_string + args.model + _today + "_model_weights.h5")
adam = Adam(lr=0.00001)
finetune_model.compile(adam, loss='categorical_crossentropy', metrics=['accuracy'])
num_train_images = utils.get_num_files(TRAIN_DIR)
num_val_images = utils.get_num_files(VAL_DIR)
# Decay definition
def lr_decay(epoch):
if epoch%20 == 0 and epoch!=0:
lr = K.get_value(model.optimizer.lr)
K.set_value(model.optimizer.lr, lr/2)
print("LR changed to {}".format(lr/2))
return K.get_value(model.optimizer.lr)
learning_rate_schedule = LearningRateScheduler(lr_decay)
# L2 regularization definition
l2_callback = LambdaCallback(on_epoch_end=lambda epoch, logs: regularizers.l2(0.01))
# checkpoint callback
filepath="./checkpoints/" + args.model + _today + "_model_weights.h5"
checkpoint = ModelCheckpoint(filepath, monitor=["acc"], verbose=1, mode='max')
"""
# early stopping callback
early_stop = EarlyStopping(monitor='val_acc', patience=10, restore_best_weights=True, mode='min', verbose=1)
callbacks_list = [early_stop]
"""
LOG_DIRECTORY = "./logs/"
tensorboard = TensorBoard(log_dir="./logs/{}/{}{}".format(args.model, class_string, datetime.datetime.now().strftime("%Y%m%d-%H%M%S")))
#beholder = Beholder(LOG_DIRECTORY)
callbacks_list = [tensorboard, l2_callback]
history = finetune_model.fit_generator(train_generator, epochs=args.num_epochs, workers=8,
steps_per_epoch=num_train_images // BATCH_SIZE,
validation_data=validation_generator, validation_steps=num_val_images // BATCH_SIZE,
class_weight='auto', shuffle=True, callbacks=callbacks_list)
"""
doc: https://keras.io/models/sequential/#evaluate_generator
test_history = finetune_model.evaluate_generator(test_generator, callbacks=callbacks_list)
"""
utils.plot_training(history=history, model_name=args.model, class_string=class_string, today=_today)
elif args.mode == "predict":
if args.image is None:
ValueError("You must pass an image path when using prediction mode.")
# Create directories if needed
if not os.path.isdir("%s"%("Predictions")):
os.makedirs("%s"%("Predictions"))
# Read in your image
image = cv2.imread(args.image,-1)
save_image = image
image = np.float32(cv2.resize(image, (HEIGHT, WIDTH)))
image = preprocessing_function(image.reshape(1, HEIGHT, WIDTH, 3))
class_list_file = "./checkpoints/" + args.model + "_" + args.dataset + "_class_list.txt"
class_list = utils.load_class_list(class_list_file)
finetune_model = utils.build_finetune_model(base_model, dropout=args.dropout, fc_layers=FC_LAYERS, num_classes=len(class_list))
finetune_model.load_weights("./checkpoints/" + args.model + "_model_weights.h5")
# Run the classifier and print results
st = time.time()
out = finetune_model.predict(image)
confidence = out[0]
class_prediction = list(out[0]).index(max(out[0]))
class_name = class_list[class_prediction]
run_time = time.time()-st
print("Predicted class = ", class_name)
print("Confidence = ", confidence)
print("Run time = ", run_time)
cv2.imwrite("Predictions/" + class_name[0] + ".png", save_image)
elif args.mode == "predict_dir":
from keras.preprocessing.image import load_img
val_datagen = ImageDataGenerator(preprocessing_function=preprocessing_function)
validation_generator = val_datagen.flow_from_directory(VAL_DIR, target_size=(HEIGHT, WIDTH), batch_size=int(BATCH_SIZE/2))
# Get the filenames from the generator
fnames = validation_generator.filenames
# Get the ground truth from generator
ground_truth = validation_generator.classes
# Get the label to class mapping from the generator
label2index = validation_generator.class_indices
# Getting the mapping from class index to class label
idx2label = dict((v,k) for k,v in label2index.items())
class_list_file = "./checkpoints/" + args.model + "_" + args.dataset + "_class_list.txt"
class_list = utils.load_class_list(class_list_file)
finetune_model = utils.build_finetune_model(base_model, dropout=args.dropout, fc_layers=FC_LAYERS, num_classes=len(class_list))
finetune_model.load_weights("./checkpoints/" + args.model + "_model_weights.h5")
# Get the predictions from the model using the generator
predictions = finetune_model.predict_generator(validation_generator, steps=validation_generator.samples/validation_generator.batch_size,verbose=1)
predicted_classes = np.argmax(predictions,axis=1)
errors = np.where(predicted_classes != ground_truth)[0]
print("No of errors = {}/{}".format(len(errors),validation_generator.samples))
# Show the errors
for i in range(len(errors)):
pred_class = np.argmax(predictions[errors[i]])
pred_label = idx2label[pred_class]
title = 'Original label:{}, Prediction :{}, confidence : {:.3f}'.format(
fnames[errors[i]].split('/')[0],
pred_label,
predictions[errors[i]][pred_class])
original = load_img('{}/{}'.format("./dataset_w_3class/val",fnames[errors[i]]))
plt.figure(figsize=[7,7])
plt.axis('off')
plt.title(title)
plt.imshow(original)
plt.show()