forked from suletunahan2/Traffic_Sign_Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
427 lines (300 loc) · 12.8 KB
/
main.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import pickle
from google.colab import drive
drive.mount('/content/drive') # Mounted at / content / drive
# Step 1: Load The Data
training_file = '/content/drive/My Drive/Colab Notebooks/CNN/traffic-signs-data2.zip (Unzipped Files)/train.p'
testing_file = '/content/drive/My Drive/Colab Notebooks/CNN/traffic-signs-data2.zip (Unzipped Files)/test.p'
validation_file= '/content/drive/My Drive/Colab Notebooks/CNN/traffic-signs-data2.zip (Unzipped Files)/valid.p'
with open(training_file,mode='rb') as file:
train = pickle.load(file)
with open(validation_file, mode='rb') as file:
valid = pickle.load(file)
with open(testing_file, mode='rb') as file:
test = pickle.load(file)
print(type(train), type(test)) # Burda verilerin dictionary oldugunu görüyoruz.
import csv
signs=[]
signnames='/content/drive/My Drive/Colab Notebooks/CNN/signnames.csv'
# Comma Separated Values (CSV) – Virgülle Ayrılmış Değerler dosyası, bir veri listesi içeren düz metin dosyasıdır.
with open(signnames, 'r') as csvfile:
signnames = csv.reader(csvfile, delimiter=',')
next(signnames,None) # signnamesi listeye atma
for row in signnames:
signs.append(row[1])
csvfile.close()
print(signs)
#Step 2: Dataset Summary & Exploration
# print(train)
''' Eger dataya bakacak olursak :
data bir dictionary dir ve 4 key-value çiftinden olusur. Bunlar:
'features' , 'labels' , 'sizes ' , 'coords' dir.
'features' : görüntülerin pixel verilerini içeren 4 boyutlu dizidir.(num examples,width,height,channels)
'labels' : trafik isaretlerinin class\label yani etiket id sini iceren 1 boyutlu dizidir. signnames.csv dosyası her id icin ad eşlemesi yapıyor.
'sizes ' : görüntünün orjinal genişlik ve yüksekilgini içeren tuple
'coords' : koordinatlarını temsil eden tuples (x1, y1, x2, y2) içeren bir listedir.
'''
import numpy as np
# İlk basta x ve y ye feature ve label datalarını atıyoruz.
x_train=train['features']
y_train = train['labels']
X_valid, y_valid = valid['features'], valid['labels']
x_test=test['features']
y_test =test['labels']
# print(y_test) #output is label like [16 1 38 ... 6 7 10]
# .shape →Numpy array nesnesinin kaç satır ve sütundan oluştuğunu gösteren bir tupple nesnesi döndürür.
# Number of training examples
print(f"Number of examples for training: {x_train.shape[0]}")
# Number of validation examples.
print("Number of validation examples: ", X_valid.shape[0])
# Number of testing examples
print(f"Number of examples for test : {x_test.shape[0]}")
print(f"Image data shape : {x_train[0].shape}")
n_classes = len(np.unique(y_train))
print(f"Number of classes : {n_classes}")
print(X_train.shape,X_test.shape,y_train.shape,y_test.shape)#y'nin boyutu düzeltilmeli
# matplotlib
import matplotlib.pyplot as plt
import random
def image_list(dataset_x,dataset_y, ylabel="",cmap=None):
'''Bir döngü sayesinde datasetten rastgele üretilen bir sayiya gore verileri görselleştirir.
Bunu yapmak icin matplotlib kütüphanesini kullanır.
Parametreler :(dataset_x : x_train, dataset_y : y_train, ylabel : y eksenin ismi ,cmap : color map (renk özellestirme icin))
Matplotlib işlevi imshow (), 2 boyutlu bir numpy dizisinden bir görüntü oluşturur. Resimde dizinin her bir öğesi için bir kare olacaktır. Her karenin rengi, karşılık gelen dizi öğesinin değeri ve imshow () tarafından kullanılan renk haritasına göre belirlenir.
'''
plt.figure(figsize=(15,16)) # 15 inç genişlikte ve 16 inç yükseklikte bir şekil yaratır.
for i in range(5):
plt.subplot(1, 5, i+1) # subplot
indx = random.randint(0, len(dataset_x)) # 0 ile datasaet boyutu arası rastgele int sayi üret.
cmap = 'gray' if len(dataset_x[indx].shape) == 2 else cmap # cmap: color map , used to specify colors
plt.imshow(dataset_x[indx], cmap = cmap) # (numpy array generating the image , cmap)
plt.xlabel(signs[dataset_y[indx]]) # xlabel : sign listemizden gelir.
plt.ylabel(ylabel)
# Dikey ve yatay kılavuz çizgilerinin yerlerini elle belirlemek için sırayla xticks ve yticks kullanılır.
# Bunlar argüman olarak birer sayı dizisi alırlar.
plt.xticks([])
plt.yticks([])
# plt.tight_layout : alt grafik parametrelerini şekil alanına sığacak şekilde otomatik olarak alt grafik parametrelerini ayarlar.
plt.tight_layout()
plt.show() # displays the plot
# Plotting sample examples
image_list(x_train, y_train, "Training example")
image_list(x_test, y_test, "Testing example")
image_list(X_valid, y_valid, "Validation example")
X_train_one_label=X_train[np.where(y_train==0)]#tek bir id için
image_list(X_train_one_label, y_train, "One label example")
#histogram 1
import seaborn as sns
plt.figure(figsize=(10,7))
g = sns.countplot(y_train, palette="icefire")
plt.title("Number of classes")
#histogram 2
def histogram(dataset,label):
''' gelen datayı histogram seklinde görsellestirmeye yarar.
Parametreleri : (dataset : gelen data , label : x etiketi )
kullanım : plt.hist(x, bins = number of bins) ya da
numpy.histogram(a:input data, bins=10 : )
'''
# len(np.unique(y_train)) : number of classes
hist,bins=np.histogram(dataset, bins=len(np.unique(y_train)))
# plt.bar işlevi konumların ve değerlerin bir listesini alır
# x için etiketler plt.xticks () tarafından sağlanır.
#center = [x for x, _ in enumerate(datset)]
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.title("Histogram")
plt.xlabel(label)
plt.ylabel("Resim Sayisi")
plt.show()
# Plotting histograms of the count of each sign
histogram(y_train, "Training examples")
histogram(y_test, "Testing examples")
histogram(y_valid, "Validation examples")
# Step 3: Data Preprocessing
''' Veri önişleme için yapilacak islemler:
Shuffling , Grayscaling , Local Histogram Equalization , Normalization.
'''
from sklearn.utils import shuffle # veri karistirma icin
'''Grayscaling(Gri Tonlama)'''
import cv2 #opencv
def gray_scale(img):
return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
'''image_gray=list(map(gray_scale,X_train))'''
# Plotting Gray Scale Image sample examples
# image_list(image_gray,y_train,"Gray Scale Image","gray")
'''Local Histogram Equalization (Lokal Histogram Eşitleme)
'''
import skimage.morphology as morp # skimage :scikit-image for Local histogram equalization
from skimage.filters import rank
def local_histogram_equ(img):
# Local Equalization, disk shape kernel
# Better contrast with disk kernel but could be different
kernel=morp.disk(30)
img_local = rank.equalize(img, selem=kernel)
return img_local
'''
# Sample images after Local Histogram Equalization
loc_equalized_images = list(map(local_histogram_equ, image_gray))
# Plotting Sample images after Local Histogram Equalization
image_list(loc_equalized_images, y_train, "Equalized Image", "gray")'''
'''Normalization ( Normallestirme ) : '''
def img_normalization(img): # [0, 1] scale.
img=np.divide(img,255)
return img
# Sample images after normalization
'''
n = X_train.shape
normalized_images = np.zeros((n[0], n[1], n[2]))
for i, img in enumerate(loc_equalized_images):
normalized_images[i] = img_normalization(img)
# Plotting Sample images after normalization
image_list(normalized_images, y_train, "Normalized Image", "gray")
normalized_images = normalized_images[..., None]'''
def preprocessing(data):
''' preprocessing function :
Shuffling , Grayscaling , Local Histogram Equalization , Normalization.
'''
image_gray=list(map(gray_scale,X_train))
loc_equalized_images = list(map(local_histogram_equ, image_gray))
n = data.shape
normalized_images = np.zeros((n[0], n[1], n[2]))
for i, img in enumerate(loc_equalized_images):
normalized_images[i] = img_normalization(img)
# image_list(normalized_images, y_train, "Normalized Image", "gray")
normalized_images = normalized_images[..., None]
return normalized_images
X_train_preprocessed=preprocessing(X_train)
X_valid_preprocessed=preprocessing(X_valid)
X_train_preprocessed_dn = X_train_preprocessed.reshape(len(X_train_preprocessed), 32*32*1).astype('float32')
X_valid_preprocessed_dn = X_valid_preprocessed.reshape(len(X_valid_preprocessed), 32*32*1).astype('float32')
import keras
from keras.utils import to_categorical
y_train_final_dn = keras.utils.to_categorical(y_train, n_classes)
y_valid_final_dn = keras.utils.to_categorical(y_valid, n_classes)
print(X_train_preprocessed_dn.shape)
print(X_valid_preprocessed_dn.shape)
print(y_train_final_dn.shape)
print(y_valid_final_dn.shape)
#Design Model
# import the necessary packages
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
from keras.callbacks import ModelCheckpoint
#Building the model
class TrafficSignNet:
@staticmethod
def build(width, height, depth, classes):
# initialize the model along with the input shape to be
# "channels last" and the channels dimension itself
model = 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
#data augmentation
# construct the image generator for data augmentation
from keras.preprocessing.image import ImageDataGenerator
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")
# initialize the number of epochs to train for, base learning rate,
# and batch size
NUM_EPOCHS = 30
INIT_LR = 1e-3
BS = 64
print("[INFO] compiling model...")
opt = tf.keras.optimizers.Adam(lr=INIT_LR, decay=INIT_LR / (NUM_EPOCHS * 0.5))
model = TrafficSignNet.build(width=32, height=32, depth=3,
classes=n_classes)
model.compile(loss="categorical_crossentropy", optimizer='adam',
metrics=["accuracy"])
# train the network
print("[INFO] training network...")
H = model.fit_generator(aug.flow(X_train,y_train_final_dn,batch_size=64),validation_data=(X_valid, y_valid_final_dn),
epochs=NUM_EPOCHS)
#plotting graphs for accuracy
plt.figure(0)
plt.plot(H.history['accuracy'], label='training accuracy')
plt.plot(H.history['val_accuracy'], label='val accuracy')
plt.title('Accuracy')
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.legend()
plt.show()
plt.figure(1)
plt.plot(H.history['loss'], label='training loss')
plt.plot(H.history['val_loss'], label='val loss')
plt.title('Loss')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend()
plt.show()
# plot the training loss and accuracy
N = np.arange(0, NUM_EPOCHS)
plt.style.use("ggplot")
plt.figure()
plt.plot(N, H.history["loss"], label="train_loss")
plt.plot(N, H.history["val_loss"], label="val_loss")
plt.plot(N, H.history["accuracy"], label="train_acc")
plt.plot(N, H.history["val_accuracy"], label="val_acc")
plt.title("Training Loss and Accuracy on Dataset")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.show()
#testing accuracy on test dataset
from sklearn.metrics import accuracy_score
pred=model.predict_classes(X_test)
#Accuracy with the test data
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,pred))