VOOZH about

URL: https://www.geeksforgeeks.org/deep-learning/traffic-signs-recognition-using-cnn-and-keras-in-python/

⇱ Traffic Signs Recognition using CNN and Keras in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Traffic Signs Recognition using CNN and Keras in Python

Last Updated : 23 Jul, 2025

We always come across incidents of accidents where drivers' Overspeed or lack of vision leads to major accidents. In winter, the risk of road accidents has a 40-50% increase because of the traffic signs' lack of visibility. So here in this article, we will be implementing Traffic Sign recognition using a Convolutional Neural Network. It will be very useful in Automatic Driving Vehicles.

Convolutional Neural Networks 

A convolutional Neural Network is a Deep Learning network used to pick up features from the image. Initially, they take the input images and then find out the lines, gradients, shapes and borders from the image. 

Then further, Convolutional Layers help in processing the outputs to capture eyes, faces, etc. CNN contains many convolutional layers assembled on top of each other, each one competent of recognizing more sophisticated shapes. With two or three convolutional layers it is viable to recognize handwritten digits and with 25 layers it is possible to differentiate human faces.

Traffic Signs Recognition using CNN and Keras in Python

Here we will be using this concept for the recognition of traffic signs.

Importing Libraries

  • Pandas – Use to load the data frame in a 2D array format.
  • NumPy – NumPy arrays are very fast and can perform large computations in a very short time.
  • Matplotlib– This library is used to draw visualizations.
  • Sklearn – It contains multiple libraries having pre-implemented functions of model development and evaluation.
  • OpenCV – This library mainly focused on image processing and handling.
  • Tensorflow – It provides a range of functions to achieve complex functionalities with single lines of code.

Loading and Extracting the Dataset

The dataset has 58 classes of Traffic Signs and a label.csv file. The folder is in zip format. To unzip the dataset, we will run the code below.

Data Visualization

Data visualization means the visualization of data to understand the key features of the dataset.

Once we load the dataset, now let's visualize some random images from the different folders. For that, we will use plt.imshow() command.

Output : 

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

Let's see one more image. 

Output : 

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

Label File - This file includes the 58 rows and 2 columns. The columns contains the class id and the name of the symbol. And the rows depicts the 58 different classes id and names.

Now, Let's see the label file by printing the top 5 rows.

Output:

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

Let's see the last 5 classes from the label file

Output:

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

Data Preparation for Training

In this section, we will split the dataset into train and val set. Train set will be used to train the model and val set will be use to evaluate the performance of our model.

Output:

Found 4170 files belonging to 58 classes.
Using 3336 files for training.
Found 4170 files belonging to 58 classes.
Using 834 files for validation.

Once we split the dataset, Let's create the list of the class names and print few images along with their class names.

Let's visualize the train dataset and print 25 images from the dataset.

Output:

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

Data Augmentation

Sometimes the data is limited and we the model is not performing well with limited data. For this method, we use Data Augmentation. It is the method to increase the amount and diversity in data. We do not collect new data, rather we transform the already present data.

Model Architecture

The model will contain the following Layers:

  • Four Convolutional Layers followed by MaxPooling Layers.
  • The Flatten layer to flatten the output of the convolutional layer.
  • Then we will have three fully connected Dense layers followed by the output of the of Softmax activation function.

Let's print the summary of the model.

Output : 

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

To understand the huge number of parameters and complexity of the model which helps us to achieve a high-performance model let's see the plot_model.

Output : 

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

Model Training 

Now we will train our model, the model is working fine on epochs = 50, but you can perform hyperparameter tuning for better results.

We can also use callback function for early stopping the model training.

Output:

πŸ‘ Traffic Signs Recognition using CNN and Keras in Python

The above image shows the last epochs of the model.

Model Evaluation

Let’s visualize the training and validation accuracy with each epoch.

Output : 

πŸ‘ download

CNN model is performing very well with these layers. Further we can include more traffic and danger sign to warn the drivers.

Colab Link: click here

Comment