0% found this document useful (0 votes)
337 views

Assessment: The Dataset

The document provides instructions for an assessment to train a model to recognize fresh and rotten fruit. It describes loading a pretrained VGG16 model, adding and compiling additional layers, augmenting the dataset, training the model for multiple epochs, fine-tuning the model, evaluating it on the validation set, and running an assessment on the model to check if it achieves over 92% accuracy.

Uploaded by

Praveen Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
337 views

Assessment: The Dataset

The document provides instructions for an assessment to train a model to recognize fresh and rotten fruit. It describes loading a pretrained VGG16 model, adding and compiling additional layers, augmenting the dataset, training the model for multiple epochs, fine-tuning the model, evaluating it on the validation set, and running an assessment on the model to check if it achieves over 92% accuracy.

Uploaded by

Praveen Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

07_assessment about:srcdoc

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.

Load ImageNet Base Model

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

In [ ]: from tensorflow import keras

base_model = keras.applications.VGG16(
weights=FIXME,
input_shape=(224, 224, 3),
include_top=FIXME)

Freeze Base Model

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.

In [ ]: # Freeze base model


base_model.trainable = FIXME

Add Layers to Model

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.

In [ ]: # Create inputs with correct shape


inputs = FIXME

x = base_model(inputs, training=False)

# Add pooling layer or flatten layer


x = FIXME

# Add final dense layer


outputs = keras.layers.Dense(FIXME, activation = 'softmax')(x)

# Combine inputs and outputs to create model


model = keras.Model(FIXME)

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

In [ ]: model.compile(loss = FIXME , metrics = FIXME)

Augment the Data

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.

In [ ]: from tensorflow.keras.preprocessing.image import ImageDataGenerator

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).

In [ ]: # load and iterate training dataset


train_it = datagen.flow_from_directory(FIXME,
target_size=FIXME,
color_mode='rgb',
class_mode="categorical")
# load and iterate validation dataset
valid_it = datagen.flow_from_directory(FIXME,
target_size=FIXME,
color_mode='rgb',
class_mode="categorical")

Train the Model

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

Unfreeze Model for Fine Tuning

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 [ ]: # Unfreeze the base model


base_model.trainable = FIXME

# Compile the model with a low learning rate


model.compile(optimizer=keras.optimizers.RMSprop(learning_rate = FIXM
E),
loss = FIXME , metrics = FIXME)

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)

Evaluate the Model

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)

Run the Assessment

To assess your model run the following two cells.

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 [ ]: from run_assessment import 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy