In [1]:
%%bash
source ~/kerai/bin/activate
In [2]:
%matplotlib inline
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from keras.models import Sequential
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
from keras.layers import Lambda, Conv2D, MaxPooling2D, Dropout, Dense, Flatten, Activation
Import helper functions
In [3]:
from helper import get_class_names, get_train_data, get_test_data, plot_images, plot_model
Change matplotlib graph style
In [4]:
matplotlib.style.use('ggplot')
Import class names
In [5]:
class_names = get_class_names()
print(class_names)
Get number of classes
In [6]:
num_classes = len(class_names)
print(num_classes)
In [1]:
# Hight and width of the images
IMAGE_SIZE = 32
# 3 channels, Red, Green and Blue
CHANNELS = 3
Load the training dataset. Labels are integers whereas class is one-hot encoded vectors.
In [7]:
images_train, labels_train, class_train = get_train_data()
Normal labels
In [8]:
print(labels_train)
One hot encoded labels
In [9]:
print(class_train)
Load the testing dataset.
In [10]:
images_test, labels_test, class_test = get_test_data()
In [11]:
print("Training set size:\t",len(images_train))
print("Testing set size:\t",len(images_test))
The CIFAR-10 dataset has been loaded and consists of a total of 60,000 images and corresponding labels.
In [12]:
def cnn_model():
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', input_shape=(IMAGE_SIZE,IMAGE_SIZE,CHANNELS)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
return model
Build model
In [13]:
model = cnn_model()
Save the model after every epoch
In [14]:
checkpoint = ModelCheckpoint('best_model_simple.h5', # model filename
monitor='val_loss', # quantity to monitor
verbose=0, # verbosity - 0 or 1
save_best_only= True, # The latest best model will not be overwritten
mode='auto') # The decision to overwrite model is made
# automatically depending on the quantity to monitor
Configure the model for training
In [15]:
model.compile(loss='categorical_crossentropy', # Better loss function for neural networks
optimizer=Adam(lr=1.0e-4), # Adam optimizer with 1.0e-4 learning rate
metrics = ['accuracy']) # Metrics to be evaluated by the model
For more information on categorical cross entropy loss function see - https://jamesmccaffrey.wordpress.com/2013/11/05/why-you-should-use-cross-entropy-error-instead-of-classification-error-or-mean-squared-error-for-neural-network-classifier-training/
Fit the model on the data provided
In [16]:
model_details = model.fit(images_train, class_train,
batch_size = 128, # number of samples per gradient update
epochs = 100, # number of iterations
validation_data= (images_test, class_test),
callbacks=[checkpoint],
verbose=1)
In [17]:
scores = model.evaluate(images_test, class_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))
In [18]:
plot_model(model_details)
Predict class for test set images
In [23]:
class_pred = model.predict(images_test, batch_size=32)
print(class_pred[0])
Get the index of the largest element in each vector
In [24]:
labels_pred = np.argmax(class_pred,axis=1)
print(labels_pred)
Check which labels have been predicted correctly
In [25]:
correct = (labels_pred == labels_test)
print(correct)
print("Number of correct predictions: %d" % sum(correct))
Calculate accuracy using manual calculation
In [26]:
num_images = len(correct)
print("Accuracy: %.2f%%" % ((sum(correct)*100)/num_images))
Get the incorrectly classified images
In [27]:
incorrect = (correct == False)
# Images of the test-set that have been incorrectly classified.
images_error = images_test[incorrect]
# Get predicted classes for those images
labels_error = labels_pred[incorrect]
# Get true classes for those images
labels_true = labels_test[incorrect]
Plot the first 9 mis-classified images
In [28]:
plot_images(images=images_error[0:9],
labels_true=labels_true[0:9],
class_names=class_names,
labels_pred=labels_error[0:9])