Source: Deep Learning on Medium
I recently completed my third project where I have to Build and traine a deep neural network to classify traffic signs, using TensorFlow. Experiment with different network architectures. Performe image pre-processing and validation to guard against overfitting.
What I did to Build a Traffic Sign Recognition
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
Data Set Summary & Exploration
I used the numpy library to calculate summary statistics of the traffic signs data set:
The size of training set is 34799 The size of test set is 12630 The shape of a traffic sign image is (32, 32, 3) The number of unique classes/labels in the data set is 43
Here is an exploratory visualization of the data set. It pulls in a random set of 8 images and labels them with the correct names in reference with the csv file to their respective id’s.
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):
if(labels != None):
I also detail the dataset structure by plotting the occurrence of each image class to see how the data is distributed. This can help understand where potential pitfalls could occur if the dataset isn’t uniform in terms of a baseline occurrence.
unique_train, counts_train = np.unique(y_train, return_counts=True)
plt.title("Train Dataset Sign Counts")
Design and Test a Model Architecture
I decided to convert the images to grayscale. I assume this works better because the excess information might add extra confusion into the learning process. After the grayscale I also normalized the image data because I’ve read it helps in speed of training and performance because of things like resources. Also added additional images to the datasets through randomized modifications.
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)
Here is an example of a traffic sign images that were randomly selected.
Here is a look at the normalized images. Which should look identical, but for some small random alterations such as opencv affine and rotation.
I did a few random alterations to the images and saved multiple copies of them depending on the total images in the dataset class type.
Here is an example of 1 image I changed at random. More can be seen further in the document, but the original is on the right and the randomized opencv affine change is on the left. Small rotations are also visible further along as stated.
I increased the train dataset size to 89860 and also merged and then remade another validation dataset. Now no image class in the train set has less then 1000 images.
Validation set gained 20% of the original total mentioned above. I did this using scikit learns train test split method.
To train the model, I used an ….
| Layer. |. Description |
|: — — — — — — — — — — -:|: — — — — — — — — — — — — — — — — — — — — — — -:|
| Input | 32x32x1 RGB image |
| Convolution 3×3 | 2×2 stride, valid padding, outputs 28x28x6 |
| RELU | |
| Max pooling | 2×2 stride, outputs 14x14x64 |
| Convolution 5×5 | 2×2 stride, valid padding output 10x10x6 |
| RELU | |
| Max Pooling | 2×2 stride, outputs 5x5x16 |
| Convolution 1×1 | 2×2 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 |
why you think the architecture is suitable for the current problem.
To train the model, I used an LeNet for the most part that was given, but I did add an additional convolution without a max pooling layer after it like in the udacity lesson. I used the AdamOptimizer with a learning rate of 0.00097. The epochs used was 27 while the batch size was 156. Other important parameters I learned were important was the number and distribution of additional data generated. I played around with various different distributions of image class counts and it had a dramatic effect on the training set accuracy. It didn’t really have much of an effect on the test set accuracy, or real world image accuracy. Even just using the default settings from the Udacity lesson leading up to this point I was able to get 94% accuracy with virtually no changes on the test set. When I finally stopped testing I got 94–95.2% accuracy on the test set though so I think the extra data improved training accuracy, but not a huge help for test set accuracy. Although this did help later on with the images from the internet.
My final model results were:
Training set accuracy of 100.0% validation set accuracy of 99.3% test set accuracy of 95.1% If an iterative approach was chosen:
What were some problems with the initial architecture?
The first issue was lack of data for some images and the last was lack of knowledge of all the parameters. After I fixed those issues the LeNet model given worked pretty well with the defaults. I still couldn’t break 98% very easily until I added another convolution. After that it was much faster at reaching higher accuracy scores.
How was the architecture adjusted and why was it adjusted?
Adding a couple dropouts with a 50% probability.
Which parameters were tuned? How were they adjusted and why?
Epoch, learning rate, batch size, and drop out probability were all parameters tuned along with the number of random modifications to generate more image data was tuned. For Epoch the main reason I tuned this was after I started to get better accuracy early on I lowered the number once I had confidence I could reach my accuracy goals. The batch size I increased only slightly since starting once I increased the dataset size. The learning rate I think could of been left at .001 which is as I am told a normal starting point, but I just wanted to try something different so .00097 was used. I think it mattered little. The dropout probability mattered a lot early on, but after awhile I set it to 50% and just left it. The biggest thing that effected my accuracy was the data images generated with random modifications. This would turn my accuracy from 1–10 epochs from 40% to 60% max to 70% to 90% within the first few evaluations. Increasing the dataset in the correct places really improved the max accuracy as well.
What are some of the important design choices and why were they chosen?
I think the most important thing I learned was having a more uniform dataset along with enough convolutions to capture features will greatly improve speed of training and accuracy.
Test a Model on New Images
Here are five German traffic signs that I found on the web:
I used semi-easy images to classify and even modified them slightly. I made them all uniform in size and only had one partially cut off.
Predict the Sign Type for Each Image
Here are the results of the prediction:
The model was able to correctly guess 5 of the 5 traffic signs, which gives an accuracy of 100%. This compares favorably to the accuracy on the test set although I did throw it a softball.
Please click here to view my project code
I hope this helped you to as base to start your way!