Visualizing Activation Functions - Lab

Introduction

Now that you've built your own CNN and seen how to visualize feature maps, its time to practice loading a pretrained model from file and visualize the learned features systematically. In this lab, you'll expand upon the code from the previous lesson in order to succinctly visualize all the channels from each layer in a CNN.

Objectives

In this lab you will:

  • Load a saved Keras model
  • Use Keras methods to visualize the activation functions in CNNs

Load a Model

For this lab, load the saved model 'chest_xray_all_with_augmentation_data.h5'. This saved file includes both the model architecture and the trained weights. See the model.save() method for further details. The model was built in order to help identify patients with pneumonia. Start simply by loading the model and pulling up a summary of the layers. (To load the model use the keras.models.load_model() function.)

# Your code here

Load an Image

Before you plot the learned representations of the convolutional base, let's import an image and display it prior to processing. This will provide a comparison to the transformations formed by the model's feature maps.

Load and display the image 'person3_virus_16.jpeg'.

# Your code here

Transform the Image to a Tensor and Visualize Again

Recall that you should always preprocess images into tensors when using deep learning. As such, preprocess this image and then redisplay the tensor.

# Your code here

Plot Feature Maps

Now that you've loaded a model, practice visualizing each of the channels for each of feature maps of the convolutional layers. Recall that this process will take a few steps. First, extract the feature maps, or layer outputs from each of the activation functions in the model. From there, generate models that transform the image from its raw state to these feature maps. You can then take these transformations and visualize each channel for each feature map.

To preview the results of the solution code, take a sneek peak at the Intermediate_Activations_Visualized.pdf file.

# Your code here

Summary

Nice work! In this lab, you practiced loading a model and then visualizing the activation feature maps learned by that model on your data! In the upcoming labs and sections you will build upon the first part of this and see how you can adapt the representations learned by more experienced models to your own applications which may have limited training data.