In [ ]:
## 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()
How to classify images using CNN layers in Keras: An application of MNIST Dataset*
Using TensorFlow backend.
X_train shape: (60000, 28, 28)
60000 train samples
X_test shape: (10000, 28, 28)
10000 test samples

X_train shape: (60000, 28, 28, 1)
60000 train samples
X_test shape: (10000, 28, 28, 1)
10000 test samples
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 28, 28, 64)        640       
_________________________________________________________________
activation_1 (Activation)    (None, 28, 28, 64)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 28, 28, 64)        36928     
_________________________________________________________________
activation_2 (Activation)    (None, 28, 28, 64)        0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 64)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 14, 14, 64)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 14, 14, 64)        36928     
_________________________________________________________________
activation_3 (Activation)    (None, 14, 14, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 14, 14, 64)        36928     
_________________________________________________________________
activation_4 (Activation)    (None, 14, 14, 64)        0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 7, 7, 64)          0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 7, 7, 64)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 3136)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               1606144   
_________________________________________________________________
activation_5 (Activation)    (None, 512)               0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                5130      
_________________________________________________________________
activation_6 (Activation)    (None, 10)                0         
=================================================================
Total params: 1,722,698
Trainable params: 1,722,698
Non-trainable params: 0
_________________________________________________________________
Train on 48000 samples, validate on 12000 samples
Epoch 1/5
 8448/48000 [====>.........................] - ETA: 2:27 - loss: 2.2973 - accuracy: 0.1191