Path: blob/main/C2/W4/assignment/C2W4_Assignment.ipynb
2955 views
Week 4: Multi-class Classification
Welcome to the last assignment! In this notebook, you will get a chance to work on a multi-class classification problem. You will be using the Sign Language MNIST dataset, which contains 28x28 images of hands depicting the letters of the english alphabet.
TIPS FOR SUCCESSFUL GRADING OF YOUR ASSIGNMENT:
All cells are frozen except for the ones where you need to submit your solutions or when explicitly mentioned you can interact with it.
You can add new cells to experiment but these will be omitted by the grader, so don't rely on newly created cells to host your solution code, use the provided places for this.
You can add the comment # grade-up-to-here in any graded cell to signal the grader that it must only evaluate up to that point. This is helpful if you want to check if you are on the right track even if you are not done with the whole assignment. Be sure to remember to delete the comment afterwards!
Avoid using global variables unless you absolutely have to. The grader tests your code in an isolated environment without running all cells from the top. As a result, global variables may be unavailable when scoring your submission. Global variables that are meant to be used will be defined in UPPERCASE.
To submit your notebook, save it and then click on the blue submit button at the beginning of the page.
Let's get started!
In this assignment you will actually be working with a modified version of the original Sign Language MNIST dataset. The original dataset is presented as a csv file, however this makes the pre processing of the data very different from what you have been doing so far. To make loading the images and creating the datasetss more aligned with what you have learned so far, we have already downloaded each image as a .png file. You can find them in the data/train
and data/validation
folders. As the names suggest, the images in the first folder will be used for training, and the ones in the latter will be used for validation.
Begin by defining some globals with the paths to the training and test folders.
Let's explore the ./data
folder containing the images. There is a subdirectory for each class. In this case there will be 24 folders one for each letter in the alphabet, except for letters J and Z. Because of gesture motions these two letters can't be represented by an image, and are thus not included on the dataset.
The complete tree looks like this:
Let's take a look at what the images look like.
By plotting the images with matplotlib
you can readily see images have a resolution of 28x28 (look at the image axes) and are in greyscale, but you can double check this by using the code below:
Don't worry about the last dimension. That is because the img_to_array
function returns a 3D array. You can easily check that actually it has repeated the same values in each dimension, for example, take a look at the first 5 columns of the image. All you really care about is that your image is 28x28 pixels.
Creating the datasets for the CNN
Exercise 1: train_val_datasets
Your first task is to code the function that will create the datasets that will yield batches of images, both for training and validation. For this complete the train_val_generators
function below.
For grading purposes, make sure to use a batch size of 32.
Expected Output:
Coding the CNN
Exercise : create_model
One last step before training is to define the architecture of the model that will be trained.
Complete the create_model
function below. This function should return a Keras' model that uses the Sequential
API.
A couple of things to keep in mind when defining the architecture:
Start the model with an
Input
followed by a layer that rescales your images so that each pixel has values between 0 and 1There different ways to implement the output layer, however, we expect the last layer of your model to have a number of units that corresponds to the number of possible categories, as well as the correct activation function.
Aside from defining the architecture of the model, you should also compile it so make sure to use a
loss
function that is suitable for multi-class classification. Remember to also define suitablemetric
to monitor.
Note that you should use no more than 2 Conv2D and 2 MaxPooling2D layers to achieve the desired performance. You can also add dropout layers to improve training
The next cell allows you to check the number of total and trainable parameters of your model and prompts a warning in case these exceeds those of a reference solution, this serves the following 3 purposes listed in order of priority:
Helps you prevent crashing the kernel during training.
Helps you avoid longer-than-necessary training times.
Provides a reasonable estimate of the size of your model. In general you will usually prefer smaller models given that they accomplish their goal successfully.
Notice that this is just informative and may be very well below the actual limit for size of the model necessary to crash the kernel. So even if you exceed this reference you are probably fine. However, if the kernel crashes during training or it is taking a very long time and your model is larger than the reference, come back here and try to get the number of parameters closer to the reference.
Check that the input and output shape of your model are correct
Expected output:
Using the summary
method you can visulize the model you just defined.
Check that the architecture you used is compatible with the dataset (you can ignore the warnings prompted by using the GPU):
Expected output:
Finally, you can go ahead and train your model
Now take a look at your training history:
You will not be graded based on the accuracy of your model but try making it as high as possible for both training and validation, as an optional exercise, after submitting your notebook for grading.
A reasonable benchmark is to achieve over 99% accuracy for training and over 95% accuracy for validation within 15 epochs. Try tweaking your model's architecture or the augmentation techniques to see if you can achieve these levels of accuracy.
Congratulations on finishing this week's assignment!
You have successfully implemented a convolutional neural network that is able to perform multi-class classification tasks! Nice job!
Keep it up!