Build a Traffic Sign Recognition Project
The goals / steps of this project are the following:
- Load the data set (see below for links to the project data set)
- Explore, summarize and visualize the data set
- Design, train and test a model architecture
- Use the model to make predictions on new images
- Analyze the softmax probabilities of the new images
- Summarize the results with a written report
Here I will consider the rubric points individually and describe how I addressed each point in my implementation.
1. Provide a Writeup / README that includes all the rubric points and how you addressed each one. You can submit your writeup as markdown or pdf. You can use this template as a guide for writing the report. The submission includes the project code.
You're reading it!
1. Provide a basic summary of the data set. In the code, the analysis should be done using python, numpy and/or pandas methods rather than hardcoding results manually.
I used the numpy library to display summary statistics of the traffic signs data set:
- The size of training set is 34799
- The size of the validation set is 4410
- The size of test set is 12630
- The shape of a traffic sign image is 32x32x3
- The number of unique classes/labels in the data set is 43
Here is an exploratory visualization of the data set. Below is a set of 12 randomly selected images from training set.
1. Describe how you preprocessed the image data. What techniques were chosen and why did you choose these techniques? Consider including images showing the output of each preprocessing technique. Pre-processing refers to techniques such as converting to grayscale, normalization, etc. (OPTIONAL: As described in the "Stand Out Suggestions" part of the rubric, if you generated additional data for training, describe why you decided to generate additional data, how you generated the data, and provide example images of the additional data. Then describe the characteristics of the augmented training set like number of images in the set, number of images for each class, etc.)
As a first step, I decided to convert the images to grayscale because I believe this helps to reduce the training time significantly.
X_train_gray = np.sum(X_train/3, axis=3, keepdims=True)
X_valid_gray = np.sum(X_valid/3, axis=3, keepdims=True)
X_test_gray = np.sum(X_test/3, axis=3, keepdims=True)
Here is an example of a traffic sign image before and after grayscaling.
As a last step, I normalized the image data because it was suggested in the class lessons. Having a wider distribution of training data would make the learning difficult.
I used the simple method described in the project instructions. This will reduce the distribution of pixel data from 0-255 to -1 to +1.
Following snippet of code does the normalization.
X_training_normalized = (X_train - 128)/128
X_valid_normalized = (X_valid - 128)/128
X_test_normalized = (X_test - 128)/128
There are 43 classes of data. I have plotted the distribution of training, validation and test data sets below.
As you can see from the training data distribution, there are fewer training data set for certain classes of labels than others. I believe this can create bias in learning process. To overcome this limitation, I have decided to generate fake data as suggested in the project instructions.
If the number of training samples for a particular class is less than 1000, fake data is generated by applying one of the following image transforms on a given sample image and added to training data samples.
- Rotate by a random angle of -15 to +15 degrees (
rotate_image()). - Scale the image (
scale_image()) - Translate the image by +/-2 pixels (
translate_image())
This step ensures that there are atleast 1000 training samples for each class. This was a very time consuming to run and generate the fake images.
Following is the training data distribution after augmenting the traing set with fake data.
Here is an example of an original image and an augmented image:
No fake data generated for Validation and test data sets.
2. Describe what your final model architecture looks like including model type, layers, layer sizes, connectivity, etc.) Consider including a diagram and/or table describing the final model.
My final model is implemented in function LeNet(). I started this model based on the model we implemented in the lesson.
My final model consisted of the following layers:
| Layer | Description |
|---|---|
| Input | 32x32x1 Gray scale image |
| Convolution 5x5 | 1x1 stride, valid padding, outputs 28x28x6 |
| RELU | |
| Dropout | |
| Max pooling | 2x2 stride, outputs 14x14x6 |
| Convolution 5x5 | 1x1 stride, valid padding, outputs 10x10x16 |
| RELU | |
| Max pooling | 2x2 stride, outputs 5x5x16 |
| Flatten | output 5x5x1 = 400 |
| Fully connected | input=400, output=120 |
| RELU | |
| Dropout | Keep Probability 65% |
| Fully connected | input=120, output=84 |
| RELU | |
| Dropout | Keep Probability 65% |
| Fully connected | input=84, output=43 |
3. Describe how you trained your model. The discussion can include the type of optimizer, the batch size, number of epochs and any hyperparameters such as learning rate.
I started the model implementation based on the LeNet model we implemented in the lesson. I added dropout layers at the end of the first two fully connected layers.
I used a learning rate of 0.0009, batch size of 128 and EPOCH of 20. I used a EPOCH of 10 initially, then found that it helps to improve the accuracy with increased EPOCHs, but it quickly diminishes after a certain point. I found EPOCH=20 is optimal for this model.
I used AdamOptimizer from TensorFlow as the Optimier.
With this model and hyperparameters, I was able to achieve validation set accuracy of about 94% and test set accuracy of 92.7%.
4. Describe the approach taken for finding a solution and getting the validation set accuracy to be at least 0.93. Include in the discussion the results on the training, validation and test sets and where in the code these were calculated. Your approach may have been an iterative process, in which case, outline the steps you took to get to the final solution and why you chose those steps. Perhaps your solution involved an already well known implementation or architecture. In this case, discuss why you think the architecture is suitable for the current problem.
My final model results were:
- training set accuracy of 99.3%
- validation set accuracy of 94.2%
- test set accuracy of 92.7%
The above metrics where calculated by following code snippets.
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, "./lenet2")
training_accuracy = evaluate(X_train, y_train)
validate_accuracy = evaluate(X_valid, y_valid)
test_accuracy = evaluate(X_test, y_test)
print("Training Set Accuracy = {:.3f}".format(training_accuracy))
print("Validation Set Accuracy = {:.3f}".format(validate_accuracy))
print("Test Set Accuracy = {:.3f}".format(test_accuracy))
I started with the default training data set and LeNet() implenetation in the Udacity lesson. With this model, I was getting a validation set accuracy of about 89%.
First thing I did was to augment the training data such that all the Traffic Sign classes have atleast 1000 samples. I observed that the training set accuracy was converging to 100 pretty quickly but the validation set accuracy wasn't improving. This indicated some kind of "over fitting" of the data. Next thing I did was to add dropout and it helped to reduce the over fitting. I then played around with the learning rate and arrived at a rate of 0.0009.
I also tried to implement the model based on the architecture described in paper Traffic Sign Recognition with Multi-Scale Convolutional Networks
. This is implemented in function LeNetPlus(). However, for some reason, I was not able to improve the accuracy of validation set. The architecture suggested in the paper should produce much higher accuracy. I ran out of time and couldn't debug the LeNetPlus() implementation. So I decided to switch back to LeNet() implementation.
Final hyper parameter selection:
- Batch Size = 128
- EPOCH = 20
- Learning Rate : 0.0009
- Dropout Keep probability : 65%
1. Choose five German traffic signs found on the web and provide them in the report. For each image, discuss what quality or qualities might be difficult to classify.
Here are five German traffic signs that I found on the web:
I believe the traffic sign images I got from the web was very clear and probably not very challenging.
2. Discuss the model's predictions on these new traffic signs and compare the results to predicting on the test set. At a minimum, discuss what the predictions were, the accuracy on these new predictions, and compare the accuracy to the accuracy on the test set (OPTIONAL: Discuss the results in more detail as described in the "Stand Out Suggestions" part of the rubric).
Here are the results of the prediction:
| Image | Prediction |
|---|---|
| Road work | Road work |
| 60 Km/h | 60 Km/h |
| 30 Km/h | 30 Km/h |
| Vehicles over 3.5 metric tons prohibited | Vehicles over 3.5 metric tons prohibited |
| Turn left ahead | Turn left ahead |
The model was able to correctly guess 5 out of 5 traffic signs, which gives an accuracy of 100%.
3. Describe how certain the model is when predicting on each of the five new images by looking at the softmax probabilities for each prediction. Provide the top 5 softmax probabilities for each image along with the sign type of each probability. (OPTIONAL: as described in the "Stand Out Suggestions" part of the rubric, visualizations can also be provided such as bar charts)
I have implemented code that prints top five softmax probablities. Following is a visualization of top five softmax probablitites predicted for 5 traffic sign images downloaded from web.












