Back to Feed
ProjectsAI

CS50AI - Road sign identifier

Check out the code for this project here

Building a Traffic Sign Classifier with TensorFlow

For this project I built a Python-based deep learning model to automatically classify photographs of road signs. This project was made for Harvard's CS50’s Introduction to Artificial Intelligence with Python. This was a good introduction to Convolutional Neural Networks (CNNs).

2023-12-09-road-sign-identifier credit

Formatting the data

The data we are using for this project is the German Traffic Sign Recognition Benchmark (GTSRB). This data contains thousands of images that include 43 different categories of traffic signs.

The issue with this dataset is that these images are all in various sizes, resolutions and lighting conditions. To fix this I have a load_data function that uses the OpenCV library (cv2) to read every single image and resize them to a uniform 30x30 pixel grid and then puts them in the training arrays. I then also used tf.keras.utils.to_categorical to one-hot encode the labels. This translates the human categories of road signs into a mathematical format the AI can understand.

The Architecture

I then have a get_model function that uses TensorFlow and Keras to build a Convolutional Neural Network. These are the layers that I used in my model:

  • Convolutional Layers (Conv2D): I had two of these layers where each of them apply 32 mathematical filters using a 3x3 kernel across the image. The early layers detect the basic features in the image like straight lines and the deeper layers learn the more complex shapes.

  • Max Pooling Layers (MaxPooling2D): After the features are extracted we need to downsample the image. I do this by adding a 2x2 pooling layer after each convolution. This keeps the maximum value from the small grid of pixels.

  • Flattening and Dense Layers: After this I include Flatten layer that converts the 2D matrix into a 1D array. I then pass this layer into a Dense layer with 128 nodes. Then we have an output layer that has 43 nodes where each one has a softmax function and corresponds to one of the road sign catergories.

Preventing overfitting

I wanted to prevent overfitting in this project so its able to perform well on the test dataset as well as on the training dataset. To be able to do this I used Dropout layer in the training phase. I set the drop out rate to 0.2. What this means is that the neural network randomly turns off 20% of the neurons in every training step. This then forces the remaining neurons to work harder and prevents them relying too heavily on one single path. This forces the model to be more generalised.

By setting the dropout rate to 0.2, the network randomly turns off 20% of its nuerons during every training step. This forces the remaining neurons to work harder and prevents the network from relying too heavily on any single pathway, resulting in a much more robust, generalised model.