0% found this document useful (0 votes)
6 views8 pages

DLA Week 7

The document outlines the implementation of a Convolutional Neural Network (CNN) using Python and TensorFlow/Keras for classifying handwritten digits from the MNIST dataset. It details the steps for installing libraries, loading and preprocessing the dataset, building and training the CNN model, and evaluating its performance. The conclusion emphasizes the effectiveness of CNNs in image classification and their potential for more complex applications in machine learning.

Uploaded by

manoharmanukoji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

DLA Week 7

The document outlines the implementation of a Convolutional Neural Network (CNN) using Python and TensorFlow/Keras for classifying handwritten digits from the MNIST dataset. It details the steps for installing libraries, loading and preprocessing the dataset, building and training the CNN model, and evaluating its performance. The conclusion emphasizes the effectiveness of CNNs in image classification and their potential for more complex applications in machine learning.

Uploaded by

manoharmanukoji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Week 7

CNN Implementation on MNIST Dataset


Aim
 The primary aim of this Program is to develop and implement a Convolutional Neural
Network (CNN) using Python and TensorFlow/Keras for the classification of
handwritten digits from the MNIST dataset. By leveraging the hierarchical feature
extraction capabilities of CNNs, this project seeks to achieve high accuracy in
recognizing and categorizing the digits (0-9) from grayscale images, ultimately
demonstrating the effectiveness of deep learning techniques in image processing tasks.
Step 1: Install Required Libraries

1. pip install tensorflow

Step 2: Import Libraries

1. import tensorflow as tf
2. from tensorflow import keras
3. from tensorflow.keras import layers
4. import numpy as np
5. import matplotlib.pyplot as plt
Step 3: Load the MNIST Dataset

# Load the dataset


1. mnist = keras.datasets.mnist

# Split the dataset into training and testing data


1. (x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the images to values between 0 and 1


1. x_train = x_train.astype('float32') / 255
2. x_test = x_test.astype('float32') / 255

# Reshape the images to add the channel dimension (28x28x1)


1. x_train = np.expand_dims(x_train, -1)
2. x_test = np.expand_dims(x_test, -1)
Step 4: Build the CNN Model

1. model = keras.Sequential([
2. layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
3. layers.MaxPooling2D(pool_size=(2, 2)),
4. layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
5. layers.MaxPooling2D(pool_size=(2, 2)),
6. layers.Flatten(),
7. layers.Dense(128, activation='relu'),
8. layers.Dense(10, activation='softmax')
9. ])

# Print the model summary


1. model.summary()
Step 5: Compile the Model

1. model.compile(optimizer='adam',
2. loss='sparse_categorical_crossentropy',
3. metrics=['accuracy'])

Step 6: Train the Model

1. model.fit(x_train, y_train, epochs=5, batch_size=64, validation_split=0.2)

Step 7: Evaluate the Model

1. test_loss, test_accuracy = model.evaluate(x_test, y_test)


2. print(f'Test accuracy: {test_accuracy:.4f}')
Step 8: Make Predictions

1. predictions = model.predict(x_test)

# Display the first test image and its predicted label

1. plt.imshow(x_test[0].reshape(28, 28), cmap='gray')


2. plt.title(f'Predicted label: {np.argmax(predictions[0])}')
3. plt.show()
Conclusion

 In conclusion, the implementation of a Convolutional Neural Network (CNN) on the


MNIST dataset effectively demonstrates the capabilities of deep learning in image
classification tasks. The model successfully learns to recognize and classify
handwritten digits with high accuracy, showcasing the importance of feature
extraction through convolutional layers and the role of pooling in reducing
dimensionality. This project not only highlights the practical application of CNNs in
computer vision but also serves as a foundational step for exploring more complex
architectures and datasets in the realm of machine learning.

You might also like