This notebook contains the code samples found in Chapter 5, Section 4 of Deep Learning with R. Note that the original text features far more content, in particular further explanations and figures: in this notebook, you will only find source code and related comments.
It is often said that deep learning models are “black boxes”, learning representations that are difficult to extract and present in a human-readable form. While this is partially true for certain types of deep learning models, it is definitely not true for convnets. The representations learned by convnets are highly amenable to visualization, in large part because they are representations of visual concepts. Since 2013, a wide array of techniques have been developed for visualizing and interpreting these representations. We won’t survey all of them, but we will cover three of the most accessible and useful ones:
For the first method – activation visualization – we will use the small convnet that we trained from scratch on the cat vs. dog classification problem two sections ago. For the next two methods, we will use the VGG16 model that we introduced in the previous section.
Visualizing intermediate activations consists in displaying the feature maps that are output by various convolution and pooling layers in a network, given a certain input (the output of a layer is often called its “activation”, the output of the activation function). This gives a view into how an input is decomposed unto the different filters learned by the network. These feature maps we want to visualize have 3 dimensions: width, height, and depth (channels). Each channel encodes relatively independent features, so the proper way to visualize these feature maps is by independently plotting the contents of every channel, as a 2D image. Let’s start by loading the model that we saved in section 5.2:
library(keras)
model <- load_model_hdf5("cats_and_dogs_small_2.h5")
summary(model) # As a reminder.
______________________________________________________________________________________
Layer (type) Output Shape Param #
======================================================================================
conv2d_21 (Conv2D) (None, 148, 148, 32) 896
______________________________________________________________________________________
max_pooling2d_21 (MaxPooling2D) (None, 74, 74, 32) 0
______________________________________________________________________________________
conv2d_22 (Conv2D) (None, 72, 72, 64) 18496
______________________________________________________________________________________
max_pooling2d_22 (MaxPooling2D) (None, 36, 36, 64) 0
______________________________________________________________________________________
conv2d_23 (Conv2D) (None, 34, 34, 128) 73856
______________________________________________________________________________________
max_pooling2d_23 (MaxPooling2D) (None, 17, 17, 128) 0
______________________________________________________________________________________
conv2d_24 (Conv2D) (None, 15, 15, 128) 147584
______________________________________________________________________________________
max_pooling2d_24 (MaxPooling2D) (None, 7, 7, 128) 0
______________________________________________________________________________________
flatten_6 (Flatten) (None, 6272) 0
______________________________________________________________________________________
dropout_3 (Dropout) (None, 6272) 0
______________________________________________________________________________________
dense_11 (Dense) (None, 512) 3211776
______________________________________________________________________________________
dense_12 (Dense) (None, 1) 513
======================================================================================
Total params: 3,453,121
Trainable params: 3,453,121
Non-trainable params: 0
______________________________________________________________________________________
This will be the input image we will use – a picture of a cat, not part of images that the network was trained on:
img_path <- "~/Downloads/cats_and_dogs_small/test/cats/cat.1700.jpg"
# We preprocess the image into a 4D tensor
img <- image_load(img_path, target_size = c(150, 150))
img_tensor <- image_to_array(img)
img_tensor <- array_reshape(img_tensor, c(1, 150, 150, 3))
# Remember that the model was trained on inputs
# that were preprocessed in the following way:
img_tensor <- img_tensor / 255
dim(img_tensor)
[1] 1 150 150 3
Let’s display our picture:
plot(as.raster(img_tensor[1,,,]))
In order to extract the feature maps you want to look at, you’ll create a Keras model that takes batches of images as input, and outputs the activations of all convolution and pooling layers. To do this, we will use the keras_model()
function, which takes two arguments: an input tensor (or list of input tensors) and an output tensor (or list of output tensors). The resulting class is a Keras model, just like the ones created by the keras_sequential_model()
function that you are familiar with, mapping the specified inputs to the specified outputs. What sets this type of model apart apart is that it allows for models with multiple outputs (unlike keras_sequential_model()
). For more information about creating models with the keras_model()
function, see section 7.1.
# Extracts the outputs of the top 8 layers:
layer_outputs <- lapply(model$layers[1:8], function(layer) layer$output)
# Creates a model that will return these outputs, given the model input:
activation_model <- keras_model(inputs = model$input, outputs = layer_outputs)
When fed an image input, this model returns the values of the layer activations in the original model. This is the first time you encounter a multi-output model in this book: until now the models you have seen only had exactly one input and one output. In the general case, a model could have any number of inputs and outputs. This one has one input and 8 outputs, one output per layer activation.
# Returns a list of five arrays: one array per layer activation
activations <- activation_model %>% predict(img_tensor)
For instance, this is the activation of the first convolution layer for our cat image input:
first_layer_activation <- activations[[1]]
dim(first_layer_activation)
[1] 1 148 148 32
It’s a 148 x 148 feature map with 32 channels. Let’s visualize some of them. First we define an R function that will plot a channel:
plot_channel <- function(channel) {
rotate <- function(x) t(apply(x, 2, rev))
image(rotate(channel), axes = FALSE, asp = 1,
col = terrain.colors(12))
}
Let’s try visualizing the 5th channel:
plot_channel(first_layer_activation[1,,,5])
This channel appears to encode some sort of edge detector. Let’s try the 7th channel – but note that your own channels may vary, since the specific filters learned by convolution layers are not deterministic.
plot_channel(first_layer_activation[1,,,7])
This channel is subtly different, and unlike the 5th channel seems to be picking up the iris of the cat’s eye. At this point, let’s go and plot a complete visualization of all the activations in the network. We’ll extract and plot every channel in each of our 8 activation maps, and we will stack the results in one big image tensor, with channels stacked side by side.
dir.create("cat_activations")
image_size <- 58
images_per_row <- 16
for (i in 1:8) {
layer_activation <- activations[[i]]
layer_name <- model$layers[[i]]$name
n_features <- dim(layer_activation)[[4]]
n_cols <- n_features %/% images_per_row
png(paste0("cat_activations/", i, "_", layer_name, ".png"),
width = image_size * images_per_row,
height = image_size * n_cols)
op <- par(mfrow = c(n_cols, images_per_row), mai = rep_len(0.02, 4))
for (col in 0:(n_cols-1)) {
for (row in 0:(images_per_row-1)) {
channel_image <- layer_activation[1,,,(col*images_per_row) + row + 1]
plot_channel(channel_image)
}
}
par(op)
dev.off()
}
A few remarkable things to note here:
We have just evidenced a very important universal characteristic of the representations learned by deep neural networks: the features extracted by a layer get increasingly abstract with the depth of the layer. The activations of layers higher-up carry less and less information about the specific input being seen, and more and more information about the target (in our case, the class of the image: cat or dog). A deep neural network effectively acts as an information distillation pipeline, with raw data going in (in our case, RBG pictures), and getting repeatedly transformed so that irrelevant information gets filtered out (e.g. the specific visual appearance of the image) while useful information get magnified and refined (e.g. the class of the image).
This is analogous to the way humans and animals perceive the world: after observing a scene for a few seconds, a human can remember which abstract objects were present in it (e.g. bicycle, tree) but could not remember the specific appearance of these objects. In fact, if you tried to draw a generic bicycle from mind right now, chances are you could not get it even remotely right, even though you have seen thousands of bicycles in your lifetime. Try it right now: this effect is absolutely real. You brain has learned to completely abstract its visual input, to transform it into high-level visual concepts while completely filtering out irrelevant visual details, making it tremendously difficult to remember how things around us actually look.
Another easy thing to do to inspect the filters learned by convnets is to display the visual pattern that each filter is meant to respond to. This can be done with gradient ascent in input space: applying gradient descent to the value of the input image of a convnet so as to maximize the response of a specific filter, starting from a blank input image. The resulting input image would be one that the chosen filter is maximally responsive to.
The process is simple: we will build a loss function that maximizes the value of a given filter in a given convolution layer, then we will use stochastic gradient descent to adjust the values of the input image so as to maximize this activation value. For instance, here’s a loss for the activation of filter 0 in the layer “block3_conv1” of the VGG16 network, pre-trained on ImageNet:
library(keras)
model <- application_vgg16(
weights = "imagenet",
include_top = FALSE
)
layer_name <- "block3_conv1"
filter_index <- 1
layer_output <- get_layer(model, layer_name)$output
loss <- k_mean(layer_output[,,,filter_index])
To implement gradient descent, we will need the gradient of this loss with respect to the model’s input. To do this, we will use the k_gradients
Keras backend function:
# The call to `gradients` returns a list of tensors (of size 1 in this case)
# hence we only keep the first element -- which is a tensor.
grads <- k_gradients(loss, model$input)[[1]]
A non-obvious trick to use for the gradient descent process to go smoothly is to normalize the gradient tensor, by dividing it by its L2 norm (the square root of the average of the square of the values in the tensor). This ensures that the magnitude of the updates done to the input image is always within a same range.
# We add 1e-5 before dividing so as to avoid accidentally dividing by 0.
grads <- grads / (k_sqrt(k_mean(k_square(grads))) + 1e-5)
Now you need a way to compute the value of the loss tensor and the gradient tensor, given an input image. You can define a Keras backend function to do this: iterate
is a function that takes a tensor (as a list of tensors of size 1) and returns a list of two tensors: the loss value and the gradient value.
iterate <- k_function(list(model$input), list(loss, grads))
# Let's test it
c(loss_value, grads_value) %<-%
iterate(list(array(0, dim = c(1, 150, 150, 3))))
At this point we can define an R loop to do stochastic gradient descent:
# We start from a gray image with some noise
input_img_data <-
array(runif(150 * 150 * 3), dim = c(1, 150, 150, 3)) * 20 + 128
step <- 1 # this is the magnitude of each gradient update
for (i in 1:40) {
# Compute the loss value and gradient value
c(loss_value, grads_value) %<-% iterate(list(input_img_data))
# Here we adjust the input image in the direction that maximizes the loss
input_img_data <- input_img_data + (grads_value * step)
}
The resulting image tensor is a floating-point tensor of shape (1, 150, 150, 3)
, with values that may not be integers within [0, 255]. Hence you need to post-process this tensor to turn it into a displayable image. You do so with the following straightforward utility function.
deprocess_image <- function(x) {
dms <- dim(x)
# normalize tensor: center on 0., ensure std is 0.1
x <- x - mean(x)
x <- x / (sd(x) + 1e-5)
x <- x * 0.1
# clip to [0, 1]
x <- x + 0.5
x <- pmax(0, pmin(x, 1))
# Reshape to original image dimensions
array(x, dim = dms)
}
Now you have all the pieces. Let’s put them together into an R function that takes as input a layer name and a filter index, and returns a valid image tensor representing the pattern that maximizes the activation of the specified filter.
generate_pattern <- function(layer_name, filter_index, size = 150) {
# Build a loss function that maximizes the activation
# of the nth filter of the layer considered.
layer_output <- model$get_layer(layer_name)$output
loss <- k_mean(layer_output[,,,filter_index])
# Compute the gradient of the input picture wrt this loss
grads <- k_gradients(loss, model$input)[[1]]
# Normalization trick: we normalize the gradient
grads <- grads / (k_sqrt(k_mean(k_square(grads))) + 1e-5)
# This function returns the loss and grads given the input picture
iterate <- k_function(list(model$input), list(loss, grads))
# We start from a gray image with some noise
input_img_data <-
array(runif(size * size * 3), dim = c(1, size, size, 3)) * 20 + 128
# Run gradient ascent for 40 steps
step <- 1
for (i in 1:40) {
c(loss_value, grads_value) %<-% iterate(list(input_img_data))
input_img_data <- input_img_data + (grads_value * step)
}
img <- input_img_data[1,,,]
deprocess_image(img)
}
Let’s try this:
library(grid)
grid.raster(generate_pattern("block3_conv1", 1))
It seems that filter 1 in layer block3_conv1
is responsive to a polka dot pattern.
Now the fun part: we can start visualising every single filter in every layer. For simplicity, we will only look at the first 64 filters in each layer, and will only look at the first layer of each convolution block (block1_conv1, block2_conv1, block3_conv1, block4_conv1, block5_conv1). We will arrange the outputs on a 8x8 grid of filter patterns.
library(grid)
library(gridExtra)
dir.create("vgg_filters")
for (layer_name in c("block1_conv1", "block2_conv1",
"block3_conv1", "block4_conv1")) {
size <- 140
png(paste0("vgg_filters/", layer_name, ".png"),
width = 8 * size, height = 8 * size)
grobs <- list()
for (i in 0:7) {
for (j in 0:7) {
pattern <- generate_pattern(layer_name, i + (j*8) + 1, size = size)
grob <- rasterGrob(pattern,
width = unit(0.9, "npc"),
height = unit(0.9, "npc"))
grobs[[length(grobs)+1]] <- grob
}
}
grid.arrange(grobs = grobs, ncol = 8)
dev.off()
}
block1_conv1
block2_conv1
block3_conv1