-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathtraffic_sign_detection.py
288 lines (233 loc) · 7.99 KB
/
traffic_sign_detection.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
# -*- coding: utf-8 -*-
import os
from tensorflow.keras.regularizers import l2
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.models import Sequential
from tensorflow import keras
import tensorflow as tf
from imgaug import augmenters as iaa
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
import numpy as np
import cv2 as cv
import csv
import random
PATH = '/content/Dataset'
TESTING_PATH = '/content/Dataset/Test'
# Minimal number of samples for each class for data augmentation
MIN_IMGS_IN_CLASS = 500
# Learning parameters
EPOCHS = 100
INIT_LR = 0.001
BATCH_SIZE = 256
SET_DECAY = True
# Imports
# images and corresponding labels
images = []
labels = []
# loop over all 43 classes
gtFile = open(PATH + '/Train.csv')
gtReader = csv.reader(gtFile, delimiter=',') # csv parser for annotations file
next(gtReader)
# loop over all images in current annotations file
for row in gtReader:
img = cv.imread(PATH + '/' + row[7])
images.append(cv.resize(img, (28, 28)))
labels.append(row[6]) # the 6th column is the label
gtFile.close()
print('Number of loaded images: ' + str(len(images)))
print('Number of loaded labels: ' + str(len(labels)))
train_X = np.asarray(images)
train_X = train_X / 255
train_X = np.asarray(train_X, dtype="float32")
train_Y = np.asarray(labels, dtype="float32")
print('Shape of training array: ' + str(train_X.shape))
def count_images_in_classes(lbls):
"""
Determining number of images in a class for Graph
:param lbls: Labels (different classes)
:return: Dictionary of labels and count
"""
dct = {}
for i in lbls:
if i in dct:
dct[i] += 1
else:
dct[i] = 1
return dct
samples_distribution = count_images_in_classes(train_Y)
print(samples_distribution)
def distribution_diagram(dct):
"""
Histogram of Count and Label
:param dct: Dictionary of labels and count
:return: Histogram
"""
plt.bar(range(len(dct)), list(dct.values()), align='center')
plt.xticks(range(len(dct)), list(dct.keys()), rotation=90, fontsize=7)
plt.show()
distribution_diagram(samples_distribution)
def preview(images, labels):
"""
Preview of a Images rom a label
:param images: Image from a Label
:param labels: Label
"""
plt.figure(figsize=(16, 16))
for c in range(len(np.unique(labels))):
i = random.choice(np.where(labels == c)[0])
plt.subplot(10, 10, c + 1)
plt.axis('off')
plt.title('class: {}'.format(c))
plt.imshow(images[i])
preview(train_X, train_Y)
def augment_imgs(imgs, p, imgaug=None):
"""
Performs a set of augmentations with with a probability p
:param imgs: Performs Data Augmentation
:param p: Probability for augmentation
"""
from imgaug import augmenters as iaa
augs = iaa.SomeOf(
(2, 4),
[
# crop images from each side by 0 to 4px (randomly chosen)
iaa.Crop(px=(0, 4)),
iaa.Affine(scale={
"x": (0.8, 1.2),
"y": (0.8, 1.2)
}),
iaa.Affine(translate_percent={
"x": (-0.2, 0.2),
"y": (-0.2, 0.2)
}),
# rotate by -45 to +45 degrees)
iaa.Affine(rotate=(-45, 45)),
# shear by -10 to +10 degrees
iaa.Affine(shear=(-10, 10))
])
seq = iaa.Sequential([iaa.Sometimes(p, augs)])
res = seq.augment_images(imgs)
return res
def augmentation(imgs, lbls):
"""
Data Augmentation for a Label
:param imgs: Images
:param lbls: Labels
"""
classes = count_images_in_classes(lbls)
for i in range(len(classes)):
if classes[i] < MIN_IMGS_IN_CLASS:
# Number of samples to be added
add_num = MIN_IMGS_IN_CLASS - classes[i]
imgs_for_augm = []
lbls_for_augm = []
for j in range(add_num):
im_index = random.choice(np.where(lbls == i)[0])
imgs_for_augm.append(imgs[im_index])
lbls_for_augm.append(lbls[im_index])
augmented_class = augment_imgs(imgs_for_augm, 1)
augmented_class_np = np.array(augmented_class)
augmented_lbls_np = np.array(lbls_for_augm)
imgs = np.concatenate((imgs, augmented_class_np), axis=0)
lbls = np.concatenate((lbls, augmented_lbls_np), axis=0)
return imgs, lbls
train_X, train_Y = augmentation(train_X, train_Y)
print(train_X.shape)
print(train_Y.shape)
augmented_samples_distribution = count_images_in_classes(train_Y)
print(augmented_samples_distribution)
distribution_diagram(augmented_samples_distribution)
preview(train_X, train_Y)
train_X = rgb2gray(train_X)
def build(width, height, depth, classes):
"""
Initialize the model and its dimension
"""
model = keras.Sequential()
inputShape = (height, width, depth)
chanDim = -1
# CONV => RELU => BN => POOL
model.add(Conv2D(8, (5, 5), padding="same", input_shape=inputShape))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
# first set of (CONV => RELU => CONV => RELU) * 2 => POOL
model.add(Conv2D(16, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(Conv2D(16, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
# second set of (CONV => RELU => CONV => RELU) * 2 => POOL
model.add(Conv2D(32, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(Conv2D(32, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(axis=chanDim))
model.add(MaxPooling2D(pool_size=(2, 2)))
# first set of FC => RELU layers
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))
# second set of FC => RELU layers
model.add(Flatten())
model.add(Dense(128))
model.add(Activation("relu"))
model.add(BatchNormalization())
model.add(Dropout(0.5))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
# return the constructed network architecture
return model
model = build(28, 28, 1, 43)
if SET_DECAY:
opt = Adam(lr=INIT_LR, decay=INIT_LR / (EPOCHS * 0.5))
else:
opt = Adam(lr=INIT_LR)
model.compile(loss="sparse_categorical_crossentropy",
optimizer=opt,
metrics=["accuracy"])
test_images = []
test_labels = []
test = '/content/Dataset'
# loop over all 43 classes
gtFile = open('/content/Dataset/Test.csv') # annotations file
gtReader = csv.reader(gtFile, delimiter=',') # csv parser for annotations file
next(gtReader)
# loop over all images in current annotations file
for row in gtReader:
img = cv.imread(PATH + '/' + row[7])
test_images.append(cv.resize(img, (28, 28)))
test_labels.append(row[6])
gtFile.close()
test_X = np.asarray(test_images)
test_X = test_X / 255
test_X = np.asarray(test_X, dtype="float32")
test_X = rgb2gray(test_X)
test_Y = np.asarray(test_labels, dtype="float32")
print(train_X.shape)
train_X_ext = np.expand_dims(train_X, axis=3)
print(train_X_ext.shape)
print(train_Y.shape)
train_Y_ext = np.expand_dims(train_Y, axis=1)
print(train_Y_ext.shape)
with tf.device('/device:GPU:0'):
os.mkdir("models_Xception")
for i in range(EPOCHS):
model.save("models_Xception/model_" + str(i) + ".h5")
H = model.fit(train_X_ext, train_Y, epochs=1, batch_size=BATCH_SIZE)
print(model.summary())
model.save_weights('model_final.h5')