Cainvas
Model Files
traffic_signal.h5
keras
Model
deepSea Compiled Models
traffic_signal.exe
deepSea
Ubuntu

Traffic Sign Classification

Credit: AITS Cainvas Community

Photo by Rwds on Dribbble

Traffic Sign Classification is an essential part of Autonomous Vehicles so that it can understand the traffic signs properly and take actions accordingly.

Import Dataset

In [1]:
# This will load the dataset.You will see a folder called ALL in your workspace.
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/traffic.zip"
!unzip -qo traffic.zip 
!rm traffic.zip

Importing Libraries

In [2]:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
import os
import time
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from collections import Counter

import cv2
from tensorflow.keras.utils import plot_model
from keras.layers import Dense, Flatten, AveragePooling2D, Dropout
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam

Reading Data of Class Labels

In [3]:
path = 'traffic'
lab = pd.read_csv('traffic/labels.csv')
In [4]:
d = dict()
class_labels = dict()
for dirs in os.listdir(path + '/myData'):
    count = len(os.listdir(path+'/myData/'+dirs))
    d[dirs+' => '+lab[lab.ClassId == int(dirs)].values[0][1]] = count
    class_labels[int(dirs)] = lab[lab.ClassId == int(dirs)].values[0][1]

Reading Image Data

In [5]:
# input image dimensions
img_rows, img_cols = 224, 224
# The images are RGB.
img_channels = 3
nb_classes = len(class_labels.keys())

datagen = ImageDataGenerator()
data = datagen.flow_from_directory('traffic/myData',
                                    target_size=(224, 224),
                                    batch_size=2000,
                                    class_mode='categorical',
                                    shuffle=True )
Found 2000 images belonging to 20 classes.
In [6]:
X , y = data.next()
In [7]:
# Labels are one hot encoded
print(f"Data Shape   :{X.shape}\nLabels shape :{y.shape}")
Data Shape   :(2000, 224, 224, 3)
Labels shape :(2000, 20)

Sample Images of Dataset

In [8]:
fig, axes = plt.subplots(10,10, figsize=(18,18))
for i,ax in enumerate(axes.flat):
    r = np.random.randint(X.shape[0])
    ax.imshow(X[r].astype('uint8'))
    ax.grid(False)
    ax.axis('off')
    ax.set_title('Label: '+str(np.argmax(y[r])))