MNIST Classifier

To start off with modeling lets build a simple CNN digit classifier.

The 1st layer is a convolution layer that has 32 filters of shape 3*3, with 'relu' activation and dropout as the regularizer.

The 2nd layer is a convolution layer that has 64 filters of shape 3*3, with 'relu' activation and dropout as the regularizer.

The 3rd layer is a convolution layer that has 128 filters of shape 3*3, with 'relu' activation and dropout as the regularizer, which is finally flattened.

The fourth layer is a dense layer of 1024 neurons with 'relu' activation.

The final layer is a dense layer with 10 neurons corresponding to the 10 classes in the MNIST dataset, and the activation used here is softmax.

Batch size: 128

Optimizer: Adam

Validation split: 0.2, this means that 20% of the training set will be used as the validation set.

# input image shape
input_shape = (28,28,1)

def train_mnist(input_shape, X_train, y_train):
model = Sequential()
model.add(Conv2D(32, (3, 3), strides=2, padding='same',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Dropout(0.2))

model.add(Conv2D(64, (3, 3), strides=2, padding='same'))
model.add(Activation('relu'))
model.add(Dropout(0.2))

model.add(Conv2D(128, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Flatten())

model.add(Dense(1024, activation = 'relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = 'adam', metrics = ['accuracy'])
model.fit(X_train, y_train, batch_size = 128,
epochs = 3, validation_split=0.2, verbose = 1 )
return model

mnist_model = train_mnist(input_shape, X_train, y_train)
Figure 14.6: MNIST CNN classifier training for 3 epochs

Use the built CNN digit classifier on the masked images to get a measure of its performance on digits that are missing small sections. 

# prediction on the masked images
pred_labels = mnist_model.predict_classes(noised_test_data)
print('The model model accuracy on the masked images is:',np.mean(pred_labels==y_test)*100)

On the Masked Images, the CNN digit classifier is 74.9% accurate. Might be slightly different when you run it but will be very close.

We have not used maxpooling in the above classifier. Try building the same classifier with maxpooling or other pooling options.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.136