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.
Python
import matplotlib.image as mpimg
import os
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
from keras.utils.np_utils import to_categorical
from tensorflow.keras.utils import image_dataset_from_directory
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Activation, Dropout, Flatten, Dense
from tensorflow.keras.models import Sequential
from keras import layers
from tensorflow import keras
from tensorflow.keras.layers.experimental.preprocessing import Rescaling
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
import numpy as np
from glob import glob
import cv2
import warnings
warnings.filterwarnings('ignore')
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.
Python
# Extracting the compressed dataset.
from zipfile import ZipFile data_path
= '/content/traffic-sign-dataset-classification.zip' with
ZipFile(data_path, 'r') as zip
: zip.extractall()
Data Visualization
Data visualization means the visualization of data to understand the key features of the dataset.
Python
# path to the folder containing our dataset
dataset = '../content/traffic_Data/DATA'
# path of label file
labelfile = pd.read_csv('labels.csv')
Once we load the dataset, now let's visualize some random images from the different folders. For that, we will use plt.imshow() command.
Python
# Visualize some images from the dataset
img = cv2.imread("/content/traffic_Data/DATA/10/010_0011.png")
plt.imshow(img)
Output :
Let's see one more image.
Python
img = cv2.imread("/content/traffic_Data/DATA/23/023_0001.png")
plt.imshow(img)
Output :
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.
Python
Output:
Let's see the last 5 classes from the label file
Python
Output:
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.
Python
import tensorflow as tf
dataset = "/content/DATA"
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
dataset,
validation_split=0.2,
subset='training',
seed=123,
image_size=(224, 224),
batch_size=32
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
dataset,
validation_split=0.2,
subset='validation',
seed=123,
image_size=(224, 224),
batch_size=32
)
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.
Python
class_numbers = train_ds.class_names
class_names = []
for i in class_numbers:
class_names.append(labelfile['Name'][int(i)])
Let's visualize the train dataset and print 25 images from the dataset.
Python
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
for i in range(25):
ax = plt.subplot(5, 5, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
plt.show()
Output:
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.
Python
import tensorflow as tf
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal", input_shape=(224, 224, 3)),
tf.keras.layers.RandomRotation(0.1),
tf.keras.layers.RandomZoom(0.2),
tf.keras.layers.RandomFlip("horizontal_and_vertical") # No 'mode' argument needed
])
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.
Python
model = Sequential()
model.add(data_augmentation)
model.add(Rescaling(1./255))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(Dense(len(labelfile), activation='softmax'))
Let's print the summary of the model.
Python
Output :
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.
Python
keras.utils.plot_model(
model,
show_shapes=True,
show_dtype=True,
show_layer_activations=True
)
Output :
Python
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(),
optimizer='adam',
metrics=['accuracy'])
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.
Python
# Set callback functions to early stop training
mycallbacks = [EarlyStopping(monitor='val_loss', patience=5)]
history = model.fit(train_ds,
validation_data=val_ds,
epochs=50,
callbacks=mycallbacks)
Output:
The above image shows the last epochs of the model.
Model Evaluation
Let’s visualize the training and validation accuracy with each epoch.
Python
# Loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['loss', 'val_loss'], loc='upper right')
# Accuracy
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.legend(['accuracy', 'val_accuracy'], loc='upper right')
Output :
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
Similar Reads
Deep Learning Tutorial Deep Learning is a subset of Artificial Intelligence (AI) that helps machines to learn from large datasets using multi-layered neural networks. It automatically finds patterns and makes predictions and eliminates the need for manual feature extraction. Deep Learning tutorial covers the basics to adv
5 min read
Deep Learning Basics
Introduction to Deep LearningDeep Learning is transforming the way machines understand, learn and interact with complex data. Deep learning mimics neural networks of the human brain, it enables computers to autonomously uncover patterns and make informed decisions from vast amounts of unstructured data. How Deep Learning Works?
7 min read
Artificial intelligence vs Machine Learning vs Deep LearningNowadays many misconceptions are there related to the words machine learning, deep learning, and artificial intelligence (AI), most people think all these things are the same whenever they hear the word AI, they directly relate that word to machine learning or vice versa, well yes, these things are
4 min read
Deep Learning Examples: Practical Applications in Real LifeDeep learning is a branch of artificial intelligence (AI) that uses algorithms inspired by how the human brain works. It helps computers learn from large amounts of data and make smart decisions. Deep learning is behind many technologies we use every day like voice assistants and medical tools.This
3 min read
Challenges in Deep LearningDeep learning, a branch of artificial intelligence, uses neural networks to analyze and learn from large datasets. It powers advancements in image recognition, natural language processing, and autonomous systems. Despite its impressive capabilities, deep learning is not without its challenges. It in
7 min read
Why Deep Learning is ImportantDeep learning has emerged as one of the most transformative technologies of our time, revolutionizing numerous fields from computer vision to natural language processing. Its significance extends far beyond just improving predictive accuracy; it has reshaped entire industries and opened up new possi
5 min read
Neural Networks Basics
What is a Neural Network?Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
12 min read
Types of Neural NetworksNeural networks are computational models that mimic the way biological neural networks in the human brain process information. They consist of layers of neurons that transform the input data into meaningful outputs through a series of mathematical operations. In this article, we are going to explore
7 min read
Layers in Artificial Neural Networks (ANN)In Artificial Neural Networks (ANNs), data flows from the input layer to the output layer through one or more hidden layers. Each layer consists of neurons that receive input, process it, and pass the output to the next layer. The layers work together to extract features, transform data, and make pr
4 min read
Activation functions in Neural NetworksWhile building a neural network, one key decision is selecting the Activation Function for both the hidden layer and the output layer. It is a mathematical function applied to the output of a neuron. It introduces non-linearity into the model, allowing the network to learn and represent complex patt
8 min read
Feedforward Neural NetworkFeedforward Neural Network (FNN) is a type of artificial neural network in which information flows in a single direction i.e from the input layer through hidden layers to the output layer without loops or feedback. It is mainly used for pattern recognition tasks like image and speech classification.
6 min read
Backpropagation in Neural NetworkBack Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Deep Learning Models
Deep Learning Frameworks
TensorFlow TutorialTensorFlow is an open-source machine-learning framework developed by Google. It is written in Python, making it accessible and easy to understand. It is designed to build and train machine learning (ML) and deep learning models. It is highly scalable for both research and production.It supports CPUs
2 min read
Keras TutorialKeras high-level neural networks APIs that provide easy and efficient design and training of deep learning models. It is built on top of powerful frameworks like TensorFlow, making it both highly flexible and accessible. Keras has a simple and user-friendly interface, making it ideal for both beginn
3 min read
PyTorch TutorialPyTorch is an open-source deep learning framework designed to simplify the process of building neural networks and machine learning models. With its dynamic computation graph, PyTorch allows developers to modify the networkâs behavior in real-time, making it an excellent choice for both beginners an
7 min read
Caffe : Deep Learning FrameworkCaffe (Convolutional Architecture for Fast Feature Embedding) is an open-source deep learning framework developed by the Berkeley Vision and Learning Center (BVLC) to assist developers in creating, training, testing, and deploying deep neural networks. It provides a valuable medium for enhancing com
8 min read
Apache MXNet: The Scalable and Flexible Deep Learning FrameworkIn the ever-evolving landscape of artificial intelligence and deep learning, selecting the right framework for building and deploying models is crucial for performance, scalability, and ease of development. Apache MXNet, an open-source deep learning framework, stands out by offering flexibility, sca
6 min read
Theano in PythonTheano is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays efficiently. It is mostly used in building Deep Learning Projects. Theano works way faster on the Graphics Processing Unit (GPU) rather than on the CPU. This article will help you to unde
4 min read
Model Evaluation
Deep Learning Projects