What can your model see?

Ridwan Amure
Analytics Vidhya
Published in
4 min readMar 30, 2021

Visualize your image dataset from the perspective of your computer vision model

Photo by Christopher Gower on Unsplash

The process of building computer vision models is computationally intensive and can become expensive with more training time. However, improving the accuracy of models is another daunting task that most practitioners dread. Another problem that usually arises or that is common with computer vision models or neural networks, in general, is when the model performed well during training and validation but performs not-too-good in production. To correctly debug this problem, you need to be able to visualize what your model is looking at.

There are methods of inspecting the filters learned by the convolution layers. These visualization techniques can be used to debug a computer vision model. Let’s examine some of them.

Visualizing Intermediate Activations

This is a method of visualizing the feature maps that are produced by each convolution layer and its corresponding pooling. Given certain input (image, in our case), the layer will produce an output, these outputs are called the activation of that particular layer. The output can be visualized as a 2D image.

The PlantVillage disease dataset was used in training the model used in this article.

Let's get right to it

# import the required modulesfrom keras import layers
from keras import models
from keras import optimizers
from keras import backend as Kimport cv2
import tensorflow as tf

This is what the model looks like

model.summary()

Let’s visualize our test image

We’ll build a Keras model that takes batches of images as input and outputs the activations of all convolution and pooling layers in order to retrieve the function maps you want to look at. We’ll use the Keras class Model to do this. An input tensor (or a list of input tensors) and an output tensor are used to create a model (or list of output tensors). The resulting class is a Keras model, which works similarly to Sequential models in that it maps specified inputs to specified outputs. The Model class differs from Sequential in that it allows for models with different outputs¹.

layer_outputs = [layer.output for layer in model.layers[:8]] #Extract the top 8 layers
activation_model = models.Model(inputs=model.input, outputs=layer_outputs) # Creates a model that will return these outputs, given the model input

This model returns the values of the layer activations in the original model when given an image input.

img_tensor=np.expand_dims(image, axis=0) # expanding the image dimension
activations = activation_model.predict(img_tensor)
first_layer_activation = activations[0]
Visualizing the fourth channel

Let's visualize all the channels at once to have a general overview of the changes that went on under the hood.

The sparsity of the activations increases with layer depth: in the first layer, the input image activates all filters; but, as the layers advance, more and more filters become null. This suggests that the filter’s pattern was not found in the input image.

We’ve just shown an essential universal property of deep neural network representations: the features derived by a layer become increasingly abstract as the layer’s depth increases. Higher layer activations hold less and less information about the real input that is being seen and more and more information about the target. The neural network act as a separator of different information encoded in the input image.

This is similar to way to the human view and conceptualized information

Visualizing heatmaps of class Activation

This visualization is important for understanding which parts of a given image led a convnet to its final classification decision. This is important in identifying “what the model is looking at”. It consists of producing heatmaps of class activation over input images. A class activation heatmap is a 2D grid of scores associated with a specific output class, computed for every location in any input image, indicating how important each location is with respect to the class under consideration¹.

The implementation is shown below

The heat maps will answer the following question:

  • Why does the model classify the image as a certain class? In this case bacterial spot.
  • Where are the leaves affected by the bacterial spots?

These visualization techniques can be used to explain the performance of a model. Once a model can pass through this stage successfully, the confidence in the performance of the model when deployed to production will definitely increase.

1.François Chollet (2018). Deep Learning with python. Shelter Island: Manning Publications Co.

--

--

Ridwan Amure
Analytics Vidhya

A data scientist with love for research and development.