Code

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

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
## The Dataset
In this exercise, you will train a model to recognize fresh and rotten fruits.
The dataset comes from Kaggle, 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.

Image
Load ImageNet Base Model
## 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 where we
implemented transfer learning.

from tensorflow import keras

base_model = keras.applications.VGG16(
weights='imagenet',
input_shape=(224, 224, 3),
include_top=False)
from tensorflow import keras

base_model = keras.applications.VGG16(
weights='imagenet',
input_shape=(224, 224, 3),
include_top=False)
sumary()
base_model.summary()
Model: "vgg16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_pool (MaxPooling2D) (None, 7, 7, 512) 0
=================================================================
Total params: 14,714,688
Trainable params: 14,714,688
Non-trainable params: 0
_________________________________________________________________
Freeze Base Model
Next, we suggest freezing the base model, as done in notebook 05b. This is done
so that all the learning from the ImageNet dataset does not get destroyed in the
initial training.

# Freeze base model


base_model.trainable = False
Add Layers to Model
## Add Layers to Model
Now it's time to add layers to the pretrained model. Notebook 05b 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.

# Create inputs with correct shape


inputs = keras.Input(shape=(224, 224, 3))

x = base_model(inputs, training=False)

# Add pooling layer or flatten layer


x = keras.layers.GlobalAveragePooling2D()(x)

# Add final dense layer


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

# Combine inputs and outputs to create model


model = keras.Model(inputs,outputs)
summary()
model.summary()
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 224, 224, 3)] 0
_________________________________________________________________
vgg16 (Model) (None, 7, 7, 512) 14714688
_________________________________________________________________
global_average_pooling2d (Gl (None, 512) 0
_________________________________________________________________
dense (Dense) (None, 6) 3078
=================================================================
Total params: 14,717,766
Trainable params: 3,078
Non-trainable params: 14,714,688
_________________________________________________________________
Compile Model
## 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.

model.compile(loss=keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=[keras.metrics.CategoricalAccuracy()])
model.compile(loss=keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=[keras.metrics.CategoricalAccuracy()])
## Augment the Data
If you'd like, try to augment the data to improve the dataset. Feel free to look
at notebook 04a and notebook 05b for augmentation examples. There is also
documentation for the Keras ImageDataGenerator class. This step is optional, but
it may be helpful to get to 92% accuracy.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen_train = ImageDataGenerator(
samplewise_center=True, # set each sample mean to 0
rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range=0.1, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of
total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of
total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False,
) # we don't expect Bo to be upside-down so we will not flip vertically

# No need to augment validation data


datagen_valid = ImageDataGenerator(samplewise_center=True)
from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen_train = ImageDataGenerator(
samplewise_center=True, # set each sample mean to 0
rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180)
zoom_range=0.1, # Randomly zoom image
width_shift_range=0.1, # randomly shift images horizontally (fraction of
total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of
total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False,
) # we don't expect Bo to be upside-down so we will not flip vertically

# No need to augment validation data


datagen_valid = ImageDataGenerator(samplewise_center=True)
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). For a reference, check out notebook
05b.

# load and iterate training dataset


train_it = datagen_train.flow_from_directory(
"data/fruits/train",
target_size=(224, 224),
color_mode="rgb",
class_mode="categorical",
)
# load and iterate validation dataset
valid_it = datagen_valid.flow_from_directory(
"data/fruits/valid",
target_size=(224, 224),
color_mode="rgb",
class_mode="categorical",
)
Found 1182 images belonging to 6 classes.
Found 329 images belonging to 6 classes.
Train the Model
Time to train the model! Pass the train and valid iterators into the fit
function, as well as setting the desired number of epochs.

model.fit(train_it,
validation_data=valid_it,
steps_per_epoch=train_it.samples/train_it.batch_size,
validation_steps=valid_it.samples/valid_it.batch_size,
epochs=20)
Epoch 1/20
37/36 [==============================] - 26s 712ms/step - loss: 1.6059 -
categorical_accuracy: 0.4272 - val_loss: 1.4281 - val_categorical_accuracy:
0.6231
Epoch 2/20
37/36 [==============================] - 17s 460ms/step - loss: 1.3729 -
categorical_accuracy: 0.6827 - val_loss: 1.3399 - val_categorical_accuracy:
0.7082
Epoch 3/20
37/36 [==============================] - 17s 465ms/step - loss: 1.3108 -
categorical_accuracy: 0.7343 - val_loss: 1.3325 - val_categorical_accuracy:
0.7082
Epoch 4/20
37/36 [==============================] - 17s 463ms/step - loss: 1.2945 -
categorical_accuracy: 0.7462 - val_loss: 1.2802 - val_categorical_accuracy:
0.7660
Epoch 5/20
37/36 [==============================] - 17s 465ms/step - loss: 1.2862 -
categorical_accuracy: 0.7572 - val_loss: 1.2959 - val_categorical_accuracy:
0.7508
Epoch 6/20
37/36 [==============================] - 17s 467ms/step - loss: 1.2755 -
categorical_accuracy: 0.7690 - val_loss: 1.2879 - val_categorical_accuracy:
0.7538
Epoch 7/20
37/36 [==============================] - 17s 469ms/step - loss: 1.2701 -
categorical_accuracy: 0.7733 - val_loss: 1.2607 - val_categorical_accuracy:
0.7751
Epoch 8/20
37/36 [==============================] - 17s 464ms/step - loss: 1.2315 -
categorical_accuracy: 0.8164 - val_loss: 1.1881 - val_categorical_accuracy:
0.8480
Epoch 9/20
37/36 [==============================] - 17s 462ms/step - loss: 1.1358 -
categorical_accuracy: 0.9162 - val_loss: 1.1488 - val_categorical_accuracy:
0.8997
Epoch 10/20
37/36 [==============================] - 17s 466ms/step - loss: 1.1100 -
categorical_accuracy: 0.9442 - val_loss: 1.1201 - val_categorical_accuracy:
0.9331
Epoch 11/20
37/36 [==============================] - 17s 464ms/step - loss: 1.0948 -
categorical_accuracy: 0.9543 - val_loss: 1.1124 - val_categorical_accuracy:
0.9392
Epoch 12/20
37/36 [==============================] - 17s 467ms/step - loss: 1.0860 -
categorical_accuracy: 0.9653 - val_loss: 1.1145 - val_categorical_accuracy:
0.9301
Epoch 13/20
37/36 [==============================] - 17s 467ms/step - loss: 1.0812 -
categorical_accuracy: 0.9695 - val_loss: 1.1137 - val_categorical_accuracy:
0.9271
Epoch 14/20
37/36 [==============================] - 17s 466ms/step - loss: 1.0720 -
categorical_accuracy: 0.9763 - val_loss: 1.0911 - val_categorical_accuracy:
0.9514
Epoch 15/20
37/36 [==============================] - 17s 468ms/step - loss: 1.0705 -
categorical_accuracy: 0.9763 - val_loss: 1.1043 - val_categorical_accuracy:
0.9422
Epoch 16/20
37/36 [==============================] - 17s 468ms/step - loss: 1.0666 -
categorical_accuracy: 0.9805 - val_loss: 1.0828 - val_categorical_accuracy:
0.9574
Epoch 17/20
37/36 [==============================] - 17s 467ms/step - loss: 1.0670 -
categorical_accuracy: 0.9805 - val_loss: 1.0965 - val_categorical_accuracy:
0.9392
Epoch 18/20
37/36 [==============================] - 17s 468ms/step - loss: 1.0638 -
categorical_accuracy: 0.9848 - val_loss: 1.0849 - val_categorical_accuracy:
0.9605
Epoch 19/20
37/36 [==============================] - 17s 463ms/step - loss: 1.0602 -
categorical_accuracy: 0.9873 - val_loss: 1.0830 - val_categorical_accuracy:
0.9605
Epoch 20/20
37/36 [==============================] - 17s 463ms/step - loss: 1.0621 -
categorical_accuracy: 0.9856 - val_loss: 1.0872 - val_categorical_accuracy:
0.9574
<tensorflow.python.keras.callbacks.History at 0x7f05d4b0f828>
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.

# Unfreeze the base model


base_model.trainable = True

# Compile the model with a low learning rate


model.compile(optimizer=keras.optimizers.RMSprop(learning_rate = .00001), #
Very low learning rate
loss=keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=[keras.metrics.CategoricalAccuracy()])
model.fit(train_it,
validation_data=valid_it,
steps_per_epoch=train_it.samples/train_it.batch_size,
validation_steps=valid_it.samples/valid_it.batch_size,
epochs=20)
Epoch 1/20
37/36 [==============================] - 29s 779ms/step - loss: 1.0849 -
categorical_accuracy: 0.9602 - val_loss: 1.0873 - val_categorical_accuracy:
0.9574
Epoch 2/20
37/36 [==============================] - 19s 516ms/step - loss: 1.0662 -
categorical_accuracy: 0.9805 - val_loss: 1.0754 - val_categorical_accuracy:
0.9696
Epoch 3/20
37/36 [==============================] - 18s 486ms/step - loss: 1.0529 -
categorical_accuracy: 0.9932 - val_loss: 1.0740 - val_categorical_accuracy:
0.9696
Epoch 4/20
37/36 [==============================] - 18s 488ms/step - loss: 1.0606 -
categorical_accuracy: 0.9839 - val_loss: 1.0743 - val_categorical_accuracy:
0.9635
Epoch 5/20
37/36 [==============================] - 18s 490ms/step - loss: 1.0523 -
categorical_accuracy: 0.9924 - val_loss: 1.1083 - val_categorical_accuracy:
0.9301
Epoch 6/20
37/36 [==============================] - 18s 498ms/step - loss: 1.0562 -
categorical_accuracy: 0.9890 - val_loss: 1.0730 - val_categorical_accuracy:
0.9726
Epoch 7/20
37/36 [==============================] - 18s 477ms/step - loss: 1.0545 -
categorical_accuracy: 0.9898 - val_loss: 1.0730 - val_categorical_accuracy:
0.9696
Epoch 8/20
37/36 [==============================] - 18s 492ms/step - loss: 1.0510 -
categorical_accuracy: 0.9932 - val_loss: 1.0929 - val_categorical_accuracy:
0.9514
Epoch 9/20
37/36 [==============================] - 18s 493ms/step - loss: 1.0474 -
categorical_accuracy: 0.9975 - val_loss: 1.0488 - val_categorical_accuracy:
0.9970
Epoch 10/20
37/36 [==============================] - 18s 482ms/step - loss: 1.0480 -
categorical_accuracy: 0.9958 - val_loss: 1.0680 - val_categorical_accuracy:
0.9726
Epoch 11/20
37/36 [==============================] - 19s 506ms/step - loss: 1.0476 -
categorical_accuracy: 0.9966 - val_loss: 1.0821 - val_categorical_accuracy:
0.9635
Epoch 12/20
37/36 [==============================] - 18s 489ms/step - loss: 1.0580 -
categorical_accuracy: 0.9848 - val_loss: 1.0673 - val_categorical_accuracy:
0.9787
Epoch 13/20
37/36 [==============================] - 18s 488ms/step - loss: 1.0493 -
categorical_accuracy: 0.9941 - val_loss: 1.0704 - val_categorical_accuracy:
0.9726
Epoch 14/20
37/36 [==============================] - 18s 486ms/step - loss: 1.0462 -
categorical_accuracy: 0.9975 - val_loss: 1.0697 - val_categorical_accuracy:
0.9726
Epoch 15/20
37/36 [==============================] - 18s 491ms/step - loss: 1.0515 -
categorical_accuracy: 0.9932 - val_loss: 1.0659 - val_categorical_accuracy:
0.9787
Epoch 16/20
37/36 [==============================] - 18s 483ms/step - loss: 1.0516 -
categorical_accuracy: 0.9932 - val_loss: 1.0580 - val_categorical_accuracy:
0.9848
Epoch 17/20
37/36 [==============================] - 18s 482ms/step - loss: 1.0531 -
categorical_accuracy: 0.9907 - val_loss: 1.0941 - val_categorical_accuracy:
0.9483
Epoch 18/20
37/36 [==============================] - 18s 481ms/step - loss: 1.0453 -
categorical_accuracy: 0.9983 - val_loss: 1.0532 - val_categorical_accuracy:
0.9909
Epoch 19/20
37/36 [==============================] - 18s 488ms/step - loss: 1.0471 -
categorical_accuracy: 0.9975 - val_loss: 1.0642 - val_categorical_accuracy:
0.9787
Epoch 20/20
37/36 [==============================] - 19s 504ms/step - loss: 1.0479 -
categorical_accuracy: 0.9958 - val_loss: 1.0567 - val_categorical_accuracy:
0.9848
<tensorflow.python.keras.callbacks.History at 0x7f05bf7ff470>
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.
)
model.evaluate(valid_it, steps=valid_it.samples/valid_it.batch_size)
11/10 [================================] - 1s 134ms/step - loss: 1.0665 -
categorical_accuracy: 0.9757
[1.066458821296692, 0.975683867931366]
Run the Assessment
## 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`.
from run_assessment import run_assessment
run_assessment(model, valid_it)
Evaluating model 5 times to obtain average accuracy...

11/10 [================================] - 1s 132ms/step - loss: 1.0571 -


categorical_accuracy: 0.9848
11/10 [================================] - 1s 133ms/step - loss: 1.0693 -
categorical_accuracy: 0.9726
11/10 [================================] - 2s 144ms/step - loss: 1.0632 -
categorical_accuracy: 0.9787
11/10 [================================] - 1s 127ms/step - loss: 1.0632 -
categorical_accuracy: 0.9787
11/10 [================================] - 2s 139ms/step - loss: 1.0609 -
categorical_accuracy: 0.9818

Accuracy required to pass the assessment is 0.92 or greater.


Your average accuracy is 0.9793.

Congratulations! You passed the assessment!


See instructions below to generate a certificate.
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.

Image

You might also like