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 Conv2D, Dense, MaxPool2D, Input, Activation, Flatten, Dropout
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)
Process all the data as close as mean 0.0 and standard deviation 1.0.
In [4]:
def normalize(arr):
arr = arr.astype('float32')
return (arr - np.mean(arr))/np.std(arr)
X_train_norm = normalize(X_train)
X_val_norm = normalize(X_val)
X_test_norm = normalize(X_test)
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, X_train_norm)
print_info('Valdation', X_val, X_val_norm)
print_info('Test', X_test, X_test_norm)
Convert all the classes as one hot encode.
In [5]:
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 [6]:
trc = rn.sample(range(n_test), 16)
In [7]:
def plot_images(arr_1, arr_2, pred):
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 type(pred) != type(np.array([])):
ax.imshow(arr_1[trc[i]])
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_test, Y_test, pred=False)
After normalization
In [8]:
plot_images(X_test_norm, Y_test, pred=False)
In [9]:
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(32, 32, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(43, activation='softmax'))
In [10]:
model.summary()
In [11]:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
In [12]:
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 [13]:
score, acc = model.evaluate(X_test_norm, Y_test_cat, batch_size=64, verbose=0)
In [14]:
print('Score: ', score)
print('Acc: ', acc)
In [15]:
Y_pred = model.predict_classes(X_test_norm, batch_size=64, verbose=0)
In [16]:
plot_images(X_test, Y_test, Y_pred)
In [17]:
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(32, 32, 3), activation='relu'))
model.add(MaxPool2D((2,2)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(43, activation='softmax'))
In [18]:
model.summary()
In [19]:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
In [20]:
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 [21]:
score, acc = model.evaluate(X_test_norm, Y_test_cat, batch_size=64, verbose=0)
In [22]:
print('Score: ', score)
print('Acc: ', acc)
In [23]:
Y_pred = model.predict_classes(X_test_norm, batch_size=64, verbose=0)
In [24]:
plot_images(X_test, Y_test, Y_pred)
In [25]:
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(32, 32, 3), activation='relu'))
model.add(MaxPool2D((2,2)))
model.add((Dropout(0.5)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(43, activation='softmax'))
In [26]:
model.summary()
In [27]:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
In [28]:
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 [29]:
score, acc = model.evaluate(X_test_norm, Y_test_cat, batch_size=64, verbose=0)
In [30]:
print('Score: ', score)
print('Acc: ', acc)
In [31]:
Y_pred = model.predict_classes(X_test_norm, batch_size=64, verbose=0)
In [32]:
plot_images(X_test, Y_test, Y_pred)
In [33]:
model = Sequential()
model.add(Conv2D(16, (5, 5), input_shape=(32, 32, 3), activation='relu'))
model.add(MaxPool2D((2,2)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3), input_shape=(16, 16, 16), activation='relu'))
model.add(MaxPool2D((2,2)))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3), input_shape=(7, 7, 32), activation='relu'))
model.add(MaxPool2D((2,2)))
model.add(Activation('relu'))
model.add(Flatten())
model.add((Dropout(0.5)))
model.add(Dense(128, activation='relu'))
model.add(Dense(43, activation='softmax'))
In [34]:
model.summary()
In [35]:
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
In [36]:
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 [37]:
score, acc = model.evaluate(X_test_norm, Y_test_cat, batch_size=64, verbose=0)
In [38]:
print('Score: ', score)
print('Acc: ', acc)
In [39]:
Y_pred = model.predict_classes(X_test_norm, batch_size=64, verbose=0)
In [40]:
plot_images(X_test, Y_test, Y_pred)
In [ ]:
In [ ]:
In [ ]: