This project uses CNN (TensorFlow, Keras) to recognize digits from the MNIST dataset.
The neural network is built using the Sequential API of Keras.
Convolutional and Pooling Layers:
-
The first convolutional layer with 32 filters of size (3, 3), ReLU activation, and input shape (28, 28, 1).
-
Max pooling layer with a pool size of (2, 2).
-
The second convolutional layer with 64 filters of size (3, 3) and ReLU activation.
-
Another max pooling layer with a pool size of (2, 2).
Flatten and Dense Layers:
-
A flatten layer to convert the 2D feature maps into a 1D vector.
-
A dense layer with 64 neurons and ReLU activation.
-
The final dense layer with 10 neurons (number of classes) and softmax activation for multi-class classification.
Model Compilation
The model is compiled using the Adam optimizer, categorical crossentropy as the loss function, and accuracy as the metric to monitor during training.
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Model Training
The training process involves fitting the model to the training data for 5 epochs with a batch size of 64. The validation split is set to 20% to monitor performance on a validation subset.
model.fit(train_images.reshape(-1, 28, 28, 1), train_labels, epochs=5, batch_size=64, validation_split=0.2)
Test accuracy: 0.9891999959945679
Test loss:0.034049030393362045
![](https://private-user-images.githubusercontent.com/91430159/311371613-47f9c3f5-cf4e-4339-acd2-85b4b5406421.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjAxNzEyMDIsIm5iZiI6MTcyMDE3MDkwMiwicGF0aCI6Ii85MTQzMDE1OS8zMTEzNzE2MTMtNDdmOWMzZjUtY2Y0ZS00MzM5LWFjZDItODViNGI1NDA2NDIxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzA1VDA5MTUwMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWZmYTE1OGU5MmUwZDM1NmZhNmVjOTFhMzgyNzg4Y2QxZjdjMzAxYzA5ZjJkZmNiZGZlZjc2MjQ5ODQzYjE2NWEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.ENiRROrfUrTMZNsJMlNsxhbmlD3isOb5XjQpP3XNQMc)
I also tested the model to see if it can predict the digits written by myself.
While exploring the data, I observed that most sevens in the dataset were written without a horizontal bar. There was a vsible lack of horizontal-bar-sevens in the dataset (the way I personally write it).
And it turned out to be more difficult for a model to recognize such sevens (on the left).
I also presented an extreme case of writing a '3' digit to the model and it had hard time recognizing it to (also possibly due to poor picture quality)