-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.py
478 lines (329 loc) · 14.3 KB
/
testing.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# coding: utf-8
# # Self-Driving Car
#
########################DISABLE TENSORFLOW WARNING###############
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#################################################################
# ## CNN Based Traffic Sign Recognition Classifier
#########################DATABASE################################
import MySQLdb as my
db = my.connect("127.0.0.1","root","","pythondb")
cursor = db.cursor()
sql = "UPDATE `task` SET value = 0 WHERE slno=1;"
cursor.execute(sql)
db.commit()
cursor = db.cursor()
sql = "select value from `test` where slno=1;"
cursor.execute(sql)
result= cursor.fetchall()
for row in result:
number_to_stop = row[0]
#print(number_to_stop)
#################################################################
# Load pickled data
import pickle
training_file = "train.p"
validation_file= "valid.p"
testing_file = "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)
#number_to_stop=13
X_train, y_train = train['features'], train['labels']
X_valid, y_valid = valid['features'], valid['labels']
# ---
#
# ## Dataset Summary & Exploration
#
# The pickled data is a dictionary with 4 key/value pairs:
#
# - `'features'` is array containing raw pixel data of the traffic sign images
# - `'labels'` is a 1D array containing the label/class id of the traffic sign.The file `signnames.csv` contains id -> name mappings for each id.
# - `'sizes'` is a list containing tuples, (width, height) representing the the original width and height the image.
# - `'coords'` is a list containing tuples, (x1, y1, x2, y2) representing coordinates of a bounding box around the sign in the image. **THE PICKLED DATA CONTAINS RESIZED VERSIONS (32 by 32) OF THESE IMAGES**
# ### About Data
# In[2]:
import numpy as np
# Number of training examples
n_train = X_train.shape[0]
# What's the shape of an traffic sign image?
image_shape = X_train.shape[1:]
# How many unique classes/labels there are in the dataset.
n_classes = len(np.unique(y_train))
# ### Visualization of the dataset
# In[3]:
import matplotlib.pyplot as plt
import random
import csv
z=50
def plot_figures(figures, nrows = 1, ncols=1, labels=None):
fig, axs = plt.subplots(ncols=ncols, nrows=nrows, figsize=(12, 2))
axs = axs.ravel()
for index, title in zip(range(len(figures)), figures):
axs[index].imshow(figures[title], plt.gray())
if(labels != None):
axs[index].set_title(labels[index])
else:
axs[index].set_title(title)
axs[index].set_axis_off()
plt.tight_layout()
global z
kurs = "images/ratio/%i.png" % z
z=z+1
plt.savefig(kurs, format='png')
name_values = np.genfromtxt('signnames.csv', skip_header=1, dtype=[('myint','i8'), ('mysring','S55')], delimiter=',')
#number_to_stop = 8
figures = {}
labels = {}
for i in range(number_to_stop):
index = random.randint(0, n_train-1)
labels[i] = name_values[y_train[index]][1].decode('ascii')
figures[i] = X_train[index]
#plot_figures(figures, 4, 2, labels)
# <h1>Personal Note</h1>
# Data appears good although occasionally for some reason the image cannot be displayed properly. Maybe bad images in the dataset?
unique_train, counts_train = np.unique(y_train, return_counts=True)
# ## Step 2: Design and Test a Model Architecture
#
# There are various aspects to consider when thinking about this problem:
#
# - Neural network architecture
# - Generate fake data.
# ### Augumentation and greyscale the image
### Preprocess the data here. Preprocessing steps could include normalization, converting to grayscale, etc.
import tensorflow as tf
from tensorflow.contrib.layers import flatten
from math import ceil
from sklearn.utils import shuffle
# Convert to grayscale
# Convert training data to greyscale
X_train_rgb = X_train
X_train_gray = np.sum(X_train/3, axis=3, keepdims=True)
# Convert validation data to greyscale
X_valid_rgb = X_valid
X_valid_gray = np.sum(X_valid/3, axis=3, keepdims=True)
# Store the Greyscale images as the training, testing and validation data
X_train = X_train_gray
X_valid = X_valid_gray
# Test the data availabe so that we can see that data had been greyscaled
image_depth_channels = X_train.shape[3]
# ## Augumentation (Make Duplicate data)
import cv2
more_X_train = []
more_y_train = []
more2_X_train = []
more2_y_train = []
new_counts_train = counts_train
#print(new_counts_train)
for i in range(n_train):
if(new_counts_train[y_train[i]] < 3000):
for j in range(3):
# cv2.warpAffine crops the input image
dx, dy = np.random.randint(-1.7, 1.8, 2)
M = np.float32([[1, 0, dx], [0, 1, dy]])
dst = cv2.warpAffine(X_train[i], M, (X_train[i].shape[0], X_train[i].shape[1]))
dst = dst[:,:,None]
more_X_train.append(dst)
more_y_train.append(y_train[i])
#cv2.getPerspectiveTransform ,transforms and saves
random_higher_bound = random.randint(27, 32)
random_lower_bound = random.randint(0, 5)
points_one = np.float32([[0,0],[32,0],[0,32],[32,32]])
points_two = np.float32([[0, 0], [random_higher_bound, random_lower_bound], [random_lower_bound, 32],[32, random_higher_bound]])
M = cv2.getPerspectiveTransform(points_one, points_two)
dst = cv2.warpPerspective(X_train[i], M, (32,32))
more2_X_train.append(dst)
more2_y_train.append(y_train[i])
#cv2.getRotationMatrix2D rotates the image
tilt = random.randint(-12, 12)
M = cv2.getRotationMatrix2D((X_train[i].shape[0]/2, X_train[i].shape[1]/2), tilt, 1)
dst = cv2.warpAffine(X_train[i], M, (X_train[i].shape[0], X_train[i].shape[1]))
more2_X_train.append(dst)
more2_y_train.append(y_train[i])
new_counts_train[y_train[i]] += 2
more_X_train = np.array(more_X_train)
more_y_train = np.array(more_y_train)
X_train = np.concatenate((X_train, more_X_train), axis=0)
y_train = np.concatenate((y_train, more_y_train), axis=0)
more2_X_train = np.array(more_X_train)
more2_y_train = np.array(more_y_train)
more2_X_train = np.reshape(more2_X_train, (np.shape(more2_X_train)[0], 32, 32, 1))
X_train = np.concatenate((X_train, more2_X_train), axis=0)
y_train = np.concatenate((y_train, more2_y_train), axis=0)
X_train = np.concatenate((X_train, X_valid), axis=0)
y_train = np.concatenate((y_train, y_valid), axis=0)
from sklearn.model_selection import train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
def normalize(im):
return -np.log(1/((1 + im)/257) - 1)
X_train_normalized = X_train/127.5-1
# Get normalized dataset
X_train = X_train_normalized
# ### Model Architecture
#
# My final model consisted of the following layers:
#
# | Layer | Description |
# |:---------------------:|:---------------------------------------------:|
# | Input | 32x32x1 grayscale image |
# | Convolution 5x5 | 2x2 stride, valid padding, outputs 28x28x6 |
# | RELU | |
# | Max pooling | 2x2 stride, outputs 14x14x6 |
# | Convolution 5x5 | 2x2 stride, valid padding, outputs 10x10x16 |
# | RELU | |
# | Max pooling | 2x2 stride, outputs 5x5x16 |
# | Convolution 1x1 | 2x2 stride, valid padding, outputs 1x1x412 |
# | RELU | |
# | Fully connected | input 412, output 122 |
# | RELU | |
# | Dropout | 50% keep |
# | Fully connected | input 122, output 84 |
# | RELU | |
# | Dropout | 50% keep |
# | Fully connected | input 84, output 43 |
#
#
#define basic property of a layer
def conv2d(x, W, b, strides=1):
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='VALID')
x = tf.nn.bias_add(x, b)
#print(x.shape)
return tf.nn.relu(x)
def LeNet(x):
mu = 0
sigma = 0.1
W_one = tf.Variable(tf.truncated_normal(shape=(5, 5, image_depth_channels, 6), mean = mu, stddev = sigma))
b_one = tf.Variable(tf.zeros(6))
layer_one = conv2d(x, W_one, b_one, 1)
layer_one = tf.nn.max_pool(layer_one, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
W_two = tf.Variable(tf.truncated_normal(shape=(5, 5, 6, 16), mean = mu, stddev = sigma))
b_two = tf.Variable(tf.zeros(16))
layer_two = conv2d(layer_one, W_two, b_two, 1)
layer_two = tf.nn.max_pool(layer_two, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
W_two_a = tf.Variable(tf.truncated_normal(shape=(5, 5, 16, 412), mean = mu, stddev = sigma))
b_two_a = tf.Variable(tf.zeros(412))
layer_two_a = conv2d(layer_two, W_two_a, b_two_a, 1)
flat = flatten(layer_two_a)
W_three = tf.Variable(tf.truncated_normal(shape=(412, 122), mean = mu, stddev = sigma))
b_three = tf.Variable(tf.zeros(122))
layer_three = tf.nn.relu(tf.nn.bias_add(tf.matmul(flat, W_three), b_three))
layer_three = tf.nn.dropout(layer_three, keep_prob)
W_four = tf.Variable(tf.truncated_normal(shape=(122, 84), mean = mu, stddev = sigma))
b_four = tf.Variable(tf.zeros(84))
layer_four = tf.nn.relu(tf.nn.bias_add(tf.matmul(layer_three, W_four), b_four))
layer_four = tf.nn.dropout(layer_four, keep_prob)
W_five = tf.Variable(tf.truncated_normal(shape=(84, 43), mean = mu, stddev = sigma))
b_five = tf.Variable(tf.zeros(43))
layer_five = tf.nn.bias_add(tf.matmul(layer_four, W_five), b_five)
return layer_five
x = tf.placeholder(tf.float32, (None, 32, 32, image_depth_channels))
y = tf.placeholder(tf.int32, (None))
one_hot_y = tf.one_hot(y, 43)
keep_prob = tf.placeholder(tf.float32)
# ### Train, Validate and Test the Model
### Train your model here.
EPOCHS = 40
BATCH_SIZE = 256
train=0
rate = 0.00097
##CALL CNN##
logits = LeNet(x)
############
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=one_hot_y)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()
def evaluate(X_data, y_data):
num_examples = len(X_data)
total_accuracy = 0
sess = tf.get_default_session()
for offset in range(0, num_examples, BATCH_SIZE):
batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE]
accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y, keep_prob: 1.0})
total_accuracy += (accuracy * len(batch_x))
return total_accuracy / num_examples
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('.'))
train_accuracy = evaluate(X_train, y_train)
print("Train Accuracy = {:.3f}".format(train_accuracy))
valid_accuracy = evaluate(X_valid, y_valid)
print("Valid Accuracy = {:.3f}".format(valid_accuracy))
# # Testing
# ### Load and Output the Images
import glob
import cv2
my_images = sorted(glob.glob('./mysigns/*.png'))
my_labels = np.array([1, 22, 35, 15, 37, 18,40,17,21,38,27,24,0])
figures = {}
labels = {}
my_signs = []
index = 0
for my_image in my_images:
img = cv2.cvtColor(cv2.imread(my_image), cv2.COLOR_BGR2RGB)
my_signs.append(img)
figures[index] = img
labels[index] = name_values[my_labels[index]][1].decode('ascii')
index += 1
#plot_figures(figures, 5, 2, labels)
my_signs = np.array(my_signs)
my_signs_gray = np.sum(my_signs/3, axis=3, keepdims=True)
my_signs_normalized = my_signs_gray/127.5-1
figures = {}
labels = {}
for i in range(number_to_stop):
labels[i] = name_values[my_labels[i]][1].decode('ascii')
figures[i] = my_signs_gray[i].squeeze()
#plot_figures(figures,int(number_to_stop/2), 2, labels)
### Run the predictions here and use the model to output the prediction for each image.
### Make sure to pre-process the images with the same pre-processing pipeline used earlier.
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess, "./lenet")
my_accuracy = evaluate(my_signs_normalized, my_labels)
print("My Data Set Accuracy = {:.3f}".format(my_accuracy))
#### Analyze Performance
### For example, if the model predicted 1 out of 5 signs correctly, it's 20% accurate on these new images.
my_single_item_array = []
my_single_item_label_array = []
for i in range(number_to_stop):
my_single_item_array.append(my_signs_normalized[i])
my_single_item_label_array.append(my_labels[i])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess, "./lenet")
my_accuracy = evaluate(my_single_item_array, my_single_item_label_array)
print('Image {}'.format(i+1))
print("Image Accuracy = {:.3f}".format(my_accuracy))
print()
# ### Output Top 5 Softmax Probabilities For Each Image Found on the Web
# In[22]:
### Print out the top five softmax probabilities for the predictions on the German traffic sign images found on the web.
### Feel free to use as many code cells as needed.
k_size = 5
softmax_logits = tf.nn.softmax(logits)
top_k = tf.nn.top_k(softmax_logits, k=k_size)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver.restore(sess, "./lenet")
my_softmax_logits = sess.run(softmax_logits, feed_dict={x: my_signs_normalized, keep_prob: 1.0})
my_top_k = sess.run(top_k, feed_dict={x: my_signs_normalized, keep_prob: 1.0})
for i in range(number_to_stop):
figures = {}
labels = {}
figures[0] = my_signs[i]
labels[0] = "Original"
for j in range(k_size):
labels[j+1] = 'Guess {} : ({:.0f}%)'.format(j+1, 100*my_top_k[0][i][j])
figures[j+1] = X_valid[np.argwhere(y_valid == my_top_k[1][i][j])[0]].squeeze()
plot_figures(figures, 1, 6, labels)
#########################DATABASE##############################
sql = "UPDATE `task` SET value = 1 WHERE slno=1;"
number_of_rows = cursor.execute(sql)
db.commit()
db.close()
#########################End-DATABASE##########################