Using German Traffic Sign Dataset (http://benchmark.ini.rub.de/?section=gtsrb&subsection=dataset). You can download the from here
In [1]:
import matplotlib.pyplot as plt
import random as rn
import numpy as np
from sklearn.model_selection import train_test_split
import pickle
from keras.models import Sequential
from keras.layers import Dense, Input, Activation
from keras.utils import np_utils
%matplotlib inline
In [2]:
train_data = 'data/train.p'
test_data = 'data/test.p'
with open(train_data, 'rb') as f:
train = pickle.load(f)
with open(test_data, 'rb') as f:
test = pickle.load(f)
Spliting the train data as train and validation set
In [3]:
X_train, X_val, Y_train, Y_val = train_test_split(train['features'], train['labels'], test_size=0.3, random_state=0)
X_test, Y_test = test['features'], test['labels']
n_train = X_train.shape[0]
n_val = X_val.shape[0]
n_test = X_test.shape[0]
image_shape = X_train.shape[1], X_train.shape[2]
n_channels = X_train.shape[3]
n_classes = np.unique(train['labels']).size
print('Train data size:\t\t\t', n_train)
print('Validation data size:\t\t\t', n_val)
print('test data size:\t\t\t\t', n_test)
print('Image shape:\t\t\t\t', image_shape)
print('Number of color channels in image:\t', n_channels)
print('Number of classes:\t\t\t', n_classes)
In [4]:
def reshape(arr):
return arr.reshape(-1, image_shape[0]*image_shape[1]*n_channels)
X_train_flat = reshape(X_train)
X_val_flat = reshape(X_val)
X_test_flat = reshape(X_test)
def print_info(st, arr_1, arr_2):
print('{} data shape before reshape: {}, and after reshape: {}'.format(st, arr_1.shape, arr_2.shape))
print_info('Train', X_train, X_test_flat)
print_info('Validation', X_val, X_val_flat)
print_info('Test', X_test, X_test_flat)
Process all the data as close as mean 0.0 and standard deviation 1.0.
In [5]:
def normalize(arr):
arr = arr.astype('float32')
return (arr - np.mean(arr))/np.std(arr)
X_train_norm = normalize(X_train_flat)
X_val_norm = normalize(X_val_flat)
X_test_norm = normalize(X_test_flat)
def print_info(st, arr_1, arr_2):
print('{} Data: Before normalization : type: {}, mean: {}, std: {}. After processing, type: {}, mean: {}, std: {}'. format(st, arr_1.dtype, round(np.mean(arr_1),2), round(np.std(arr_1),2), arr_2.dtype, round(np.mean(arr_2),2), round(np.std(arr_2),2)))
print_info('Train', X_train_flat, X_train_norm)
print_info('Valdation', X_val_flat, X_val_norm)
print_info('Test', X_test_flat, X_test_norm)
Convert all the classes as one hot encode.
In [6]:
def make_categorical(arr):
return np_utils.to_categorical(arr, n_classes)
Y_train_cat = make_categorical(Y_train)
Y_val_cat = make_categorical(Y_val)
Y_test_cat = make_categorical(Y_test)
Before normalization
In [7]:
trc = rn.sample(range(n_test), 16)
In [8]:
def plot_images(arr_1, arr_2, pred=False):
fig, axes = plt.subplots(4, 4, figsize=(10,10))
fig.subplots_adjust(hspace=0.3, wspace=0.3)
for i, ax in enumerate(axes.flat):
if len(arr_1.shape) == 2:
ax.imshow(arr_1[trc[i]].reshape(32,32,3))
ax.set_xlabel('true:{}'.format(arr_2[trc[i]]))
else:
ax.imshow(arr_1[trc[i]])
ax.set_xlabel('true:{}, pred:{}'.format(arr_2[trc[i]], pred[trc[i]]))
ax.set_xticks([])
ax.set_yticks([])
plot_images(X_train_flat, Y_train)
After normalization
In [9]:
plot_images(X_train_norm, Y_train)
In [10]:
model = Sequential()
model.add(Dense(256, activation='relu', input_shape=(32*32*3,)))
#model.add(Dense(800, activation='relu',input_shape=(1555,)))
model.add(Dense(43, activation='softmax'))
In [11]:
model.summary()
In [12]:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
In [13]:
history = model.fit(X_train_norm, Y_train_cat, batch_size=64, epochs=20, verbose=1, validation_data=(X_val_norm, Y_val_cat))
In [14]:
history.history['val_acc'][-1]
Out[14]:
In [40]:
score, acc = model.evaluate(X_test_norm, Y_test_cat, batch_size=64, verbose=0)
In [38]:
print('Score:\t', score)
print('Acc:\t{}%'.format(round(acc*100)))
In [41]:
Y_predd = model.predict_classes(X_test_norm, batch_size=64, verbose=0)
In [43]:
plot_images(X_test, Y_test, Y_pred)
In [ ]: