-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_pokemon_small.py
269 lines (221 loc) · 10.3 KB
/
train_pokemon_small.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
import os
if os.system('nvidia-smi') == 0:
import setGPU
import tensorflow as tf
import glob
import sys
import argparse
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import roc_auc_score
import resnet_v1_eembc
import resnet_rn06_eembc
import yaml
import csv
# from keras_flops import get_flops # (different flop calculation)
import kerop
import datetime
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.layers.experimental.preprocessing import RandomCrop
random_crop_model = tf.keras.models.Sequential()
random_crop_model.add(RandomCrop(32, 32, input_shape=(32, 32, 3,)))
def random_crop(x):
return random_crop_model.predict(x)
def get_lr_schedule_func(initial_lr, lr_decay):
def lr_schedule_func(epoch):
return initial_lr * (lr_decay ** epoch)
return lr_schedule_func
def main(args):
# parameters
input_shape = [32, 32, 3]
num_classes = 10
with open(args.config) as stream:
config = yaml.safe_load(stream)
num_filters = config['model']['filters']
kernel_sizes = config['model']['kernels']
strides = config['model']['strides']
l1p = float(config['model']['l1'])
l2p = float(config['model']['l2'])
skip = bool(config['model']['skip'])
avg_pooling = bool(config['model']['avg_pooling'])
batch_size = config['fit']['batch_size']
num_epochs = config['fit']['epochs']
verbose = config['fit']['verbose']
patience = config['fit']['patience']
save_dir = config['save_dir']
model_name = config['model']['name']
loss = config['fit']['compile']['loss']
model_file_path = os.path.join(save_dir, 'small_model_best.h5')
# quantization parameters
if 'quantized' in model_name:
logit_total_bits = config["quantization"]["logit_total_bits"]
logit_int_bits = config["quantization"]["logit_int_bits"]
activation_total_bits = config["quantization"]["activation_total_bits"]
activation_int_bits = config["quantization"]["activation_int_bits"]
alpha = config["quantization"]["alpha"]
use_stochastic_rounding = config["quantization"]["use_stochastic_rounding"]
logit_quantizer = config["quantization"]["logit_quantizer"]
activation_quantizer = config["quantization"]["activation_quantizer"]
try:
final_activation = bool(config['model']['final_activation'])
except:
print("No final_activation parameter in .yml file (RN06 doesn't have one)")
# optimizer
optimizer = getattr(tf.keras.optimizers, config['fit']['compile']['optimizer'])
initial_lr = config['fit']['compile']['initial_lr']
lr_decay = config['fit']['compile']['lr_decay']
# load dataset
#(X_train, y_train), (X_test, y_test) = cifar10.load_data()
# X_train, X_test = X_train/256., X_test/256.
#y_train = tf.keras.utils.to_categorical(y_train, num_classes)
#y_test = tf.keras.utils.to_categorical(y_test, num_classes)
#if loss == 'squared_hinge':
# y_train = y_train * 2 - 1 # -1 or 1 for hinge loss
# y_test = y_test * 2 - 1
# define data generator
datagen = ImageDataGenerator(
rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
validation_split=0.25,
rescale=1./255 # normalize values to between 0-1
# preprocessing_function=random_crop,
#brightness_range=(0.9, 1.2),
#contrast_range=(0.9, 1.2)
)
tdatagen = ImageDataGenerator(rescale=1./255)
train_generator = datagen.flow_from_directory('./data/SmallPokemonData/',
target_size=(32,32),
batch_size=batch_size,
color_mode='rgb',
class_mode='categorical',
interpolation='bilinear',
subset='training',
keep_aspect_ratio=True,
)
validation_generator = datagen.flow_from_directory('./data/SmallPokemonData/',
target_size=(32,32),
batch_size=batch_size,
color_mode='rgb',
class_mode='categorical',
interpolation='bilinear',
subset='validation',
keep_aspect_ratio=True
)
test_generator = datagen.flow_from_directory('./data/PokeCard/',
target_size=(32,32),
batch_size=batch_size,
color_mode='rgb',
class_mode='categorical',
interpolation='bilinear')#,
#keep_aspect_ratio=True)
test2_generator = tdatagen.flow_from_directory('./data/PokeCard/',
target_size=(32,32),
batch_size=batch_size,
color_mode='rgb',
class_mode='categorical',
interpolation='bilinear')#,
#keep_aspect_ratio=True)
# run preprocessing on training dataset
#datagen.fit(X_train)
kwargs = {'input_shape': input_shape,
'num_classes': num_classes,
'num_filters': num_filters,
'kernel_sizes': kernel_sizes,
'strides': strides,
'l1p': l1p,
'l2p': l2p,
'skip': skip,
'avg_pooling': avg_pooling}
# pass quantization params
if 'quantized' in model_name:
kwargs["logit_total_bits"] = logit_total_bits
kwargs["logit_int_bits"] = logit_int_bits
kwargs["activation_total_bits"] = activation_total_bits
kwargs["activation_int_bits"] = activation_int_bits
kwargs["alpha"] = None if alpha == 'None' else alpha
kwargs["use_stochastic_rounding"] = use_stochastic_rounding
kwargs["logit_quantizer"] = logit_quantizer
kwargs["activation_quantizer"] = activation_quantizer
try:
kwargs["final_activation"] = final_activation
except:
print("No final_activation parameter in .yml file (RN06 doesn't have one)")
# define model
model = getattr(resnet_rn06_eembc, model_name)(**kwargs)
# print model summary
print('#################')
print('# MODEL SUMMARY #')
print('#################')
print(model.summary())
print('#################')
# analyze FLOPs (see https://github.com/kentaroy47/keras-Opcounter)
layer_name, layer_flops, inshape, weights = kerop.profile(model)
# visualize FLOPs results
total_flop = 0
for name, flop, shape in zip(layer_name, layer_flops, inshape):
print("layer:", name, shape, " MFLOPs:", flop/1e6)
total_flop += flop
print("Total FLOPs: {} MFLOPs".format(total_flop/1e6))
tf.keras.utils.plot_model(model,
to_file="model.png",
show_shapes=True,
show_dtype=False,
show_layer_names=False,
rankdir="TB",
expand_nested=False)
# Alternative FLOPs calculation (see https://github.com/tokusumi/keras-flops), ~same answer
#total_flop = get_flops(model, batch_size=1)
#print("FLOPS: {} GLOPs".format(total_flop/1e9))
# compile model with optimizer
model.compile(optimizer=optimizer(learning_rate=initial_lr),
loss=loss,
metrics=['accuracy'])
# callbacks
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler
lr_schedule_func = get_lr_schedule_func(initial_lr, lr_decay)
log_dir = "logs/fit_rn06_10/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
callbacks = [ModelCheckpoint(model_file_path, monitor='val_accuracy', verbose=verbose, save_best_only=True),
EarlyStopping(monitor='val_accuracy', patience=patience, verbose=verbose, restore_best_weights=True),
LearningRateScheduler(lr_schedule_func, verbose=verbose),
tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
]
# train
history = model.fit_generator(train_generator,
steps_per_epoch = train_generator.samples // batch_size,
validation_data = validation_generator,
validation_steps = validation_generator.samples // batch_size,
epochs = num_epochs,
callbacks=callbacks,
verbose=verbose)
'''
history = model.fit(datagen.flow(X_train, y_train, batch_size=batch_size),
steps_per_epoch=X_train.shape[0] // batch_size,
epochs=num_epochs,
validation_data=(X_test, y_test),
callbacks=callbacks,
verbose=verbose)
'''
# restore "best" model
model.load_weights(model_file_path)
# get predictions
y_pred = model.predict(test_generator)
# evaluate with test dataset and share same prediction results
evaluation = model.evaluate(test_generator)
y_test = test_generator.classes
auc = roc_auc_score(y_test, y_pred, average='weighted', multi_class='ovr')
print('Model test accuracy = %.3f' % evaluation[1])
print('Model test weighted average AUC = %.3f' % auc)
#Test on un augmented test set too
y_pred = model.predict(test2_generator)
# evaluate with test dataset and share same prediction results
evaluation = model.evaluate(test2_generator)
y_test = test_generator.classes
auc = roc_auc_score(y_test, y_pred, average='weighted', multi_class='ovr')
print('Model test2 accuracy = %.3f' % evaluation[1])
print('Model test2 weighted average AUC = %.3f' % auc)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', type=str, default="baseline.yml", help="specify yaml config")
args = parser.parse_args()
main(args)