Reference no: EM133353199
Convolutional Neural Network (CNN)
In this question, the goal is to train a Convolutional Neural Network (CNN) to classify images of handwritten digits from the MNIST dataset using Google Colab. The MNIST dataset is a well-known benchmark dataset for image classification tasks, consisting of 60,000 training images and 10,000 test images of handwritten digits (0-9) in grayscale, each with a resolution of 28x28 pixels. The goal of the task is to train a CNN model that can accurately classify each image in the test set according to its correct digit label. Follow the provided instructions step-by-step to load the MNIST dataset and preprocess the data by normalizing the pixel values to be between 0 and 1, and then train a CNN model using Keras API in TensorFlow. Afterward, answer the questions which involve evaluating the model's performance on the test set.
1. First, open your web browser and navigate to the Google Colab website
2. If you are not already signed into your Google account, click the "Sign in" button in the top-right corner of the screen and enter your Google credentials. Once you are signed in, you can create a new Colab notebook by clicking the "New Notebook" button on the home screen. This will open a new notebook with a blank Python code cell. You can write and execute Python code in the code cell just as you would in a local Python environment. To execute a cell, click the "Run" button to the left of the cell, or press the "Shift + Enter" keyboard shortcut.
3. To save your work in Google Colab, you can either use the keyboard shortcut "Ctrl + S" or click on "File" in the menu bar and then select "Save" or "Save a copy in Drive". If you choose "Save a copy in Drive", a copy of the notebook will be saved to your Google Drive account. If you choose "Save", the notebook will be saved in the Colab environment. If you want to download the notebook to your local machine, you can click on "File" in the menu bar and then select "Download .ipynb". This will download the notebook file in Jupyter notebook format to your local machine.
4. Copy and paste the following code at the first cell of your code:
This code imports the necessary TensorFlow and Keras libraries for building and training the CNN model.
5. Next, load the MNIST dataset by adding the following code:
This code loads the MNIST dataset, which consists of 60,000 training images and 10,000 test images of handwritten digits. x_train and x_test are the images as the input and y_train and y_test are their labels as the output.
6. Show one of the images in the dataset with its label using the following code:
You can try different images by changing 0 to a random number in img and label.
7. Normalize the pixel values of the images by adding the following code:
This code scales the pixel values of each image to be between 0 and 1, which can improve the performance of the model.
8. Build the CNN model by adding the following code:
This code defines a simple CNN model with a convolutional layer, a pooling layer, and two fully connected layers. The input shape of the model is (28, 28, 1), which corresponds to the shape of the MNIST images. The convolutional layer has 32 filters with a kernel size of 3x3 and uses the ReLU activation function. The pooling layer reduces the spatial dimensions of the output by a factor of 2. The first fully connected layer has 128 units and uses the ReLU activation function, while the output layer has 10 units and uses the SoftMax activation function to produce a probability distribution over the 10 possible digit classes.
9. Compile the model by adding the following code:
This code specifies the optimizer, loss function, and evaluation metrics for the model. The Adam optimizer is a popular choice for training CNNs, and the sparse categorical cross entropy loss function is appropriate for multi-class classification problems like MNIST.
10. Train the model by adding the following code:
This code trains the model on the MNIST training data for 10 epochs, using the validation data to monitor the performance of the model during training.
Now, it's time to evaluate the model on the test data.
Question 1) Plot the training and validation accuracy curves over the 10 epochs of training. You can use the history.history attribute which contains information about the training history of the model, including the values of the training and validation metrics at each epoch. For example,
training_accuracy = history.history['accuracy'] plt.plot(training_accuracy, label='Training Accuracy')
How does the training and validation accuracy change over time?
Question 2) Plot the training and validation loss curves over the 10 epochs of training. How does the training and validation loss change over time?
Question 3) Show five example images from the test set with their predicted labels. To predict the labels you can use the model.predict method to predict the labels for a set of input images. Also, in order to get the more probable label for each image, you can use the np.argmax method. For example:
import numpy as np
y_pred = model.predict(np.reshape(x_test[‘sample_number'], (1,28,28))) print(y_pred)
y_label = np.argmax (y_pred) print(y_label)
np.reshape() is used to change the shape of the x_test to an appropriate format. You can change
‘sample_number' into five different samples you want.
Question 4) Calculate the overall test accuracy of the model using "model.evaluate" which computes the loss and metrics for the test data.
test_loss, test_acc = model.evaluate(x_test, y_test)
Question 5) What is your suggestion to improve the overall test accuracy?
Submission:
Submit two files:
1. A PDF file containing the solutions and the requested figures/explanations. Save your file as
FirstName-LastName.PDF.
2. Download your code as "Assignment_3_ FirstName-LastName.ipynb". Create and upload a .zip file of your code, called code.zip.
Attachment:- Convolutional Neural Network.rar