How to Build a Convolutional Neural Network for Handwritten Digits Classification Using Deep Learning with Keras in Python?
Share
Condition for Building a Convolutional Neural Network for Handwritten Digits Classification Using Deep Learning with Keras in Python
Description: The code implements a Convolutional Neural Network (CNN) to classify handwritten digits from the MNIST dataset. It preprocesses the image data, builds a CNN model with convolutional and dense layers, and evaluates the model's performance using various metrics such as accuracy, F1-score, recall, and precision. The results are displayed in the form of classification reports and confusion matrix.
Step-by-Step Process
Step1: The MNIST dataset is loaded using TensorFlow's keras.datasets.mnist function, providing training and testing images and labels.
Step2: The function plot_sample_images is used to display 15 random images from the training dataset with their corresponding labels.
Step3: Both the training and testing image pixel values are normalized by dividing them by 255 to scale them between 0 and 1.
Step4: The training and testing images are reshaped to add an extra dimension for the channel (grayscale images), converting them into 4D tensors.
Step5: The CNN_model function defines the CNN architecture with two convolutional layers, followed by max-pooling layers, flattening, and dense layers for classification.
Step6: The CNN model is compiled with the Adam optimizer, sparse categorical cross-entropy loss function, and accuracy as the evaluation metric.
Step7: The model is trained on the training data using model.fit, with a batch size of 16 and 10 epochs, while also validating on the test data.
Step8: After training, predictions are made on the test data using model.predict, and the output is converted from probability scores to class labels.
Step9: The model's performance is assessed by generating a classification report, confusion matrix, and calculating accuracy, F1-score, recall, and precision.
Step10: The performance metrics (accuracy, F1-score, recall, precision) are printed along with the classification report and confusion matrix to evaluate the model’s effectiveness.
Sample Code
#Import Necessary Libraries
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.models import Model
from sklearn.metrics import (classification_report,confusion_matrix,accuracy_score,
f1_score,recall_score,precision_score)
import warnings
warnings.filterwarnings("ignore")
# Load the MNIST dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Class names for MNIST (digits 0-9)
class_names = [str(i) for i in range(10)]
# Function to plot sample images
def plot_sample_images(images, labels, class_names, rows=3, cols=5):
plt.figure(figsize=(10, 7))
for i in range(rows * cols):
plt.subplot(rows, cols, i + 1)
plt.imshow(images[i], cmap="gray")
plt.title(class_names[labels[i]])
plt.axis("off")
plt.tight_layout()
plt.show()
plot_sample_images(x_train, y_train, class_names)
#data_normalization
x_train = x_train/255
x_test = x_test/255
x_train = x_train.reshape(x_train.shape[0],x_train.shape[1],x_train.shape[2],1)
x_test = x_test.reshape(x_test.shape[0],x_test.shape[1],x_test.shape[2],1)
def CNN_model(input_shape):
# Input layer (for image data, input_shape includes height, width, and channels)
inputs = Input(shape=input_shape)
# Convolutional layers
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
# Flatten the 2D feature maps into a 1D feature vector
flatten = Flatten()(pool2)
# Fully connected (Dense) layers
dense1 = Dense(64, activation='relu')(flatten)
dense2 = Dense(32,activation='relu')(dense1)
output_layer = Dense(10, activation='softmax')(dense2)
# Build the model
cnn_model = Model(inputs=inputs, outputs=output_layer)
# Compile the model with Adam optimizer and binary crossentropy loss function
cnn_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return cnn_model
model = CNN_model((x_train.shape[1],x_train.shape[2],1))
model.fit(x_train,y_train,batch_size=16,epochs=10,validation_data=(x_test,y_test))
y_pred = model.predict(x_test)
y_pred = [np.argmax(i) for i in y_pred]
print("___Performance_Metrics___\n")
print('Classification_Report:\n',classification_report(y_test, y_pred))
print('Confusion_Matrix:\n',confusion_matrix(y_test, y_pred))
print('\n')
print('Accuracy_Score: ',accuracy_score(y_test, y_pred))
print('F1_Score (macro): ', f1_score(y_test, y_pred, average='macro'))
print('Recall_Score (macro): ', recall_score(y_test, y_pred, average='macro'))
print('Precision_Score (macro): ', precision_score(y_test, y_pred, average='macro'))