<jupyter_start><jupyter_text>**Import Libraries**<jupyter_code>import keras
from keras.models import Sequential
from keras.layers.convolutional import Conv2D ,MaxPooling2D
from keras.layers import Dense, Dropout, Flatten
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import seaborn as sns
import cv2
import pickle
import pandas as pd
import numpy as np
import random<jupyter_output>Using TensorFlow backend.
/usr/local/lib/python3.6/dist-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.
  import pandas.util.testing as tm<jupyter_text>**Import Dataset and Explore it**<jupyter_code># cloning repo containing the traffic sign dataset
!git clone https://bitbucket.org/jadslim/german-traffic-signs
# list dataset contents
 
!ls german-traffic-signs

#load the dataset

data = pd.read_csv('german-traffic-signs/signnames.csv')
data

#loading pickled dataset

with open("german-traffic-signs/train.p",mode='rb') as training:
  train = pickle.load(training)

with open("german-traffic-signs/valid.p",mode='rb') as validation:
  valid = pickle.load(validation)

with open("german-traffic-signs/test.p",mode='rb') as testing:
  test = pickle.load(testing)


X_train , y_train = train['features'], train['labels']
X_validation , y_validation = valid['features'], valid['labels']
X_test , y_test = test['features'], test['labels']
print(X_train.shape)
print(X_validation.shape)
print(X_test.shape)

index = np.random.randint(1,len(X_train))
plt.imshow(X_train[index])
print("image label={}".format(y_train[index]))<jupyter_output>image label=5<jupyter_text>Image Pre-processing  1. Converting to grayscale 2. Histogram Equalization 3. Normalization<jupyter_code># shuffle the data
from sklearn.utils import shuffle
X_train , y_train = shuffle(X_train,y_train)


def preprocessing(img): 
  #convert to grayscale
  img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  #Applying histogram equalization 
  img = cv2.equalizeHist(img)
  #Normalization
  img = img/255
  return img
    
     
X_train_processed = np.array(list(map(preprocessing,X_train)))
X_validation_processed = np.array(list(map(preprocessing,X_validation)))
X_test_processed = np.array(list(map(preprocessing,X_test)))

X_train_processed = X_train_processed.reshape(34799,32,32,1)
X_test_processed = X_test_processed.reshape(12630,32,32,1)
X_validation_processed = X_validation_processed.reshape(4410,32,32,1)
print(X_train_processed.shape)
print(X_test_processed.shape)
print(X_validation_processed.shape)

i= random.randint(1,len(X_train))
plt.imshow(X_train_processed[i].squeeze(),cmap='gray')
plt.figure()
plt.imshow(X_train[i].squeeze())<jupyter_output><empty_output><jupyter_text>Build Deep Convolution Neural Network Model<jupyter_code>model = Sequential()
# add the convolutional layer
# filters, size of filters,input_shape,activation_function
model.add(Conv2D(32,(5,5),activation='relu',input_shape=(32,32,1)))

# pooling layer
model.add(MaxPooling2D(pool_size=(2,2)))

# place a dropout layer
model.add(Dropout(0.25))

# add another convolutional layer
model.add(Conv2D(64,(5,5),activation='relu'))

# pooling layer
model.add(MaxPooling2D(pool_size=(2,2)))

# Flatten the image to 1 dimensional array
model.add(Flatten())

# add a dense layer : amount of nodes, activation
model.add(Dense(256,activation='relu'))

# place a dropout layer
# 0.5 drop out rate is recommended, half input nodes will be dropped at each update
model.add(Dropout(0.5))

# defining the ouput layer of our network
model.add(Dense(43,activation='softmax'))

model.summary()

#compile our model
model.compile(Adam(lr=0.0001),loss='sparse_categorical_crossentropy',metrics=['accuracy'])

history = model.fit(X_train_processed,
                    y_train,
                    batch_size=500,
                    epochs=50,
                    verbose=1,
                    validation_data=(X_validation_processed,y_validation))<jupyter_output>Train on 34799 samples, validate on 4410 samples
Epoch 1/50
34799/34799 [==============================] - 8s 243us/step - loss: 3.5824 - accuracy: 0.0616 - val_loss: 3.4530 - val_accuracy: 0.2002
Epoch 2/50
34799/34799 [==============================] - 2s 62us/step - loss: 3.2380 - accuracy: 0.1776 - val_loss: 2.9461 - val_accuracy: 0.3517
Epoch 3/50
34799/34799 [==============================] - 2s 62us/step - loss: 2.6096 - accuracy: 0.3453 - val_loss: 2.2761 - val_accuracy: 0.4297
Epoch 4/50
34799/34799 [==============================] - 2s 62us/step - loss: 2.0999 - accuracy: 0.4414 - val_loss: 1.8384 - val_accuracy: 0.5177
Epoch 5/50
34799/34799 [==============================] - 2s 62us/step - loss: 1.7612 - accuracy: 0.5098 - val_loss: 1.5191 - val_accuracy: 0.6034
Epoch 6/50
34799/34799 [==============================] - 2s 62us/step - loss: 1.5161 - accuracy: 0.5659 - val_loss: 1.2880 - val_accuracy: 0.6703
Epoch 7/50
34799/34799 [==============================] - 2s 62us/st[...]<jupyter_text>Evaluate Performance of the model<jupyter_code>score = model.evaluate(X_test_processed,y_test)
print('Test accuracy',score[1])
history.history.keys()
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['training','validation'])
plt.title('Training and validation losses')
plt.xlabel('epochs')

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training','validation'])
plt.title('Training and validation accuracy')
plt.xlabel('epochs')
prediction = model.predict_classes(X_test_processed)
y_true_label= y_test
from sklearn.metrics import confusion_matrix
matrix = confusion_matrix(y_true_label,prediction)
plt.figure(figsize=(20,20))
sns.heatmap(matrix,annot=True)
L=6
W=6
fig , axes = plt.subplots(L,W,figsize=(12,12))
axes = axes.ravel()
for i in range(0,L*W):
  axes[i].imshow(X_test[i])
  axes[i].set_title('Prediction ={}\n True={}'.format(prediction[i],y_true_label[i]))
  axes[i].axis('off')
plt.subplots_adjust(wspace=1)

  
  
model.save('my_model.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16217548471902726, 'fraction_numerical': 0.05750903713440684, 'mean_word_length': 5.847019122609674, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '237249', 'license': 'Apache-2.0', 'n_tokens_mistral': 2404, 'n_tokens_neox': 2109, 'n_words': 421, 'path': 'traffic_signs_classification .ipynb', 'repo_name': 'RATHEESHWARAA/Traffic-Sign-Classification-', 'size': '6086'}