-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_and_validate.py
220 lines (169 loc) · 7.3 KB
/
train_and_validate.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
import tensorflow as tf
import argparse
from matplotlib import pyplot as plt
import pandas as pd
import os
import numpy as np
import logging
import datetime
import csv
from sklearn.utils import class_weight
print('tensorflow version:{}'.format(tf.__version__ ))
# ["0: Amblyomma, 1: Dermacentor, 2: Ixodes"] Class nums
img_shape = (224, 224, 3)
# Input arguments as a single comma separated line
def read_args():
parser = argparse.ArgumentParser(description="TickNet NN")
parser.add_argument("input",
type=str,
help="name of input file")
args = parser.parse_args()
return args
args = read_args()
def read_input(args):
inputs = [line.rstrip() for line in open(args.input)]
return inputs
inputs = read_input(args)
save_model = bool(inputs[9])
log = bool(inputs[10])
if log:
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s',
level=logging.INFO)
else:
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
def config(inputs):
logging.info("FUNCTION: config() -- initializing")
config_dict = {
"datetime": str(datetime.datetime.now()),
"test_name": inputs[0],
"train_dir": inputs[2],
"val_dir": inputs[3],
"num_classes": len(inputs[1].strip('][').split(', ')),
"classes": inputs[1].strip('][').split(', '),
"img_shape": img_shape,
"model": "ResNet50V2",
"optimizer": "Adam",
"learning_rate": float(inputs[4]),
"momentum": float(inputs[5]),
"epochs": int(inputs[6]),
"mbs": int(inputs[7]),
"loss": "CategoricalCrossentropy",
"weight_init": "imagenet",
"class_weights": bool(inputs[8])
}
# logging.info("FUNCTION: config() -- writing config_dict to config.txt")
# with open('config.txt', 'w') as f:
# print(config_dict, file=f)
with open('config_{}.csv'.format(lr), 'w') as f:
writer = csv.writer(f)
for key, value in config_dict.items():
writer.writerow([key, value])
logging.info("FUNCTION: config() -- exiting")
return config_dict
configs = config(inputs)
def define_model():
logging.info("FUNCTION: define_model() -- initializing")
base_model = tf.keras.applications.InceptionV3(
input_shape=img_shape,
include_top=False,
weights="imagenet")
# # Alternative model construction
# maxpool_layer = tf.keras.layers.GlobalMaxPooling2D()
# prediction_layer = tf.keras.layers.Dense(len(configs['classes']), activation='softmax')
# model = tf.keras.Sequential([
# base_model,
# maxpool_layer,
# prediction_layer
# ])
x = base_model.output
x1 = tf.keras.layers.GlobalMaxPooling2D()(x)
x2 = tf.keras.layers.Dense(len(configs['classes']), activation='softmax')(x1)
model = tf.keras.models.Model(inputs=base_model.input, outputs=x2)
model.summary() # print network architecture
opt = tf.keras.optimizers.SGD(lr=configs['lr'], momentum=configs['momentum'])
model.compile(optimizer=opt,
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])
logging.info("FUNCTION: define_model() -- exiting (model compiled)")
return model
def get_class_weights(train_nums):
logging.info("FUNCTION: get_class_weights() -- initializing")
class_weights = class_weight.compute_class_weight('balanced',
np.unique(train_nums).tolist(),
train_nums)
class_weight_dict = dict(enumerate(class_weights))
logging.info("FUNCTION: get_class_weights() -- exiting")
return class_weight_dict
def model_results(model, val_generator):
logging.info("FUNCTION: model_results() -- initializing")
pred = model.predict(val_generator, steps=len(val_generator), verbose=0)
results_dict = {
"fname2": [os.path.basename(file) for file in val_generator.filenames],
"class_num": val_generator.classes,
"label": [list(val_generator.class_indices)[idx] for idx in val_generator.classes],
"pred_num": np.argmax(pred, 1),
"pred_label": [list(val_generator.class_indices)[idx] for idx in np.argmax(pred, 1)],
"prob": [round(np.max(idx) * 100, 2) for idx in pred],
"prob_all": [idx for idx in pred]
}
correct = []
for idx in range(len(results_dict["fname2"])):
if results_dict['class_num'][idx] == results_dict['pred_num'][idx]:
correct.append(1)
else:
correct.append(0)
results_dict["correct"] = correct
df = pd.DataFrame.from_dict(results_dict)
logging.info("FUNCTION: model_results() -- exiting (results_dict returned as pd.Dataframe)")
return df
# Save model as json file (optional)
def model2json(model):
model_json = model.to_json()
with open("model_{}.json".format(lr), "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model_{}.h5".format(lr))
print("Saved model to disk")
def run_test_harness():
logging.info("FUNCTION: run_test_harness() -- initializing")
# define model
model = define_model()
# create data generator
datagen = tf.keras.preprocessing.image.ImageDataGenerator(featurewise_std_normalization=True,
featurewise_center=True)
# specify mean values for centering (ImageNet)
datagen.mean = [0.485, 0.456, 0.406]
datagen.std = 0.225 # [0.229, 0.224, 0.225]
# prepare iterator
train_it = datagen.flow_from_directory(configs['train_dir'],
class_mode='categorical',
batch_size=configs['mbs'],
target_size=(img_shape[0], img_shape[1]))
val_it = datagen.flow_from_directory(configs['val_dir'],
class_mode='categorical',
batch_size=configs['mbs'],
target_size=(img_shape[0], img_shape[1]),
shuffle=False)
if class_weights:
weights = get_class_weights(train_it.classes)
history = model.fit(train_it,
steps_per_epoch=len(train_it),
validation_data=val_it,
validation_steps=int(len(val_it)/2),
epochs=configs['epochs'],
class_weight=weights,
verbose=1)
else:
# fit model
history = model.fit(train_it,
steps_per_epoch=len(train_it),
validation_data=val_it,
validation_steps=int(len(val_it)/2),
epochs=configs['epochs'],
verbose=1)
results = model_results(model, val_it)
logging.info("FUNCTION: run_test_harness() -- writing results to results.csv")
results.to_csv("results.csv")
model.save('my_model') # Model directory. See Tensorflow method https://www.tensorflow.org/guide/keras/save_and_serialize
logging.info("FUNCTION: run_test_harness() -- exiting")
run_test_harness()