-
Notifications
You must be signed in to change notification settings - Fork 80
/
retrain_cnn_keras.py
89 lines (75 loc) · 2.72 KB
/
retrain_cnn_keras.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
from keras.models import load_model
from keras.utils import np_utils
from keras import optimizers
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
import numpy as np
import pickle
from time import time
def train(pre_model_name, new_model_name, learning_rate, epochs, batch_size):
with open("train_images", "rb") as f:
train_images = np.array(pickle.load(f))
with open("train_labels", "rb") as f:
train_labels = np.array(pickle.load(f), dtype=np.uint8)
with open("test_images", "rb") as f:
test_images = np.array(pickle.load(f))
with open("test_labels", "rb") as f:
test_labels = np.array(pickle.load(f), dtype=np.uint8)
with open("val_images", "rb") as f:
val_images = np.array(pickle.load(f))
with open("val_labels", "rb") as f:
val_labels = np.array(pickle.load(f), dtype=np.uint8)
train_images = np.reshape(train_images, (train_images.shape[0], 100, 100, 1))
test_images = np.reshape(test_images, (test_images.shape[0], 100, 100, 1))
val_images = np.reshape(val_images, (val_images.shape[0], 100, 100, 1))
train_labels = np_utils.to_categorical(train_labels)
test_labels = np_utils.to_categorical(test_labels)
val_labels = np_utils.to_categorical(val_labels)
checkpoint = ModelCheckpoint(new_model_name, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
tensorboard = TensorBoard(log_dir="./logs/{}".format(time()))
callbacks_list = [checkpoint, tensorboard]
model = load_model(pre_model_name)
sgd = optimizers.SGD(lr=learning_rate)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.fit(train_images, train_labels, validation_data=(test_images, test_labels), epochs=epochs, batch_size=batch_size, callbacks=callbacks_list)
model = load_model(new_model_name)
scores = model.evaluate(val_images, val_labels, verbose=1)
print("CNN Error: %.2f%%" % (100-scores[1]*100))
while True:
pre_model_name = input('Enter pre trained model file name: ')
if pre_model_name != '':
break
new_model_name = input('Enter new model file name (default is same as pre trained model file name): ')
if new_model_name == '':
new_model_name = pre_model_name
while True:
learning_rate = input('Enter learning rate (default 0.01): ')
if learning_rate == '':
learning_rate = 0.01
break
try:
learning_rate = float(learning_rate)
break
except:
continue
while True:
epochs = input('Enter epochs (default 100): ')
if epochs == '':
epochs = 100
break
try:
epochs = int(epochs)
break
except:
continue
while True:
batch_size = input('Enter batch size (default 50): ')
if batch_size == '':
batch_size = 50
break
try:
batch_size = int(batch_size)
break
except:
continue
train(pre_model_name, new_model_name, learning_rate, epochs, batch_size)