-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
278 lines (213 loc) · 8.7 KB
/
train.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
import argparse
import csv
import io as io_simple
import os
import random
import keras
import matplotlib
import matplotlib.pyplot as plot
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
from keras.callbacks import EarlyStopping
from keras.callbacks import TensorBoard
from skimage import exposure
from skimage import io
from skimage import transform
from sklearn.metrics import classification_report
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical
from trafficSignCnn_v4 import TrafficSignNet_v4
from trainingMonitor import TrainingMonitor
matplotlib.use("Agg")
EVALSIZE = 20
# data split and training done according to Adrian Rosebrock's model, found at https://www.pyimagesearch.com/2019/11/04/traffic-sign-classification-with-keras-and-deep-learning/
def writeTopToCSV(name, list):
with open(name, mode='w') as top_file:
top_writer = csv.writer(top_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC,
lineterminator="\n")
for top in list:
top_writer.writerow(top)
def evaluate(model, evalX, evalY):
stats = {0: 0, 1: 0, 2: 0, 3: 0}
top5 = []
for i, image in enumerate(evalX):
image = image.astype("float32") / 255.0
image = np.expand_dims(image, axis=0)
preds = model.predict(image)
top = np.argsort(-preds, axis=1)
if evalY[i] == top[0][0]:
stats[0] += 1
elif evalY[i] == top[0][1]:
stats[1] += 1
elif evalY[i] == top[0][2]:
stats[2] += 1
else:
stats[3] += 1
top5.append([top[0][0], top[0][1], top[0][2], top[0][3], top[0][4], evalY[i]])
return stats, top5
# split training data again into TRAINING and FINAL EVALUATION/TESTING - DISJOINT
def load_data_and_labels(basePath, csvPath, evaluation_split=False):
data = []
labels = []
# count the number of images
rows = open(csvPath).read().strip().split("\n")[1:]
random.shuffle(rows)
if evaluation_split:
eval_dict = {}
# for each image
for (i, row) in enumerate(rows):
if i > 0 and i % 1000 == 0:
print("[INFO] processed {} total images".format(i))
# find path and id
(label, imagePath) = row.strip().split(",")[-2:]
imagePath = os.path.sep.join([basePath, imagePath])
image = io.imread(imagePath)
image = transform.resize(image, (32, 32))
image = exposure.equalize_adapthist(image, clip_limit=0.1)
intaux = int(label)
if evaluation_split:
if intaux not in eval_dict:
eval_dict[intaux] = [image]
elif len(eval_dict[intaux]) < EVALSIZE - 1:
eval_dict[intaux].append(image)
else:
data.append(image)
labels.append(intaux)
else:
data.append(image)
labels.append(intaux)
if evaluation_split:
eval_data = []
eval_labels = []
for key in eval_dict:
for value in eval_dict[key]:
eval_data.append(value)
eval_labels.append(key)
data = np.array(data)
labels = np.array(labels)
if evaluation_split:
return ((eval_data, eval_labels), (data, labels))
return data, labels
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True, help="path to input training model")
ap.add_argument("-m", "--model", required=True, help="path to output model")
ap.add_argument("-o", "--output", required=True, help="path to output dictionary")
ap.add_argument("-p", "--plot", type=str, default="plot.png", help="path to training history plot")
args = vars(ap.parse_args())
# default parameters, might need adjustment
NUM_EPOCHS = 100
INIT_LR = 1e-3
BS = 64
labelNames = open("signnames.csv").read().strip().split("\n")[1:]
labelNames = [l.split(",")[1] for l in labelNames]
trainPath = os.path.sep.join([args["dataset"], "Train.csv"])
testPath = os.path.sep.join([args["dataset"], "Test.csv"])
# load data to a tuple with the previous function
print("[INFO] loading training, validation and evaluation data...")
# train data
((evalX, evalY), (trainX, trainY)) = load_data_and_labels(args["dataset"], trainPath, evaluation_split=True)
# test data
(testX, testY) = load_data_and_labels(args["dataset"], testPath)
# scale to [0, 1]
trainX = trainX.astype("float32") / 255.0
testX = testX.astype("float32") / 255.0
# convert to one-hot encoding (boolean values from which only one is true at a time)
numLabels = len(np.unique(trainY))
trainY = to_categorical(trainY, numLabels)
testY = to_categorical(testY, numLabels)
classTotals = trainY.sum(axis=0)
classWeight = classTotals.max() / classTotals
aug = ImageDataGenerator(
rotation_range=10,
zoom_range=0.15,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.15,
horizontal_flip=False,
vertical_flip=False,
fill_mode="nearest"
)
print("[INFO] compiling model...")
base_learning_rate = 0.0001
opt = Adam(lr=INIT_LR, decay=INIT_LR / (NUM_EPOCHS * 0.5))
model = TrafficSignNet_v4.build(width=32, height=32, depth=3, classes=numLabels)
file_writer = tf.summary.create_file_writer("logs\\fit\\" + args["output"] + "\\cm")
def log_confusion_matrix(epoch, logs):
# Use the model to predict the values from the validation dataset.
test_pred = model.predict(testX)
con_mat = tf.math.confusion_matrix(labels=testY.argmax(axis=1), predictions=test_pred.argmax(axis=1)).numpy()
con_mat_norm = np.around(con_mat.astype('float') / con_mat.sum(axis=1)[:, np.newaxis], decimals=2)
classes = [i for i in range(numLabels)]
con_mat_df = pd.DataFrame(con_mat_norm,
index=classes,
columns=classes)
figure = plot.figure(figsize=(16, 16))
sns.heatmap(con_mat_df, annot=True, cmap=plot.cm.Blues)
plot.tight_layout()
plot.ylabel('True label')
plot.xlabel('Predicted label')
buf = io_simple.BytesIO()
plot.savefig(buf, format='png')
plot.close(figure)
buf.seek(0)
image = tf.image.decode_png(buf.getvalue(), channels=4)
image = tf.expand_dims(image, 0)
# Log the confusion matrix as an image summary.
with file_writer.as_default():
tf.summary.image("Confusion Matrix", image, step=epoch)
cm_callback = keras.callbacks.LambdaCallback(on_epoch_end=log_confusion_matrix)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
log_dir = "logs\\fit\\" + args["output"]
tensorboard_callback = TensorBoard(log_dir=log_dir, histogram_freq=1)
# training monitor
figPath = os.path.sep.join([args["output"], "{}.png".format(os.getpid())])
jsonPath = os.path.sep.join([args["output"], "{}.json".format(os.getpid())])
callbacks = [TrainingMonitor(figPath, jsonPath=jsonPath),
tensorboard_callback, cm_callback, EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)]
print("[INFO] training network...")
H = model.fit_generator(
aug.flow(trainX, trainY, batch_size=BS),
validation_data=(testX, testY),
steps_per_epoch=trainX.shape[0] // BS,
epochs=NUM_EPOCHS,
class_weight=classWeight,
callbacks=callbacks,
verbose=1
)
print("[INFO] serializing network to '{}'...".format(args["model"]))
model.save(args["model"])
evalXDum = list(evalX)
evalYDum = list(evalY)
stats, top5 = evaluate(model, evalXDum, evalYDum)
writeTopToCSV(args["model"] + '\\top5.csv', top5)
plot.bar(range(len(stats)), list(stats.values()), align='center')
plot.xticks(range(len(stats)), list(stats.keys()))
plot.savefig(args["model"] + "\\stats.jpg")
evalX = np.array(evalX, dtype=np.float32) / 255.0
evalY = to_categorical(evalY, numLabels)
print("[INFO] evaluating network...")
predictions = model.predict(evalX, batch_size=BS)
f = open(args["model"] + "/classification_report.txt", "w+")
f.write(classification_report(evalY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames))
f.close()
# add evaluation part
# for each model find how many were predicted right first try
# second try
# third try
# for each prediction print top 5 and what it should have been
N = np.arange(0, len(H["loss"]))
plot.style.use("ggplot")
plot.figure()
plot.plot(N, H["loss"], label="train_loss")
plot.plot(N, H["val_loss"], label="val_loss")
plot.plot(N, H["accuracy"], label="train_acc")
plot.plot(N, H["val_accuracy"], label="val_acc")
plot.title("Training Loss and Accuracy [Epoch {}]".format(
len(H["loss"])))
plot.xlabel("Epoch #")
plot.ylabel("Loss/Accuracy")
plot.legend(loc="lower left")
plot.savefig(args["plot"])