Build a Traffic Sign Recognition Classifier
# Load pickled data
import pickle
# TODO: Fill this in based on where you saved the training and testing data
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)
with open(testing_file, mode='rb') as f:
test = pickle.load(f)
X_train, y_train = train['features'], train['labels']
X_valid, y_valid = valid['features'], valid['labels']
X_test, y_test = test['features'], test['labels']
Step 1: Dataset Summary & Exploration
The pickled data is a dictionary with 4 key/value pairs:
'features'
is a 4D array containing raw pixel data of the traffic sign images, (num examples, width, height, channels).'labels'
is a 1D array containing the label/class id of the traffic sign. The filesignnames.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. THESE COORDINATES ASSUME THE ORIGINAL IMAGE. THE PICKLED DATA CONTAINS RESIZED VERSIONS (32 by 32) OF THESE IMAGES
Complete the basic data summary below. Use python, numpy and/or pandas methods to calculate the data summary rather than hard coding the results. For example, the pandas shape method might be useful for calculating some of the summary results.
### Replace each question mark with the appropriate value.
### Use python, pandas or numpy methods rather than hard coding the results
import numpy as np
# Number of training examples
n_train = X_train.shape[0]
# Number of testing examples.
n_test = X_test.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))
print("Number of training examples =", n_train)
print("Number of testing examples =", n_test)
print("Image data shape =", image_shape)
print("Number of classes =", n_classes)
Visualize the German Traffic Signs Dataset using the pickled file(s). This is open ended, suggestions include: plotting traffic sign images, plotting the count of each sign, etc.
The Matplotlib examples and gallery pages are a great resource for doing visualizations in Python.
NOTE: It's recommended you start with something simple first. If you wish to do more, come back to it after you've completed the rest of the sections.
### Data exploration visualization code goes here.
### Feel free to use as many code cells as needed.
import matplotlib.pyplot as plt
# Visualizations will be shown in the notebook.
%matplotlib inline
import random
import csv
def plot_figures(figures, nrows = 1, ncols=1, labels=None):
fig, axs = plt.subplots(ncols=ncols, nrows=nrows, figsize=(12, 14))
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()
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')
# print(name_values[y_train[index]][1].decode('ascii'))
figures[i] = X_train[index]
plot_figures(figures, 4, 2, labels)
Personal Note
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)
plt.bar(unique_train, counts_train)
plt.grid()
plt.title("Train Dataset Sign Counts")
plt.show()
unique_test, counts_test = np.unique(y_test, return_counts=True)
plt.bar(unique_test, counts_test)
plt.grid()
plt.title("Test Dataset Sign Counts")
plt.show()
unique_valid, counts_valid = np.unique(y_valid, return_counts=True)
plt.bar(unique_valid, counts_valid)
plt.grid()
plt.title("Valid Dataset Sign Counts")
plt.show()
Personal Note
Data appears uniform although each part of the dataset doesn't have equal sizes. This should be ok though, but something to keep in mind if I run into problems detecting specific signs that should be classified out of the 43.# yuv = np.array([[1, 0, 1.13983], [1, -0.39465, -0.58060], [1, 2.03211, 0]])
# X_train_yuv = X_train*yuv
# from skimage import color
# X_train_yuv = color.convert_colorspace(X_train, 'RGB', 'YUV')
# X_train_yuv = color.rgb2yuv(X_train)
# X_train_yuv = color.rgb2yuv(X_train)
# X_train_y = X_train_yuv[0:,:,]
# number_to_stop = 8
# figures = {}
# for i in range(number_to_stop):
# index = random.randint(0, n_train-1)
# print(name_values[y_train[index]])
# figures[y_train[index]] = X_train_yuv[index]
# plot_figures(figures, 2, 4)
# print(X_train_y)
# X_train = X_train_yuv
Personal Note
I tried to use YUV, but kind of ran out of time. In the paper recommended by the class, from Pierre Sermanet and Yann LeCun, they said they used it. I am still curious exactly how they did it.Step 2: Design and Test a Model Architecture
Design and implement a deep learning model that learns to recognize traffic signs. Train and test your model on the German Traffic Sign Dataset.
There are various aspects to consider when thinking about this problem:
- Neural network architecture
- Play around preprocessing techniques (normalization, rgb to grayscale, etc)
- Number of examples per label (some have more than others).
- Generate fake data.
Here is an example of a published baseline model on this problem. It's not required to be familiar with the approach used in the paper but, it's good practice to try to read papers like these.
NOTE: The LeNet-5 implementation shown in the classroom at the end of the CNN lesson is a solid starting point. You'll have to change the number of classes and possibly the preprocessing, but aside from that it's plug and play!
Use the code cell (or multiple code cells, if necessary) to implement the first step of your project.
### Preprocess the data here. Preprocessing steps could include normalization, converting to grayscale, etc.
### Feel free to use as many code cells as needed.
import tensorflow as tf
from tensorflow.contrib.layers import flatten
from math import ceil
from sklearn.utils import shuffle
# Convert to grayscale
X_train_rgb = X_train
X_train_gray = np.sum(X_train/3, axis=3, keepdims=True)
X_test_rgb = X_test
X_test_gray = np.sum(X_test/3, axis=3, keepdims=True)
X_valid_rgb = X_valid
X_valid_gray = np.sum(X_valid/3, axis=3, keepdims=True)
print(X_train_rgb.shape)
print(X_train_gray.shape)
print(X_test_rgb.shape)
print(X_test_gray.shape)
# X_train = tf.image.rgb_to_grayscale(X_train, name=None)
X_train = X_train_gray
X_test = X_test_gray
X_valid = X_valid_gray
image_depth_channels = X_train.shape[3]
# print(image_depth_channels)
number_to_stop = 8
figures = {}
random_signs = []
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].squeeze()
random_signs.append(index)
# print(random_signs)
plot_figures(figures, 4, 2, labels)
import cv2
more_X_train = []
more_y_train = []
more2_X_train = []
more2_y_train = []
new_counts_train = counts_train
for i in range(n_train):
if(new_counts_train[y_train[i]] < 3000):
for j in range(3):
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])
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])
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)
figures1 = {}
labels = {}
figures1[0] = X_train[n_train+1].squeeze()
labels[0] = y_train[n_train+1]
figures1[1] = X_train[0].squeeze()
labels[1] = y_train[0]
plot_figures(figures1, 1, 2, labels)
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)
print("New Dataset Size : {}".format(X_train.shape[0]))
unique, counts = np.unique(y_train, return_counts=True)
plt.bar(unique, counts)
plt.grid()
plt.title("Train Dataset Sign Counts")
plt.show()
unique, counts = np.unique(y_test, return_counts=True)
plt.bar(unique, counts)
plt.grid()
plt.title("Test Dataset Sign Counts")
plt.show()
unique, counts = np.unique(y_valid, return_counts=True)
plt.bar(unique, counts)
plt.grid()
plt.title("Valid Dataset Sign Counts")
plt.show()
def normalize(im):
return -np.log(1/((1 + im)/257) - 1)
# X_train_normalized = normalize(X_train)
# X_test_normalized = normalize(X_test)
X_train_normalized = X_train/127.5-1
X_test_normalized = X_test/127.5-1
number_to_stop = 8
figures = {}
count = 0
for i in random_signs:
labels[count] = name_values[y_train[i]][1].decode('ascii')
figures[count] = X_train_normalized[i].squeeze()
count += 1;
plot_figures(figures, 4, 2, labels)
X_train = X_train_normalized
X_test = X_test_normalized
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')
print(layer_one.shape)
print()
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')
print(layer_two.shape)
print()
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)
#
If a well known architecture was chosen:
* What architecture was chosen?
* Why did you believe it would be relevant to the traffic sign application?
* How does the final model's accuracy on the training, validation and test set provide evidence that the model is working well?layer_two_a = tf.nn.max_pool(layer_two_a, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
print(layer_two_a.shape)
print()
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)
A validation set can be used to assess how well the model is performing. A low accuracy on the training and validation sets imply underfitting. A high accuracy on the test set but low accuracy on the validation set implies overfitting.
### Train your model here.
### Calculate and report the accuracy on the training and validation set.
### Once a final model architecture is selected,
### the accuracy on the test set should be calculated and reported as well.
### Feel free to use as many code cells as needed.
EPOCHS = 27
BATCH_SIZE = 156
rate = 0.00097
logits = LeNet(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(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:
sess.run(tf.global_variables_initializer())
num_examples = len(X_train)
print("Training...")
print()
validation_accuracy_figure = []
test_accuracy_figure = []
for i in range(EPOCHS):
X_train, y_train = shuffle(X_train, y_train)
for offset in range(0, num_examples, BATCH_SIZE):
end = offset + BATCH_SIZE
batch_x, batch_y = X_train[offset:end], y_train[offset:end]
sess.run(training_operation, feed_dict={x: batch_x, y: batch_y, keep_prob: 0.5})
validation_accuracy = evaluate(X_valid, y_valid)
validation_accuracy_figure.append(validation_accuracy)
test_accuracy = evaluate(X_train, y_train)
test_accuracy_figure.append(test_accuracy)
print("EPOCH {} ...".format(i+1))
print("Test Accuracy = {:.3f}".format(test_accuracy))
print("Validation Accuracy = {:.3f}".format(validation_accuracy))
print()
saver.save(sess, './lenet')
print("Model saved")
plt.plot(validation_accuracy_figure)
plt.title("Test Accuracy")
plt.show()
plt.plot(validation_accuracy_figure)
plt.title("Validation Accuracy")
plt.show()
Display Accuracy on test set
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))
test_accuracy = evaluate(X_test, y_test)
print("Test Accuracy = {:.3f}".format(test_accuracy))
Step 3: Test a Model on New Images
To give yourself more insight into how your model is working, download at least five pictures of German traffic signs from the web and use your model to predict the traffic sign type.
You may find signnames.csv
useful as it contains mappings from the class id (integer) to the actual sign name.
import glob
import cv2
my_images = sorted(glob.glob('./mysigns/*.png'))
my_labels = np.array([1, 22, 35, 15, 37, 18])
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, 3, 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
number_to_stop = 6
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, 3, 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.
### Feel free to use as many code cells as needed.
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# saver = tf.train.import_meta_graph('./lenet.meta')
saver.restore(sess, "./lenet")
my_accuracy = evaluate(my_signs_normalized, my_labels)
print("My Data Set Accuracy = {:.3f}".format(my_accuracy))
### Calculate the accuracy for these 5 new images.
### 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(6):
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 = tf.train.import_meta_graph('./lenet.meta')
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()
For each of the new images, print out the model's softmax probabilities to show the certainty of the model's predictions (limit the output to the top 5 probabilities for each image). tf.nn.top_k
could prove helpful here.
The example below demonstrates how tf.nn.top_k can be used to find the top k predictions for each image.
tf.nn.top_k
will return the values and indices (class ids) of the top k predictions. So if k=3, for each sign, it'll return the 3 largest probabilities (out of a possible 43) and the correspoding class ids.
Take this numpy array as an example. The values in the array represent predictions. The array contains softmax probabilities for five candidate images with six possible classes. tk.nn.top_k
is used to choose the three classes with the highest probability:
# (5, 6) array
a = np.array([[ 0.24879643, 0.07032244, 0.12641572, 0.34763842, 0.07893497,
0.12789202],
[ 0.28086119, 0.27569815, 0.08594638, 0.0178669 , 0.18063401,
0.15899337],
[ 0.26076848, 0.23664738, 0.08020603, 0.07001922, 0.1134371 ,
0.23892179],
[ 0.11943333, 0.29198961, 0.02605103, 0.26234032, 0.1351348 ,
0.16505091],
[ 0.09561176, 0.34396535, 0.0643941 , 0.16240774, 0.24206137,
0.09155967]])
Running it through sess.run(tf.nn.top_k(tf.constant(a), k=3))
produces:
TopKV2(values=array([[ 0.34763842, 0.24879643, 0.12789202],
[ 0.28086119, 0.27569815, 0.18063401],
[ 0.26076848, 0.23892179, 0.23664738],
[ 0.29198961, 0.26234032, 0.16505091],
[ 0.34396535, 0.24206137, 0.16240774]]), indices=array([[3, 0, 5],
[0, 1, 4],
[0, 5, 1],
[1, 3, 5],
[1, 4, 3]], dtype=int32))
Looking just at the first row we get [ 0.34763842, 0.24879643, 0.12789202]
, you can confirm these are the 3 largest probabilities in a
. You'll also notice [3, 0, 5]
are the corresponding indices.
### 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())
# my_saver = tf.train.import_meta_graph('./lenet.meta')
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})
# print(my_top_k)
for i in range(6):
figures = {}
labels = {}
figures[0] = my_signs[i]
labels[0] = "Original"
for j in range(k_size):
# print('Guess {} : ({:.0f}%)'.format(j+1, 100*my_top_k[0][i][j]))
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()
# print()
plot_figures(figures, 1, 6, labels)