<jupyter_start><jupyter_text>Loading the desired libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import pickle
from keras.models import Sequential
from keras.layers import Flatten ,Dropout,Dense,Activation
from keras.optimizers import Adam
from keras.utils import np_utils
from keras.layers import Conv2D,GlobalAveragePooling2D,MaxPooling2D,ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator<jupyter_output>Using TensorFlow backend.<jupyter_text>Loading the dataThe data is stored in .p extension which is a pickle file.`“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshaling,” or “flattening” .` Test and Training data can be downloaded from here : https://d17h27t6h515a5.cloudfront.net/topher/2016/November/581faac4_traffic-signs-data/traffic-signs-data.zip<jupyter_code>##Loading the Data
training_file ='train.p'
testing_file  ='test.p'

with open(training_file ,mode='rb') as f:
    train =pickle.load(f)

with open(testing_file ,mode='rb') as f:
    test =pickle.load(f)

x_train ,y_train =train['features'] ,train['labels']
x_test ,y_test   =test['features']  ,test['labels']
print(x_train.shape ,y_train.shape)
print(x_test.shape ,y_test.shape)
##This csv contains the lables name of the signs
signs_classID =pd.read_csv('signnames.csv')
signs_classID.iloc[:5]
## unique classes stores the distinct labels
## unique_sign    stores the distinct classes labels
## unique_sign_counts stores the count of each label in the dataset

unique_classes,unique_sign ,unique_sign_counts=np.unique(y_train ,return_index =True ,return_counts=True)
unique_sign_counts<jupyter_output><empty_output><jupyter_text>Data Plotting and Exploration<jupyter_code>def plot_images():
    
    fig =plt.figure(figsize=(18,18))
    i=0
    for index in (unique_sign):
        #     ax.set_xticklabels([])
        #     ax.set_yticklabels([])
        #     ax.set_aspect('equal')
        img =x_train[index]
        plt.subplot(11,4,i+1)
        plt.imshow(img)
        plt.title( str(y_train[index])+'. '+signs_classID['SignName'][y_train[index]] )
        plt.axis('off')
        i =i+1
    plt.show()
import matplotlib.image as img
plot_images()
## plotting histogram

plt.figure(figsize=(15,5))
plt.bar(unique_classes,unique_sign_counts ,color='r' ,alpha=0.55)
plt.xlabel('label')
plt.ylabel('no. of distinct samples')
plt.xticks(unique_classes)
for a,b in zip(unique_classes ,unique_sign_counts):
    plt.text(a, b, str(b))
plt.tight_layout()
plt.show()<jupyter_output><empty_output><jupyter_text>Data Preprocessing Preprocessing includes normalization ,filtering noise , rescaling , dimension reduction of the images for better accuracy.Here we will be using open cv2 library to normalize the images between 50 to 200  through MIN-MAX scaling.1.Normalization will mitigate differences due to light condition across the data set and will make the pixel intesity consistant.2.Image Blurring or (Image Smoothing) :Bluring is done to remove noise/unwanted features. Median blur was choosen because it works good in removing salt and pepper noise and produces best results when tested in comparison with averaging, gaussian blur or bilateralFilter.understanding source : https://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html<jupyter_code>## preprocessing and scaling of data
import cv2
def normailze_blur(img):
    img  =cv2.normalize(img, img, 50,200 ,cv2.NORM_MINMAX)
    cv2.medianBlur(img, 3)                                 ##here 30% noise is added to the original dataset and applied median blur on it
    return img
for i in range(len(x_train)):
    normailze_blur(x_train[i])

for i in range(len(x_test)):
    normailze_blur(x_test[i])

## Plotting the normalized dataset
## clearly the are normalized on a given scale.
plot_images()<jupyter_output><empty_output><jupyter_text>Data Split into Train and Validation set<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

x_train ,y_train = shuffle(x_train,y_train)
x_test ,y_test =shuffle(x_test,y_test)
X_train , X_validation ,Y_train ,Y_validation =train_test_split(x_train,y_train,shuffle=True,random_state=0,test_size=0.3)<jupyter_output><empty_output><jupyter_text>One-hot encoding of the output as needed in the CNN network<jupyter_code>## one hot of the output
def one_hotencode(data):
    return np_utils.to_categorical(data,len(unique_classes))     # unique_classes here ,are 42
Y_train = one_hotencode(Y_train)
Y_validation =one_hotencode(Y_validation)
y_test =one_hotencode(y_test)
X_train.shape , X_validation.shape<jupyter_output><empty_output><jupyter_text>Defining the state of art (CNN) architecutre .specifying the Conv2D layers , hidden ,flatten layers<jupyter_code>## specifying the layers
model =Sequential()

model.add(Conv2D(32,(5,5),strides=(1,1) ,input_shape=(32,32,3)))
model.add(Activation('relu'))
BatchNormalization(axis=-1)
model.add(Conv2D(32 ,(5,5)))
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2,2)))
BatchNormalization(axis=-1)

model.add(Conv2D(64,(5,5)))
model.add(Activation('relu'))
BatchNormalization(axis =-1)

model.add(Conv2D(64 ,(5,5)))
model.add(Activation('relu'))

model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Flatten())
BatchNormalization()

model.add(Dense(1024))
model.add(Activation('relu'))

BatchNormalization()
model.add(Dropout(0.5))

model.add(Dense(len(unique_classes)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy' ,optimizer=Adam() ,metrics=['accuracy'])
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 28, 28, 32)        2432      
_________________________________________________________________
activation_1 (Activation)    (None, 28, 28, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 32)        25632     
_________________________________________________________________
activation_2 (Activation)    (None, 24, 24, 32)        0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 8, 8, 64)          51264     
_________________________________________________________________
activation[...]<jupyter_text>Data AugmentationData Augmentation is certainly a better way to get most out of the few or small dataset.Here Data Augmentation can certainly improve the accuracy of the network as rotating the image to a certain angle will not change the defined 'knowldege' or quality of the physical nature of the images.<jupyter_code>##Data augementation
##This network is paired using the Keras ImageDataGenerator to assist with image Augmentation through the learning process. 
##This can greatly improve the accuracy without the increase of labelled training examples.

gen = ImageDataGenerator(rotation_range=13 ,width_shift_range=0.12 ,shear_range=0.3 ,height_shift_range=0.12,zoom_range=0.2)
train_generator =gen.flow(X_train,Y_train,batch_size=32)
validate_gen =ImageDataGenerator()

validate_generator =validate_gen.flow(X_validation ,Y_validation)<jupyter_output><empty_output><jupyter_text>Training the Model<jupyter_code>model.fit_generator(train_generator ,steps_per_epoch=27446//20 ,epochs=7 ,
                    validation_data =validate_generator,validation_steps =11763//80)
x_test.shape ,y_test.shape<jupyter_output><empty_output><jupyter_text>Evaluating the accuracy metrics of the model<jupyter_code>accuracy =model.evaluate(x_test,y_test)
accuracy
print("Test Accuracy of the model --->" ,accuracy[1])
np.unique(np.argmax(y_test,axis=1) ,return_index=True ,return_counts=True)
model.predict_classes(x_test[0:50])
## Plotting the forst 50 x_test images for the verification

y_pred_labels =model.predict_classes(x_test[0:50])

#Converting onehot-encoded labels back to their numerical data
y_test_labels =np.argmax(y_test ,axis=1)
fig=plt.figure(figsize=(14,5))
for i in range(50):
    ax=fig.add_subplot(5,10,i+1 ,xticks=[] ,yticks=[])
    ax.imshow(x_train[i])
    ax.set_ylabel(y_pred_labels[i] ,color='black' if y_pred_labels[i]==y_test_labels[i] else 'red')
fig.suptitle('Predicted Labels; Incorrect Labels in Red' ,color='b' ,alpha=1.0)
plt.show<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10684624017957352, 'fraction_numerical': 0.025925925925925925, 'mean_word_length': 5.897058823529412, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '444200', 'license': 'MIT', 'n_tokens_mistral': 2965, 'n_tokens_neox': 2801, 'n_words': 706, 'path': 'Traffic Sign Recognition (ConvNet).ipynb', 'repo_name': 'BeingArnav/Traffic-Sign-Recognition-ConvNet-', 'size': '8910'}