<jupyter_start><jupyter_text>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.<jupyter_code>from tensorflow import keras

base_model = keras.applications.VGG16(
    weights=FIXME,
    input_shape=(224, 224, 3),
    include_top=FIXME)<jupyter_output><empty_output><jupyter_text>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.<jupyter_code># Freeze base model
base_model.trainable = FIXME<jupyter_output><empty_output><jupyter_text>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.<jupyter_code># 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)
model.summary()<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>model.compile(loss = FIXME , metrics = FIXME)<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(FIXME)<jupyter_output><empty_output><jupyter_text>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).<jupyter_code># 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")<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>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.<jupyter_code># Unfreeze the base model
base_model.trainable = FIXME

# Compile the model with a low learning rate
model.compile(optimizer=keras.optimizers.RMSprop(learning_rate = FIXME),
              loss = FIXME , metrics = FIXME)
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)<jupyter_output><empty_output><jupyter_text>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`.<jupyter_code>model.evaluate(valid_it, steps=valid_it.samples/valid_it.batch_size)<jupyter_output><empty_output><jupyter_text>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`.<jupyter_code>from run_assessment import run_assessment
run_assessment(model, valid_it)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06, 'fraction_numerical': 0.006482758620689655, 'mean_word_length': 4.427395209580839, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '583690', 'license': 'MIT', 'n_tokens_mistral': 2013, 'n_tokens_neox': 1918, 'n_words': 853, 'path': '07_assessment.ipynb', 'repo_name': 'edgaradel/IntroductionDeepLearning', 'size': '7250'}