Assessment: The Dataset
Assessment: The Dataset
Assessment
Congratulations on going through today's course! Hopefully, you've learned some valuable skills along the
way and had fun doing it. Now it's time to put those skills to the test. In this assessment, you will train a new
model that is able to recognize fresh and rotten fruit. You will need to get the model to a validation accuracy of
92% in order to pass the assessment, though we challenge you to do even better if you can. You will have
the use the skills that you learned in the previous exercises. Specifically, we suggest using some combination
of transfer learning, data augmentation, and fine tuning. Once you have trained the model to be at least 92%
accurate on the validation dataset, save your model, and then assess its accuracy. Let's get started!
The Dataset
In this exercise, you will train a model to recognize fresh and rotten fruits. The dataset comes from Kaggle
(https://www.kaggle.com/sriramr/fruits-fresh-and-rotten-for-classification), a great place to go if you're
interested in starting a project after this class. The dataset structure is in the data/fruits folder. There
are 6 categories of fruits: fresh apples, fresh oranges, fresh bananas, rotten apples, rotten oranges, and
rotten bananas. This will mean that your model will require an output layer of 6 neurons to do the
categorization successfully. You'll also need to compile the model with categorical_crossentropy , as
we have more than two categories.
We encourage you to start with a model pretrained on ImageNet. Load the model with the correct weights, set
an input shape, and choose to remove the last layers of the model. Remember that images have three
dimensions: a height, and width, and a number of channels. Because these pictures are in color, there will be
three channels for red, green, and blue. We've filled in the input shape for you. This cannot be changed or the
assessment will fail. If you need a reference for setting up the pretrained model, please take a look at
notebook 05b (05b_presidential_doggy_door.ipynb) where we implemented transfer learning.
1 of 5 12/07/2021, 05:02 pm
07_assessment about:srcdoc
base_model = keras.applications.VGG16(
weights=FIXME,
input_shape=(224, 224, 3),
include_top=FIXME)
Next, we suggest freezing the base model, as done in notebook 05b (05b_presidential_doggy_door.ipynb).
This is done so that all the learning from the ImageNet dataset does not get destroyed in the initial training.
Now it's time to add layers to the pretrained model. Notebook 05b (05b_presidential_doggy_door.ipynb) can
be used as a guide. Pay close attention to the last dense layer and make sure it has the correct number of
neurons to classify the different types of fruit.
x = base_model(inputs, training=False)
In [ ]: model.summary()
Compile Model
Now it's time to compile the model with loss and metrics options. Remember that we're training on a number
of different categories, rather than a binary classification problem.
2 of 5 12/07/2021, 05:02 pm
07_assessment about:srcdoc
If you'd like, try to augment the data to improve the dataset. Feel free to look at notebook 04a
(04a_asl_augmentation.ipynb) and notebook 05b (05b_presidential_doggy_door.ipynb) for augmentation
examples. There is also documentation for the Keras ImageDataGenerator class (https://keras.io
/api/preprocessing/image/#imagedatagenerator-class). This step is optional, but it may be helpful to get to
92% accuracy.
datagen = ImageDataGenerator(FIXME)
Load Dataset
Now it's time to load the train and validation datasets. Pick the right folders, as well as the right
target_size of the images (it needs to match the height and width input of the model you've created). If
you'd like a reference, you can check out notebook 05b (05b_presidential_doggy_door.ipynb).
Time to train the model! Pass the train and valid iterators into the fit function, as well as setting
your desired number of epochs.
In [ ]: model.fit(FIXME,
validation_data=FIXME,
steps_per_epoch=train_it.samples/train_it.batch_size,
validation_steps=valid_it.samples/valid_it.batch_size,
epochs=FIXME)
3 of 5 12/07/2021, 05:02 pm
07_assessment about:srcdoc
If you have reached 92% validation accuracy already, this next step is optional. If not, we suggest fine tuning
the model with a very low learning rate.
In [ ]: model.fit(FIXME,
validation_data=FIXME,
steps_per_epoch=train_it.samples/train_it.batch_size,
validation_steps=valid_it.samples/valid_it.batch_size,
epochs=FIXME)
Hopefully, you now have a model that has a validation accuracy of 92% or higher. If not, you may want to go
back and either run more epochs of training, or adjust your data augmentation.
Once you are satisfied with the validation accuracy, evaluate the model by executing the following cell. The
evaluate function will return a tuple, where the first value is your loss, and the second value is your accuracy.
To pass, the model will need have an accuracy value of 92% or higher .
In [ ]: model.evaluate(valid_it, steps=valid_it.samples/valid_it.batch_size)
NOTE: run_assessment assumes your model is named model and your validation data iterator is called
valid_it . If for any reason you have modified these variable names, please update the names of the
arguments passed to run_assessment .
In [ ]: run_assessment(model, valid_it)
4 of 5 12/07/2021, 05:02 pm
07_assessment about:srcdoc
Generate a Certificate
If you passed the assessment, please return to the course page (shown below) and click the "ASSESS TASK"
button, which will generate your certificate for the course.
5 of 5 12/07/2021, 05:02 pm