-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_model.py
199 lines (136 loc) · 5.63 KB
/
custom_model.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
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 20 13:38:11 2019
@author: Prudhvi
"""
import keras
from keras.models import Sequential
from keras.layers import Dense,Conv2D,Dropout,Flatten,MaxPooling2D
import numpy as np
import matplotlib.pyplot as plt
import pickle
import random
import cv2
import skimage.morphology as morp
from skimage.filters import rank
from sklearn.utils import shuffle
import csv
from sklearn.metrics import confusion_matrix
training_file = 'C://Users/Prudhvi/NN projects/project/traffic-signs-data/train.p'
validation_file= 'C://Users/Prudhvi/NN projects/project/traffic-signs-data/valid.p'
testing_file = 'C://Users/Prudhvi/NN projects/project/traffic-signs-data/test.p'
with open(training_file, mode='rb') as f:
train = pickle.load(f)
with open(validation_file, mode='rb') as f:
valid = pickle.load(f)
with open(testing_file, mode='rb') as f:
test = pickle.load(f)
signs = []
with open('C://Users/Prudhvi/NN projects/project/signnames.csv', 'r') as csvfile:
signnames = csv.reader(csvfile, delimiter=',')
next(signnames,None)
for row in signnames:
signs.append(row[1])
csvfile.close()
X_train, y_train = train['features'], train['labels']
X_valid, y_valid = valid['features'], valid['labels']
X_test, y_test = test['features'], test['labels']
# Number of training examples
n_train = X_train.shape[0]
# Number of testing examples
n_test = X_test.shape[0]
# Number of validation examples.
n_validation = X_valid.shape[0]
# What's the shape of an traffic sign image?
image_shape = X_train[0].shape
# How many unique classes/labels there are in the dataset.
n_classes = len(np.unique(y_train))
print("Number of training examples: ", n_train)
print("Number of testing examples: ", n_test)
print("Number of validation examples: ", n_validation)
print("Image data shape =", image_shape)
print("Number of classes =", n_classes)
def list_images(dataset, dataset_y, ylabel="", cmap=None):
plt.figure(figsize=(15, 16))
for i in range(6):
plt.subplot(1, 6, i+1)
indx = random.randint(0, len(dataset))
#Use gray scale color map if there is only one channel
cmap = 'gray' if len(dataset[indx].shape) == 2 else cmap
plt.imshow(dataset[indx], cmap = cmap)
plt.xlabel(signs[dataset_y[indx]])
plt.ylabel(ylabel)
plt.xticks([])
plt.yticks([])
plt.tight_layout(pad=0, h_pad=0, w_pad=0)
plt.show()
list_images(X_train, y_train, "Training example")
list_images(X_test, y_test, "Testing example")
list_images(X_valid, y_valid, "Validation example")
def histogram_plot(dataset, label):
hist, bins = np.histogram(dataset, bins=n_classes)
width = 0.7 * (bins[1] - bins[0])
center = (bins[:-1] + bins[1:]) / 2
plt.bar(center, hist, align='center', width=width)
plt.xlabel(label)
plt.ylabel("Image count")
plt.show()
histogram_plot(y_train, "Training examples")
histogram_plot(y_test, "Testing examples")
histogram_plot(y_valid, "Validation examples")
X_train, y_train = shuffle(X_train, y_train)
def gray_scale(image):
return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# Sample images after greyscaling
gray_images = list(map(gray_scale, X_train))
list_images(gray_images, y_train, "Gray Scale image", "gray")
def local_histo_equalize(image):
kernel = morp.disk(30)
img_local = rank.equalize(image, selem=kernel)
return img_local
equalized_images = list(map(local_histo_equalize, gray_images))
list_images(equalized_images, y_train, "Equalized Image", "gray")
def image_normalize(image):
image = np.divide(image, 255)
return image
n_training = X_train.shape
normalized_images = np.zeros((n_training[0], n_training[1], n_training[2]))
for i, img in enumerate(equalized_images):
normalized_images[i] = image_normalize(img)
list_images(normalized_images, y_train, "Normalized Image", "gray")
normalized_images = normalized_images[..., None]
def preprocess(data):
gray_images = list(map(gray_scale, data))
equalized_images = list(map(local_histo_equalize, gray_images))
n_training = data.shape
normalized_images = np.zeros((n_training[0], n_training[1], n_training[2]))
for i, img in enumerate(equalized_images):
normalized_images[i] = image_normalize(img)
normalized_images = normalized_images[..., None]
return normalized_images
X_train = X_train.reshape(34799,32,32,3)
X_train=preprocess(X_train)
X_test = X_test.reshape(12630,32,32,3)
X_test=preprocess(X_test)
X_valid=X_valid.reshape(4410,32,32,3)
X_valid=preprocess(X_valid)
y_train = keras.utils.to_categorical(y_train, 43)
y_test = keras.utils.to_categorical(y_test, 43)
y_valid=keras.utils.to_categorical(y_valid, 43)
model =Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=(32,32,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(43, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
model.fit(X_train, y_train,batch_size=128,epochs=30,verbose=1,validation_data=(X_test, y_test))
score = model.evaluate(X_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
predictions = model.predict(X_test)
y_pred = (predictions == 1)
matrix = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))