Classification - Beyond Two Classes - Coursera
Classification - Beyond Two Classes - Coursera
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.
In [ ]: import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
In [ ]: import unittests
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.
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 1/8
10/29/24, 12:57 PM C2W4_Assignment
In [ ]: TRAIN_DIR = 'data/train/'
VALIDATION_DIR = 'data/validation/'
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.
.└── data/
├── train/
| ├── A/
| │ ├── a1.jpg
| │ ├── a2.jpg
| │ └── ...
| ├── B/
| ├── b1.jpg
| ├── b2.jpg
| └── ...
| ├── ...
| ├── I/
| | ├── i1.jpg
| | ├── i2.jpg
| | └── ...
| ├── K/
| | ├── k1.jpg
| | ├── k2.jpg
| | └── ...
| ├── ...
| └── Y/
| ├── y1.jpg
| ├── y2.jpg
| └── ...
└── validation/
├── A/
│ ├── a1.jpg
│ ├── a2.jpg
│ └── ...
├── B/
| ├── b1.jpg
| ├── b2.jpg
| └── ...
├── ...
├── I/
| ├── i1.jpg
| ├── i2.jpg
| └── ...
├── K/
| ├── k1.jpg
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 2/8
10/29/24, 12:57 PM C2W4_Assignment
| ├── k2.jpg
| └── ...
├── ...
└── Y/
├── y1.jpg
├── y2.jpg
└── ...
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.
In [ ]: sample_array[0,:5]
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 3/8
10/29/24, 12:57 PM C2W4_Assignment
Returns:
(tf.data.Dataset, tf.data.Dataset): train and validation datasets
"""
### START CODE HERE ###
train_dataset = tf.keras.utils.image_dataset_from_directory(
directory=TRAIN_DIR,
batch_size=32,
image_size=(28,28),
label_mode='categorical',
color_mode = "grayscale", # Use this argument to get just one color dimensi
)
validation_dataset = tf.keras.utils.image_dataset_from_directory(
directory=VALIDATION_DIR,
batch_size=32,
image_size=(28,28),
label_mode='categorical',
color_mode = "grayscale", # Use this argument to get just one color dimensi
)
### END CODE HERE ###
Expected Output:
Complete the create_model function below. This function should return a Keras' model
that uses the Sequential API.
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 4/8
10/29/24, 12:57 PM C2W4_Assignment
Start the model with an Input followed by a layer that rescales your images so that
each pixel has values between 0 and 1
There 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 suitable metric 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
Returns:
tf.keras.model.Sequential: CNN for multi-class classification
"""
### START CODE HERE ###
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 5/8
10/29/24, 12:57 PM C2W4_Assignment
# Output layer with 24 units (one for each class) and softmax activation
tf.keras.layers.Dense(24, activation='softmax')
])
model.compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
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:
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.
In [ ]: model.summary()
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 6/8
10/29/24, 12:57 PM C2W4_Assignment
Check that the architecture you used is compatible with the dataset (you can ignore the
warnings prompted by using the GPU):
try:
model.evaluate(example_batch_images, example_batch_labels, verbose=False)
except:
print("Your model is not compatible with the dataset you defined earlier. C
else:
predictions = model.predict(example_batch_images, verbose=False)
print(f"predictions have shape: {predictions.shape}")
Expected output:
plt.show()
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 7/8
10/29/24, 12:57 PM C2W4_Assignment
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.
You have successfully implemented a convolutional neural network that is able to perform
multi-class classification tasks! Nice job!
Keep it up!
https://www.coursera.org/learn/convolutional-neural-networks-tensorflow/programming/4H8AV/classification-beyond-two-classes/lab?path=%2Flab%2… 8/8