-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid_search.py
351 lines (277 loc) · 15.4 KB
/
grid_search.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
# coding: utf-8
# In[1]:
# get_ipython().run_line_magic('matplotlib', 'inline')
# get_ipython().run_line_magic('load_ext', 'autoreload')
# get_ipython().run_line_magic('autoreload', '2')
# In[2]:
# In[4]:
# params = {}
# params["train_dir"] = "train"
# params["test_dir"] = "test"
# params["batch_size"] = 8
# params["img_width"] = 221
# params["img_height"] = 221
# params["loss"] = "categorical_crossentropy"
# params["metrics"] = ['top_k_categorical_accuracy', 'accuracy']
# params["initial_epoch"] = 2
# params["final_epoch"] = 4
# params["workers"] = 8
# params["step_per_epoch"] = 32
# params["train_threshold"] = 0
# params["phase1_optimizer"] = "adam"
# params["model_list"] = ["InceptionV3", "xception", "InceptionResNetV2", "DenseNet121", "DenseNet169", "DenseNet201"]
# params["dropout_list"] = [0.1, 0.2, 0.3]
# params["dense_list"] = [512, 1024]
# with open("params.json", "w") as f:
# json.dump(params, f)
# In[5]:
def run() :
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.xception import Xception
from keras.applications.densenet import DenseNet121
from keras.applications.densenet import DenseNet169
from keras.applications.densenet import DenseNet201
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D, Dropout
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
from keras.optimizers import Adam
from keras.metrics import top_k_categorical_accuracy
import math
from keras.callbacks import TensorBoard
import os
import json
from collections import defaultdict
import keras
import tensorflow as tf
import shutil
# In[3]:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
params = {}
with open("params_manual.json", "r") as f:
params = json.load(f)
if ("model_file" in params):
val_datagen = ImageDataGenerator(rescale=1./255)
params["test_size"] = sum([len(files) for r, d, files in os.walk( params["test_dir"] )])
validation_generator = val_datagen.flow_from_directory(
params["test_dir"],
target_size = (params["img_width"], params["img_height"]),
batch_size = params["batch_size"],
shuffle = True,
class_mode = 'categorical')
sess = tf.Session()
K.set_session(sess)
params["historypath"] = "./history/current"
if not os.path.exists(params['historypath']):
os.makedirs(params['historypath'])
model = keras.models.load_model(params["model_file"])
history = model.evaluate_generator(generator = validation_generator,
steps=math.ceil(params["test_size"] / params["batch_size"]),
max_queue_size=10,
workers=params["workers"],
use_multiprocessing=False)
final_history = {}
count = 0
for i in model.metrics_names:
final_history[i] = history[count]
count += 1
final_history['model'] = params["model_file"]
with open(params["historypath"] + "/model.txt", "w") as f:
json.dump(final_history, f)
del model
sess.close()
tf.reset_default_graph()
K.clear_session()
return
params["train_size"] = sum([len(files) for r, d, files in os.walk( params["train_dir"] )])
params["test_size"] = sum([len(files) for r, d, files in os.walk( params["test_dir"] )])
params["classes"] = sum([len(d) for r, d, files in os.walk( params["train_dir"] )])
params["phase2_optimizer"] = SGD(lr=0.001, momentum=0.9)
params["dense_num"] = 2
params["dense2"] = {"num":params["classes"], "activation":"softmax"}
# In[6]:
train_datagen = ImageDataGenerator(
rescale=1./255,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
params["train_dir"],
target_size = (params["img_width"], params["img_height"]),
batch_size = params["batch_size"],
shuffle = True,
class_mode = 'categorical')
val_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = val_datagen.flow_from_directory(
params["test_dir"],
target_size = (params["img_width"], params["img_height"]),
batch_size = params["batch_size"],
shuffle = True,
class_mode = 'categorical')
# In[7]:
def create_model():
base_model=None
if params["model"] == "InceptionV3":
params["train_threshold"] = 249
base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=None, input_shape=(params["img_width"], params["img_height"], 3))
elif params["model"] == "xception":
params["train_threshold"] = 106
base_model = Xception(weights='imagenet', include_top=False, input_tensor=None, input_shape=(params["img_width"], params["img_height"], 3))
elif params["model"] == "InceptionResNetV2":
params["train_threshold"] = 727
base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=None, input_shape=(params["img_width"], params["img_height"], 3))
elif params["model"] == "DenseNet121":
params["train_threshold"] = 403
base_model = DenseNet121(weights='imagenet', include_top=False, input_tensor=None, input_shape=(params["img_width"], params["img_height"], 3))
elif params["model"] == "DenseNet169":
params["train_threshold"] = 571
base_model = DenseNet169(weights='imagenet', include_top=False, input_tensor=None, input_shape=(params["img_width"], params["img_height"], 3))
elif params["model"] == "DenseNet201":
params["train_threshold"] = 683
base_model = DenseNet201(weights='imagenet', include_top=False, input_tensor=None, input_shape=(params["img_width"], params["img_height"], 3))
elif params["model"] == "ResNet50":
params["train_threshold"] = 140
base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=None, pooling=None, input_shape=(params["img_width"], params["img_height"], 3))
else:
print("unknown model")
count = 0
modelx = base_model.output
while count < params["dense_num"]:
count += 1
string = "dense"+str(count)
if "pool" in params[string]:
if params[string]["pool"] == "avg_poolx":
modelx = GlobalAveragePooling2D(name=params[string]["pool"])(modelx)
modelx = Dense(params[string]["num"], activation = params[string]["activation"])(modelx)
if "dropout" in params[string]:
modelx = Dropout(params[string]["dropout"])(modelx)
model = Model(inputs=base_model.input, output=modelx)
for layer in base_model.layers:
layer.trainable = False
model.compile(loss=params["loss"], optimizer=params["phase1_optimizer"], metrics=params["metrics"])
return model
# In[ ]:
class log_history(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
pass
def on_batch_end(self, batch, logs={}):
best_model1 = {}
best_model1['model'] = params['model']
best_model1['acc'] = str(logs.get('acc'))
best_model1['top_k_categorical_accuracy'] = str(logs.get('top_k_categorical_accuracy'))
best_model1['loss'] = str(logs.get('loss'))
with open("./history/current/" + 'model.txt', "w") as f:
json.dump(best_model1, f)
params["batch"]["loss"].append(str(logs.get('loss')))
params["batch"]["top_k_categorical_accuracy"].append(str(logs.get('top_k_categorical_accuracy')))
params["batch"]["acc"].append(str(logs.get('acc')))
#print(params["history"])
with open(params["historypath"] + "/batch.json", "w") as f:
json.dump(params["batch"], f)
def on_epoch_end(self, batch, logs={}):
best_model1 = {}
best_model1['model'] = params['model']
best_model1['val_acc'] = str(logs.get('val_acc'))
best_model1['val_top_k_categorical_accuracy'] = str(logs.get('val_top_k_categorical_accuracy'))
best_model1['val_loss'] = str(logs.get('val_loss'))
best_model1['acc'] = str(logs.get('acc'))
best_model1['top_k_categorical_accuracy'] = str(logs.get('top_k_categorical_accuracy'))
best_model1['loss'] = str(logs.get('loss'))
with open("./history/current/" + 'model.txt', "w") as f:
json.dump(best_model1, f)
params["history"]["val_top_k_categorical_accuracy"].append(logs.get('val_top_k_categorical_accuracy'))
params["history"]["val_loss"].append(logs.get('val_loss'))
params["history"]["loss"].append(logs.get('loss'))
params["history"]["top_k_categorical_accuracy"].append(logs.get('top_k_categorical_accuracy'))
params["history"]["acc"].append(logs.get('acc'))
params["history"]["val_acc"].append(logs.get('val_acc'))
#print(params["history"])
with open(params["historypath"] + "/epoch.json", "w") as f:
json.dump(params["history"], f)
# In[ ]:
max_accuracy = 0
for model_name in params["model_list"]:
params["model"] = model_name
best_accuracy = 0
if os.path.exists('./history/current'):
shutil.rmtree('./history/current')
for dropout_num in params["dropout_list"]:
for dense_num in params["dense_list"]:
sess = tf.Session()
K.set_session(sess)
params["dense1"] = {"num":dense_num, "dropout":dropout_num, "pool":"avg_poolx", "activation":"relu"}
params["tensorpath"] = "./tensor/" + params['model'] + "/" + "dense_"+str(dense_num) + "_dropout_" + str(dropout_num)
params["historypath"] = "./history/current" + "/" + "dense_"+str(dense_num) + "_dropout_" + str(dropout_num)
tbd = TensorBoard(log_dir=params["tensorpath"] , batch_size=params["batch_size"], write_graph=True )
params["history"] = {}
params["history"]["val_top_k_categorical_accuracy"] = []
params["history"]["val_loss"] = []
params["history"]["loss"] = []
params["history"]["top_k_categorical_accuracy"] = []
params["history"]["acc"] = []
params["history"]["val_acc"] = []
params["batch"] = {}
params["batch"]["loss"] = []
params["batch"]["acc"] = []
params["batch"]["top_k_categorical_accuracy"] = []
model = create_model()
epoch_history = log_history()
if not os.path.exists(params['historypath']):
os.makedirs(params['historypath'])
history = model.fit_generator(generator=train_generator,
steps_per_epoch = params["step_per_epoch"],
epochs = params["initial_epoch"],
use_multiprocessing=False,
max_queue_size=10,
workers = params["workers"],
validation_data = validation_generator,
callbacks=[tbd, epoch_history],
validation_steps = math.ceil(params["test_size"] / params["batch_size"]))
for layer in model.layers[:params["train_threshold"]]:
layer.trainable = False
for layer in model.layers[params["train_threshold"]:]:
layer.trainable = True
params["phase2_optimizer"] = SGD(lr=0.001, momentum=0.9)
model.compile(loss=params["loss"], optimizer=params["phase2_optimizer"], metrics=params["metrics"])
history1 = model.fit_generator(generator=train_generator,
steps_per_epoch = params["step_per_epoch"] ,
epochs = params["final_epoch"] ,
initial_epoch= params["initial_epoch"] ,
use_multiprocessing=False,
max_queue_size=10,
workers = params["workers"],
validation_data = validation_generator,
callbacks=[tbd, epoch_history],
validation_steps = math.ceil(params["test_size"] / params["batch_size"]))
if not os.path.exists("./history/best/" + params["model"]):
os.makedirs("./history/best/" + params["model"])
if (history1.history['val_acc'][-1] > best_accuracy):
best_accuracy = history1.history['val_acc'][-1]
model.save("model_" + params["model"] + ".h5")
shutil.copyfile(params["historypath"] + "/epoch.json", "./history/best/" + params["model"] +"/epoch.json")
shutil.copyfile(params["historypath"] + "/batch.json", "./history/best/" + params["model"] +"/batch.json")
if (history1.history['val_acc'][-1] > max_accuracy):
max_accuracy = history1.history['val_acc'][-1]
model.save("best_model" + ".h5")
best_model = {}
best_model['model'] = params["model"]
best_model["val_acc"] = history1.history['val_acc'][-1]
best_model['val_top_k_categorical_accuracy'] = history1.history['val_top_k_categorical_accuracy'][-1]
best_model['val_loss'] = history1.history['val_loss'][-1]
best_model["acc"] = history1.history['acc'][-1]
best_model["top_k_categorical_accuracy"] = history1.history['top_k_categorical_accuracy'][-1]
best_model["loss"] = history1.history['loss'][-1]
with open("./history/best/" + 'model.txt', "w") as f:
json.dump(best_model, f)
del model
sess.close()
tf.reset_default_graph()
K.clear_session()
if __name__ == '__main__' :
run()