## How to classify images using CNN layers in Keras: An application of MNIST Dataset
def Snippet_352():
print()
print(format('How to classify images using CNN layers in Keras: An application of MNIST Dataset','*^82'))
import time
start_time = time.time()
# load libraries
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import SGD, RMSprop, Adam
from keras.utils import np_utils
import matplotlib.pyplot as plt
# Hyper parameters for network building and training process
# MNIST is a set of 60K images 28x28 pixels on 1 channels (gray scale)
IMG_CHANNELS = 1; IMG_ROWS = 28; IMG_COLS = 28;
# Constant
BATCH_SIZE = 128; NB_CLASSES = 10; VERBOSE = 1; VALIDATION_SPLIT = 0.2
# data: shuffled and split between train and test sets
#load dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print()
print('X_train shape:', X_train.shape); print(X_train.shape[0], 'train samples')
print('X_test shape:', X_test.shape); print(X_test.shape[0], 'test samples')
# Reshpe the the dataset for Convulation Layers
X_train = X_train.reshape(X_train.shape[0], IMG_ROWS, IMG_COLS, IMG_CHANNELS)
X_test = X_test.reshape(X_test.shape[0], IMG_ROWS, IMG_COLS, IMG_CHANNELS)
print()
print('X_train shape:', X_train.shape); print(X_train.shape[0], 'train samples')
print('X_test shape:', X_test.shape); print(X_test.shape[0], 'test samples')
# convert categorical target using One-hot-coding
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
# float and normalization
X_train = X_train.astype('float32'); X_test = X_test.astype('float32');
X_train /= 255; X_test /= 255;
# ---------------------------------------------------------------------
# setup an experiment for different OPTIMIZERs, Epoch Sizes & Units
# and determine the best Accuracy
# ---------------------------------------------------------------------
accuracy = []
for OPTIMIZER in [SGD(), RMSprop(), Adam()]:
for NB_EPOCH in [5,10,20]:
for N_filters in [64, 128, 256]:
# network
# Three steps to create a CNN
# 1. Convolution
# 2. Activation
# 3. Pooling
# Repeat Steps 1,2,3 for adding more hidden layers
# 4. After that make a fully connected network
# This fully connected network gives ability to the CNN
# to classify the samples
model = Sequential()
# 1. Convolution
model.add(Conv2D(filters = N_filters, kernel_size=3, padding='same',
input_shape=(IMG_ROWS, IMG_COLS, IMG_CHANNELS)))
# 2. Activation
model.add(Activation('relu'))
# 1. Convolution
model.add(Conv2D(filters = N_filters, kernel_size=3, padding='same'))
# 2. Activation
model.add(Activation('relu'))
# 3. Pooling
model.add(MaxPooling2D(pool_size=(2, 2)))
# Dropout
model.add(Dropout(0.25))
model.add(Conv2D(filters = N_filters, kernel_size=3, padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(filters = N_filters, kernel_size=3, padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
# 4. Fully connected network
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))
# print model summary
model.summary()
model.compile(loss='categorical_crossentropy', optimizer=OPTIMIZER, metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=BATCH_SIZE, epochs=NB_EPOCH,
verbose=VERBOSE, validation_split=VALIDATION_SPLIT)
score = model.evaluate(X_test, Y_test, verbose=VERBOSE)
print()
print('Optimizers: ', OPTIMIZER)
print('Epoch Sizes: ', NB_EPOCH)
print('Neurons or filters: ', N_filters)
print("Test score:", score[0])
print('Test accuracy:', score[1])
accuracy.append(score[1])
print()
print(accuracy)
y = accuracy; N = len(y); x = range(N); width = 1./1.5;
plt.bar(x,y,width); plt.show()
print()
print("Execution Time %s seconds: " % (time.time() - start_time))
Snippet_352()