Path: blob/main/C2/W1/assignment/C2W1_Assignment.ipynb
2955 views
Week 1: Using CNN's with the Cats vs Dogs Dataset
Welcome to the 1st assignment of the course! This week, you will be using the famous Cats vs Dogs
dataset to train a model that can classify images of dogs from images of cats. For this, you will create your own Convolutional Neural Network in Tensorflow and leverage Keras' image preprocessing utilities.
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!
It is a good start to know how many images there are of each class but let's actually plot some of them to get a better sense of the kinds of images in the dataset:
These sure are cute! Notice that these images come in all kinds of resolutions!
Exercise 1: train_val_datasets
Now that you are familiar with the raw data it is time for you to create the datasets that will yield batches for both for training and validation. For this, complete the train_val_datasets
function below.
Hints::
It is recommended to read the documentation for
tf.keras.utils.image_dataset_from_directory
since this function provides a lot of functionalities.You should already know the directory in which the images are stored. You can either use the global variable defined earlier or hardcode the path.
Remember that the images in this dataset come in a variety of resolutions. This can be standardized by defining the
image_size
parameter which will be used to convert each image to this target resolution. For this exercise, use aimage_size
of (150, 150).It is recommended to use a
batch_size
of 128 as it yields a good trade-off between training times and memory usage.You should select an appropriate value for the
label_mode
parameter given that you are dealing with binary classification.Since the images haven't been splitted into training and validation sets you should use the
validation_split
parameter for this purpose. You must use 10% of the images as validation dataset.When using the argument
validation_split
, there are two other arguments that must be passed otherwise an exception will be thrown. They are:seed
, a random seed to avoid overlapping files in training and validation (any positive integer is fine)
subset
, which subset should be returned (training, validation or both). You must use the correct value given the context.
Expected Output:
Now you have two datasets, one for training and another one for validation. In the ungraded labs you applied some extra transformations to the datasets by using methods of tf.data.Dataset
such as prefetch or cache. These help training be faster but they come at the expense of more memory usage and due to memory limitations of this environment, you will not use them in this assignment.
Before proceeding take some time to inspect a batch of the training set, you can do this using the take
method from a tf.data.Dataset
while specifying how many batches you want to get. Using this method will yield a tuple with two elements, the first one being the images in the batch and the second one, their respective labels.
Notice that the pixel values of the images have not yet been normalized at this point so you must add a Rescaling layer in your model, to apply a factor of 1./255 to these values.
Exercise 2: create_model
With the data ready, your next task is to define the architecture of the model that will be trained. Complete the create_model
function below.
Aside from defining the architecture of the model, you should also compile it so make sure to use a loss
function that is compatible with the label_mode
you defined in the previous exercise, which should also be compatible with the last layer of your network. You can tell if they aren't compatible if you get an error during training.
Hints:
You should use at least 3 convolution layers to achieve the desired performance. This is a necessary condition for this function to pass the grading.
The FIRST layer must be a tf.keras.layers.Input with the appropriate shape argument.
The SECOND layer of your model MUST be the rescaling layer, i.e,
tf.keras.layers.Rescaling
, with the suitable rescaling parameter, as discussed above.Be mindful about the last layer and its activation function, as it will impact the loss you must use.
Notice that in this assignment you will define the Rescaling Layer within the model rather than as a pre-processing step. This can be done either way but including this layer within the model allows you to preserve this step when saving the model for latter use.
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 architecture you used is compatible with the dataset (you can ignore the warnings prompted by using the GPU):
Expected Output:
Where batch_size
is the one you defined and n_units
is the number of units of the last layer of your model.
Exercise 3: EarlyStoppingCallback
With the model's architecture now ready, it is time for you to define a callback to stop training if the following conditions are satisfied:
Training accuracy greater than or equal to 95%
Validation accuracy greater than or equal to 80%
For this, complete the EarlyStoppingCallback
class below. Remember from course 1 that you must create a class that inherits from tf.keras.callbacks.Callback
and you must add a method called on_epoch_end
with the correct signature to add the stop conditions.
Hints:
You may look at the documentation for
tf.keras.callbacks.Callback
Whenever you compile a model with a metric and provide validation data when training that model, TensorFlow will automatically create another metric and append
val_
to its name. Since your model was compiled with theaccuracy
metric and you are using a validation dataset, you will have access to a metric calledval_accuracy
You will probably encounter that the model is overfitting, which means that it is doing a great job at classifying the images in the training set but struggles with new data. This is perfectly fine and you will learn how to mitigate this issue in the upcoming week.
To pass this assignment your model should have achieved a training accuracy of 95% and a validation accuracy of 80%. If your model didn't achieve these thresholds, try training again with a different model architecture and remember to use at least 3 convolutional layers.
Before submitting your assignment, please run the following code to save your training history as it will be used in grading.
Congratulations on finishing this week's assignment!
You have successfully implemented a convolutional neural network that classifies images of cats and dogs, along with the helper functions needed to pre-process the images!
Keep it up!