CS6910 · Deep Learning · IIT Madras

Visualizing and fooling convolutional networks

Tools for seeing what a trained CNN has learned: the patches that excite a neuron, its filters, occlusion maps, and pixel gradients sharpened by guided backpropagation. Then the backward pass is pointed at the image itself, which gives class images, reconstructions from embeddings, Deep Dream, Deep Art and images that fool the network.

Lecture 9 Instructor Prof. Mitesh M. Khapra About 71 min of video 10 modules

At a glance

  • Four ways to look inside a trained CNN: the image patches that make a neuron fire, the first-layer filters drawn as images (the input that excites a neuron most is \(\mathbf{w}/\lVert\mathbf{w}\rVert\)), occlusion maps, and the gradient of a neuron with respect to every pixel.
  • Plain gradient maps are murky. Guided backpropagation also sets negative gradients to zero at every ReLU, so only positive influences reach the image and the map becomes sharp.
  • Freeze the weights and treat the pixels as parameters. Gradient ascent on a class score draws what the network takes that class to be; matching a layer's embedding reconstructs the image from that layer.
  • Deep Dream starts from a real image and maximizes \(h_{ij}^2\), so whatever a neuron faintly sees gets painted in. Deep Art matches the activations of a content image and the Gram matrices (style) of a style image.
  • The same machinery fools CNNs: an invisible change turns a bus into an "ostrich", and noise images get 99.6% confidence. Boundaries fitted to a few training points also label huge empty regions of input space.

Patches that make a neuron fire

This lecture is about understanding what a trained CNN has actually learned: what its filters detect, and what kinds of images make each neuron fire. Accuracy alone does not answer that. You may want to know whether any neuron fires for the dog class at all, or whether most neurons fire for every class, which would mean they have no discriminative power. Much of the material follows Andrej Karpathy's Stanford lectures on visualization, Deep Dream, neural style and adversarial examples.

The first tool needs no new maths. In Lecture 8 we saw that every neuron in every layer depends on a fixed patch of the input, its receptive field (for some layer, say, a 16 × 16 patch). The mapping is deterministic, so for any neuron we can always find which input pixels contributed to it.

  1. Find the patches a neuron fires for
  2. Pick a neuron in some layer of a trained CNN.
  3. Pass as many images as you have (training, test, anything) through the network and record the neuron's output for each.
  4. Keep the images where the output is high. These are ReLU neurons, which do not saturate at 1, so "fires" means a large output.
  5. For each of them, trace the neuron back to its receptive field and look at that image patch.

Girshick et al. (2014) ran this on 6 neurons of the pool5 layer. Each neuron's top patches had a clear theme: one fired for people's faces, one for dog faces, one for flowers (in bouquets, in pots, on tables, in any orientation), one for numbers and text such as addresses, dates and signboards, one for houses, and one for shiny surfaces. Each neuron fired on the patch that contained its object, without reacting to the surroundings, so the network had learned to localize these characteristics.

Treat this as a debugging tool, like feeding a program different inputs. Does the network need more training? Is there a class no neuron fires for? Are two classes being confused?

The previous section visualized neurons, which are outputs (entries of feature maps). The weights of a CNN are its filters, and those can be visualized too. The lecture borrows an argument from autoencoders (see Lecture 7): which input excites a given hidden neuron the most? The neuron computes \(\mathbf{w}^\top\mathbf{x}\) (before its activation) with its weight vector \(\mathbf{w}\), and the input is restricted to unit norm so the question has an answer:

\[ \max_{\mathbf{x}} \; \mathbf{w}^\top\mathbf{x} \quad \text{s.t.}\quad \lVert\mathbf{x}\rVert^2 = \mathbf{x}^\top\mathbf{x} = 1 \qquad\Longrightarrow\qquad \mathbf{x}^* = \frac{\mathbf{w}}{\sqrt{\mathbf{w}^\top\mathbf{w}}} = \frac{\mathbf{w}}{\lVert\mathbf{w}\rVert} \]

The lecture quotes this result. One line of proof: by Cauchy–Schwarz \(\mathbf{w}^\top\mathbf{x} \le \lVert\mathbf{w}\rVert\,\lVert\mathbf{x}\rVert = \lVert\mathbf{w}\rVert\), with equality exactly when \(\mathbf{x}\) is a positive multiple of \(\mathbf{w}\).

For MNIST digits the input has 784 dimensions, and so does \(\mathbf{w}\), because 784 weights connect the pixels to the neuron. So \(\mathbf{w}/\lVert\mathbf{w}\rVert\) can be drawn as a 28 × 28 image. In the autoencoder analysis some of these images showed a curve like the one in 2, 8 or 9, others a cusp like the one in 3 or 9. Each image is the pattern that makes its neuron fire.

A CNN filter is much smaller than the image: 3 × 3, 5 × 5 or 7 × 7. Think of the CNN as a feedforward network with sparse connections and weight sharing. In the figure, neuron \(h_{11}\) sees only pixels 1, 2, 5, 6 of a 4 × 4 image and \(h_{12}\) sees pixels 3, 4, 7, 8, both through the same 2 × 2 filter \(W\). Asking what input makes such a neuron fire gives the same answer as before, now for a patch: \(W/\lVert W\rVert\). Because the filter is shared, that patch triggers it wherever it occurs in the image, which is the point of convolution, since a nose should be detected at the top corner, in the centre or at the bottom alike. So filters are pattern detectors.

One filter, many patches h₁₁ h₁₂ W W 1234 5678 9101112 13141516 pixels of a 4 × 4 image, flattened row by row h₁₁ sees 1, 2, 5, 6 and h₁₂ sees 3, 4, 7, 8, both through the same 2 × 2 filter W Best input patch for a filter 1−1 1−1 filter W (illustrative) ÷ ‖W‖ = 2 0.5−0.5 0.5−0.5 x* = W / ‖W‖ High values on the left, low on the right: a vertical edge. Every other unit-norm patch gives this filter a smaller response.
Filters are pattern detectors. Left: as in the slide, two neurons look at different 2 × 2 patches of a 4 × 4 image through the same weights. Right: for an illustrative filter (shaded cells positive, marked cells negative), the unit-norm patch that maximizes the response is \(W/\lVert W\rVert\), here a vertical edge. Because \(W\) is shared, that edge fires the filter wherever it appears.

So we can simply plot each \(K \times K\) filter as an image. For the first convolution layer of a trained network the plots show edges at various orientations (slanted, vertical and others), coloured blobs and textures, even a checkerboard pattern. You want this variety. If the filters all look alike, they are not learning to tell different patterns apart, and something is wrong.

The slides add a claim with a question: this view is only interpretable for the filters of the first convolution layer. Why? The lecture leaves it as a quiz; an answer is in Test yourself.

The next debugging tool asks which portions of an image are responsible for the probability of a class. Insights like these help with design choices, for example whether 5 × 5 or 7 × 7 filters suit your data. Most users will train an off-the-shelf CNN and report accuracy; these tools are for understanding what is going on and how to improve it, and some appear in the assignment.

The idea is the usual way to judge a factor's influence: remove it and see whether the output changes. In a movie-review classifier, dropping words like "amazing" and "great" should lower the probability of the positive label (say from 0.9 to 0.6), while dropping "the", "and" or "for" should change little.

  1. Occlusion map
  2. Feed the image and note the predicted probability of the true class (for example "Pomeranian").
  3. Replace one patch of the image by a gray patch and feed the occluded image again. Record the new probability of the true class.
  4. Repeat for patches at every location you like.
  5. Plot the probabilities as a heat map over patch positions. In the slide's maps, red regions cause little drop when occluded and blue regions a large drop.

The slide shows three cases, from the figure in Zeiler and Fergus (2014):

  • Pomeranian. Covering the dog's face drops the probability the most. That is what we would want: the network is using facial features and not guessing.
  • Car wheel. The instructor expected the wheel itself to matter most, and occluding it does not cause a drastic drop. One reading is that the network has learned redundant features, which is good: a face detector should use the nose, ears, hair and eyes, so hiding one part should not destroy it. Here, though, the drop is concentrated on another single region (he reads it as the rear-view mirror), which suggests heavy reliance on one feature and less robustness.
  • Afghan hound. Occluding the woman's face lowers the probability. The network has probably learned a wrong association, for example if many training images showed the dog being held by a woman. Given two networks, prefer the one whose map points at the dog.
3 × 3
p(face), clean image–
p(face), patch here–
Drop–
An occlusion map you can probe (synthetic image, toy scorer). The 12 × 12 image has two eyes, a mouth and a 2 × 2 "ball" in the lower-left corner as a distractor (the slide's Pomeranian has a tennis ball). The scorer, written for these notes, has two hand-set detectors, each max-pooled over the image: an eye detector (a pixel whose value exceeds its left and right neighbours) and a mouth detector (a bar of four high-valued pixels with lower rows above and below). Pixel values are drawn as ink density, and the gray patch has value 0.5. It outputs \(p = \sigma(6\,f_{\text{eye}} + 6\,f_{\text{mouth}} - 5.4)\). Hover over or click either panel (or use the arrow keys) to place the gray patch. Hiding one eye barely matters because the other eye still gives the eye detector its maximum: a redundant feature. Hiding the only mouth always hurts. Make the patch 5 × 5 to cover both eyes at once.

Influence of input pixels, by backpropagation

So far we have visualized neurons, visualized weights and occluded parts of the input. Now we want a principled measure of how much each pixel influences the output, or any neuron in an intermediate layer. The course's usual tool for influence is the gradient.

Flatten the image into \(m \times n\) inputs \(x_0, x_1, \dots, x_{mn}\). The influence of input \(x_i\) on a neuron \(h_j\) is \(\partial h_j / \partial x_i\): if a small change in \(x_i\) causes a large change in \(h_j\), then \(x_i\) has a lot of influence on \(h_j\).

\(\partial h_j/\partial x_i\)Meaning
\(= 0\)no influence
smalllow influence
largehigh influence

Compute this partial derivative for all \(mn\) inputs, \(\partial h_j/\partial x_0, \dots, \partial h_j/\partial x_{mn}\), and display the resulting gradient matrix as an image. If gray stands for 0, the image should be mostly gray. A specialized neuron should depend on a small patch of pixels; if every pixel influenced it, it would not be discriminating anything. Standard name: such a picture is often called a saliency map.

How do we get gradients with respect to the input? Backpropagation (Lecture 4) always stopped at the weights of the first layer, but nothing in the chain rule forbids one more link. If we called the input \(\mathbf{h}_0\) instead of \(\mathbf{x}\), it would be just another layer. Treat the CNN as a feedforward network with sparse connections and sum over every path from the pixel to the neuron. For the neuron \(h_{32}\) in the slide and pixel \(x_2\), three first-layer neurons \(h_{1i}\) lie on such paths:

\[ \frac{\partial h_{32}}{\partial x_2} = \sum_{i=1}^{3} \frac{\partial h_{32}}{\partial h_{1i}}\,\frac{\partial h_{1i}}{\partial x_2}, \qquad h_{1i} = \sum_{j=1}^{4} w_{ji}\,x_j \;\;\Longrightarrow\;\; \frac{\partial h_{1i}}{\partial x_2} = w_{2i} \]

The slide writes the first layer as a plain weighted sum. With a ReLU on top, each \(\partial h_{1i}/\partial x_2\) also picks up the ReLU's derivative, 1 if the unit was active and 0 if not. That factor is what the next section modifies.

x₀x₁x₂x₃x₄ x₅x₆x₇x₈ h₁₁h₁₂h₁₃ h₁₄h₁₅h₁₆ h₃₂ ∂h₃₂/∂h₁₁ ∂h₃₂/∂h₁₃ neuron of interest first hidden layer input pixels Three paths from x₂ up to h₃₂ each contributes (∂h₃₂/∂h₁ᵢ) · w₂ᵢ; the gradient is their sum lower accent edges carry w₂₁, w₂₂, w₂₃ arrows: direction of the backward pass
One more link in the chain. Gradients already reach the first hidden layer; multiplying by the weights \(w_{2i}\) carries them to pixel \(x_2\). Doing this for every pixel gives the gradient image. Faint lines are the other sparse connections (each first-layer neuron sees four neighbouring pixels).

The resulting gradient image of a cat is murky. Most of it is gray, as expected, but nothing stands out. The whole cat region is non-gray, where we would have liked, say, only the eye pixels to light up for some neuron. Plain gradients do not show sharp influences. Springenberg et al. proposed guided backpropagation, which gives a better picture 4:40.

The lecture calls the idea a bit hacky and heuristic, but it works well. The setting:

  1. Influence of the input on one neuron
  2. Feed an image to the CNN and do a forward pass.
  3. Pick one neuron in some feature map at some layer (the slide uses a 2 × 2 feature map \(\begin{bmatrix}1&0\\3&2\end{bmatrix}\) and the entry 2).
  4. Keep that neuron and set all other neurons in the layer to zero, giving \(\begin{bmatrix}0&0\\0&2\end{bmatrix}\). We care only about this neuron's influence.
  5. Backpropagate all the way to the image, giving \(\partial h / \partial i_1, \partial h / \partial i_2, \dots\) for every pixel.

On the way back, every ReLU matters. In the forward pass a ReLU lets positive values through and clamps negative values to zero. In the backward pass no gradient flows through such dead ReLU units (Lecture 7), so only the gradients at positions whose forward input was positive pass down. That is ordinary backpropagation.

Guided backpropagation applies the same idea to the gradients themselves: the forward pass does not let negative values go forward, so in the backward pass do not let negative gradients go back. Any gradient arriving from the layer above that is negative is also clamped to zero. Here is the slide's example at one ReLU layer, with \(f^l\) the ReLU's input and \(R^{l+1}\) the gradient arriving from above:

\[ f^{l} = \begin{bmatrix} 1 & -1 & 5\\ 2 & -5 & -7\\ -3 & 2 & 4 \end{bmatrix} \;\xrightarrow{\ \text{ReLU}\ }\; f^{l+1} = \begin{bmatrix} 1 & 0 & 5\\ 2 & 0 & 0\\ 0 & 2 & 4 \end{bmatrix}, \qquad R^{l+1} = \begin{bmatrix} -2 & 3 & -1\\ 6 & -3 & 1\\ 2 & -1 & 3 \end{bmatrix} \]
\[ R^{l}_{\text{backprop}} = \begin{bmatrix} -2 & 0 & -1\\ 6 & 0 & 0\\ 0 & -1 & 3 \end{bmatrix}, \qquad R^{l}_{\text{guided}} = \begin{bmatrix} 0 & 0 & 0\\ 6 & 0 & 0\\ 0 & 0 & 3 \end{bmatrix} \]

Backprop zeroes the entries where \(f^l \le 0\) (the dead units). Guided backprop also zeroes the entries where \(R^{l+1} \le 0\), so of the slide's nine gradients only 6 and 3 survive.

The backward rules through a ReLU, entry by entry (\(\mathbb{I}[\cdot]\) is 1 when true, 0 otherwise) \[ \text{backprop: } R^{l}_i = \mathbb{I}[f^{l}_i > 0]\; R^{l+1}_i \qquad\quad \text{guided: } R^{l}_i = \mathbb{I}[f^{l}_i > 0]\;\mathbb{I}[R^{l+1}_i > 0]\; R^{l+1}_i \]

Guided backpropagation is applied at every ReLU on the way down to the image.

Not in the video: the figure this slide borrows from Springenberg et al. (2014) also has a "deconvnet" row (the method of Zeiler and Fergus), which keeps only the positive gradients and ignores the forward pass: \(R^{l}_i = \mathbb{I}[R^{l+1}_i > 0]\,R^{l+1}_i\). On the slide's numbers it gives \(\begin{bmatrix}0&3&0\\6&0&1\\2&0&3\end{bmatrix}\). Guided backpropagation is the combination of both masks.

Backpropagation R > 0R ≤ 0 f > 0f ≤ 0 keep Rkeep R 00 keeps R where the ReLU was on (the true gradient) Deconvnet R > 0R ≤ 0 f > 0f ≤ 0 keep R0 keep R0 keeps R where R is positive (ignores the forward pass) Guided backpropagation R > 0R ≤ 0 f > 0f ≤ 0 keep R0 00 keeps R only where both hold (backprop, then drop negatives)
The ReLU gradient rule, three ways. \(f\) is the ReLU's input in the forward pass and \(R\) the gradient arriving from the layer above. Each cell says whether \(R\) is passed down unchanged or replaced by 0. Only the left table is the true derivative.
9 values 9 values
Backprop passes–
Deconvnet passes–
Guided passes–
Edit the numbers and compare the three rules. It starts from the slide's example. Colours follow the legend, and dashed cells were blocked (set to 0) by that rule. Guided backpropagation always passes a subset of what plain backpropagation passes: the positive part.

The intuition: there may be pixels that really influence the neuron, but positive and negative gradients flowing back through many paths cancel each other, so most influences come out near zero (the mostly gray image). Neglecting all negative influences and keeping only the positive ones should give a better picture of the true influence of the input 2:34.

The lecture calls this heuristic because it messes with the maths: the correct gradient must flow back whether it is positive or negative, so the guided map is no longer \(\partial h/\partial \mathbf{x}\). Its justification is the analogy with the forward ReLU plus the cancellation argument. In practice the result on the cat is much sharper. It concentrates on the eyes, and the layout of the cat is far clearer than in the plain gradient image. Guided backpropagation is a popular tool for understanding what a CNN does, and one you implement in the assignment.

Take a trained CNN that distinguishes \(k\) classes. What image should we feed it so that it is recognized as a dumbbell (or an ostrich, or a car)? This sounds like an odd goal, but it is the seed of adversarial machine learning. If the \(k\) classes were \(k\) people in a face-recognition system, someone might want to know what image gets them recognized as a famous person. A milder use: an automatic check of whether a product looks like a dumbbell.

The answer reuses everything we know about training, with the roles swapped. Training keeps the image fixed and changes the weights, \(W \leftarrow W - \eta\,\partial\mathcal{L}/\partial W\). Here we keep the weights of the trained network fixed and treat the image itself as the parameter matrix: start from a blank (zero, gray) image and adjust its pixels \(I = (i_0, i_1, \dots, i_{mn})\) so that the score of the class goes up.

Optimization problem over the pixels \[ I^* = \arg\max_{I}\; \big(S_c(I) - \lambda\,\Omega(I)\big) \]

\(S_c(I)\) is the score of class \(c\) before the softmax, and \(\Omega(I)\) is some regularizer that makes \(I\) look like an image. This is the usual training objective plus a regularizer, with \(I\) as the parameters where we used to have \(W, \mathbf{b}\).

Training the network (Lectures 3–4) image xfixed training data network weights Wupdated loss Le.g. cross-entropy W ← W − η ∂L/∂W Optimizing over the image (this lecture) image Ipixels = parameters same networkweights frozen objectivee.g. class score Sc(I) I ← I + η ∂Sc/∂I (ascent; the network is only passed through)
Same backward pass, different parameters. Top: training moves the weights. Bottom: the network is frozen and the gradient continues past the first layer into the pixels, exactly as in the gradient image of the previous sections. Everything from here to the end of the lecture uses the bottom row with a different objective.
  1. Class image by gradient steps on the pixels (slide version)
  2. Start with a zero image.
  3. Set the score vector to \([0, 0, \dots, 1, \dots, 0, 0]\): 1 for the class of interest, 0 elsewhere. This seeds the backward pass so that it computes the gradient of \(S_c\) alone.
  4. Compute the gradient \(\partial S_c(I)/\partial i_k\) for every pixel \(i_k\).
  5. Update every pixel, \(i_k \leftarrow i_k + \eta\,\partial S_c(I)/\partial i_k\).
  6. Do a forward pass through the network again with the modified image.
  7. Go to step 2. Stop at convergence, whatever you define it to be, for example once the class gets a score of 0.9 or 0.95 instead of 1.

The slide writes step 4 as \(i_k = i_k - \eta\,\partial S_c(I)/\partial i_k\), the gradient descent form. Since the score is to be maximized, the step must go along the gradient (equivalently, descend on \(-S_c\)); the same applies to the neuron version below.

The results are strange (the images are from Simonyan et al., 2013, a source the slide does not name). For dumbbell, starting from the zero image, many dumbbell-like shapes appear in different orientations. The colours are nothing like real dumbbells, and the network is being redundant: it draws many dumbbells so that some of them make the output fire. Cup gives several trophy-like cups, dalmatian gives black and white spots and dog-like shapes, and bell pepper, lemon and husky are similar. The shapes of the object show up, but nowhere is there a clear image a person would call that object 6:58.

This is nothing to be impressed by. The network assigns these arbitrary images to "dalmatian", so anyone could upload such an image as a dalmatian and an automated system would accept it. These images can later be used to fool the network (Module 13.10).

Any neuron, not just a class M6 · 8:34

The output score is just one neuron. The same procedure works for an arbitrary neuron anywhere in the CNN, with \(A(I)\) the activation of the chosen neuron in some layer:

  1. Image that excites one neuron
  2. Feed an image through the network.
  3. Set the activations in the layer of interest to zero, except for the neuron of interest.
  4. Backpropagate to the image.
  5. Update each pixel, \(i_k \leftarrow i_k + \eta\,\partial A(I)/\partial i_k\), and repeat.

Starting from different initializations instead of the zero image gives different insights. The slide shows four images, each obtained by focusing on the same neuron in layer 8 and starting from a different initialization, and a similar analysis for layer 7, where some neuron's image looks like a ship with several layers of structure. Different neurons fire for different kinds of patterns, and making such images is a good way to learn more about your network.

Every layer's output can be thought of as an embedding of the image. The input is 227 × 227 × 3; the fc7 layer gives a 4096-dimensional vector for it, and a convolution volume can be flattened into a vector too. Think of autoencoders: a good embedding should capture all the important characteristics of the image, so we should be able to reconstruct the image from it. Can we?

Pose it as optimization over the pixels again. Pass the image of interest (the slide's example is a monkey) through the network and store its embedding \(\phi_0\). Then look for an image that (1) has an embedding close to \(\phi_0\) and (2) looks natural, which is the job of a regularizer. For squared closeness, compare the embeddings dimension by dimension and add up the squared differences.

  1. Reconstruct an image from its embedding
  2. \(\phi_0\): embedding of the image of interest. \(X\): a random image, say the zero image.
  3. Forward pass with \(X\) and compute its embedding \(\phi(X)\).
  4. Compute \(\mathcal{L}(I) = \lVert\phi(X) - \phi_0\rVert^2 + \lambda\,\lVert\phi(X)\rVert_6^6\).
  5. Update every pixel by gradient descent, \(i_k \leftarrow i_k - \eta\,\partial\mathcal{L}(I)/\partial i_k\), and repeat until convergence.

\(\lVert\mathbf{v}\rVert_6^6 = \sum_k v_k^6\). The slide puts this penalty on \(\phi(X)\). In the original work (Mahendran and Vedaldi, 2015) the 6-norm penalty is applied to the image \(X\) itself, which is what makes it an image prior. It discourages a few extreme pixel values.

The slides then reconstruct the monkey from each layer of the network in turn: Conv-1, ReLU-1, max-pool-1, Norm-1, Conv-2, ReLU-2, max-pool-2, Norm-2, Conv-3, ReLU-3, Conv-4, ReLU-4, Conv-5, ReLU-5, max-pool-5, FC-6, ReLU-6, FC-7, ReLU-7, FC-8.

  • From Conv-1 the reconstruction is almost the same image as the original.
  • Going deeper, reconstructions become more and more abstract and the monkey stops coming back. Apart from the Conv-1 and Conv-2 layers, they are not accurate reconstructions.
  • From FC-6 / FC-7 they look very weird. By then the network has abstracted the image away: perhaps something like a nose or eyes is kept here and there, but the shape and other characteristics are lost 4:45.
  • Max-pooling makes it harder still. It is just another embedding, but a much stronger compression: of each pooling window only the maximum survives.

So the deeper representations lose much of the information needed to rebuild the input, which is expected from layers built to abstract.

So far the optimization started from a blank image and pushed it towards a class. Deep Dream starts instead from an actual image, say a picture of the sky. Focus on some layer, check the activations of its neurons, and change the image so that a chosen neuron \(h_{ij}\) fires even more (the slides draw several neurons, but think of a single one).

The parameters are again the pixels. To make "fire more" a neat differentiable objective, maximize the square of the activation, and split the gradient for a pixel \(i_{mn}\) with the chain rule:

\[ \max_{I}\; \mathcal{L}(I) = h_{ij}^2, \qquad \frac{\partial\mathcal{L}(I)}{\partial i_{mn}} = \frac{\partial\mathcal{L}(I)}{\partial h_{ij}}\,\frac{\partial h_{ij}}{\partial i_{mn}} = 2\,h_{ij}\,\frac{\partial h_{ij}}{\partial i_{mn}} \]

The first factor is immediate, and \(\partial h_{ij}/\partial i_{mn}\) is backpropagation with one more link of the chain, as in Module 13.4. Then update the image and feed it back:

\[ i_{mn} \leftarrow i_{mn} + \frac{\partial\mathcal{L}(I)}{\partial i_{mn}} \]

The slide writes the step without a learning rate; a step size \(\eta\) can multiply the gradient, as in the other updates.

On the next pass \(h_{ij}\) fires a bit more, because we modified the image for exactly that purpose. Now recall what \(h_{ij}\) captures: certain patterns. If it fires more and more, the image must contain more and more of those patterns. Iterating makes the image look increasingly like whatever makes the neuron fire.

The demos on the slides:

  • A sky with clouds fills up with castles: literally castles in the air. In the training data a castle usually has the sky as its background, so the network has learned the correlation. Seeing sky, some castle neurons fire a little, and pushing them harder makes castles appear 6:49.
  • A landscape with mountains and green regions sprouts fish eyes and fish-like shapes, and birds and animals on the mountains and green areas, which in the data set appear against such green and brown backgrounds.
  • A still from the film Inception (men in brown suits) produces dogs everywhere: in this data set, brown things were mostly dogs. The network cannot dream beyond its data set.

What is happening: the network was trained to detect patterns (dogs, cats, birds and so on) that appear frequently in ImageNet, and it starts seeing them even where they hardly exist. Google's blog post on "inceptionism" describes the loop: a cloud that looks slightly like a bird is made to look more like a bird, so the network recognizes the bird more strongly on the next pass, and so on, until a detailed bird appears seemingly from nowhere.

Take a camera image (the content image, in the slides a picture of Gandalf) and an artwork (the style image), and render the content in that art form. The instructor flags one step as a leap of faith. The method defines two targets and optimizes the pixels of a new image to meet both.

Content

The content targets are the activations of all layers for the content image. The assumption is that the hidden representations capture the essence of the image (the face and its attributes), so a new image in a different style should produce activations close to these. Let \(\mathbf{p}\) and \(\mathbf{x}\) be the activations of the content image and the new image at a layer, indexed by position and channel:

\[ \mathcal{L}_{\text{content}}(\mathbf{p}, \mathbf{x}) = \sum_{i,j,k} \big(p_{ijk} - x_{ijk}\big)^2 \]

Every entry of the activation volume of the new image should match the content image. The slide shows one layer; the same term can be used for every layer.

Style

How do we capture the style of an image? The lecture takes this on faith from the original paper: if \(V\) holds the activations of a layer with 64 channels of size 256 × 256, then the 64 × 64 matrix \(V^\top V\) captures the style of the image, and deeper layers capture more of it. This matrix is the Gram matrix of the layer.

The slide writes \(V \in \mathbb{R}^{64\times(256\times256)}\) and \(V^\top V \in \mathbb{R}^{64\times 64}\). For the product to be 64 × 64, lay \(V\) out with one column per channel, \(V \in \mathbb{R}^{(256\cdot256)\times 64}\); then \((V^\top V)_{ij} = \sum_k V_{ki}V_{kj}\) is the dot product of feature maps \(i\) and \(j\) over all positions.

From a layer's activations to its Gram matrix ch 1 ch 3 C channels, each H × W (slide: C=64, H=W=256) flatten channels to columns V (H·W) × C Vᵀ V G = VᵀV G₁₁G₁₂G₁₃ G₂₁G₂₂G₂₃ G₃₁G₃₂G₃₃ C × C G₁₂ = Σₖ Vₖ₁ Vₖ₂ channel 1 · channel 2, summed over all positions diagonal: how strongly each feature fires off-diagonal: which features fire together content loss: compare activations p (content image) and x (new image) entry by entry style loss: compare Gram matrices G (style image) and A (new image) entry by entry
The Gram matrix sums positions out. Each entry is an inner product between two channels' feature maps, so it records which features co-occur and how strongly, while where they occur is summed away. That reading is the standard one; the lecture itself takes the claim that this captures style on trust. Illustrative sizes: 3 channels of 4 × 4.

To make the new image's style at layer \(\ell\) match the style image, compare the two Gram matrices. With \(G^\ell\) and \(A^\ell\) the Gram matrices at layer \(\ell\) for the style image and the new image respectively, and \(\mathbf{a}\) the style image's activations:

\[ E_\ell = \sum_{i,j} \big(G^\ell_{ij} - A^\ell_{ij}\big)^2, \qquad \mathcal{L}_{\text{style}}(\mathbf{a}, \mathbf{x}) = \sum_{\ell=0}^{L} w_\ell\,E_\ell \]

The weights \(w_\ell\) choose how much each layer counts; you can use one layer or all of them.

Total loss, minimized over the pixels of the new image \[ \mathcal{L}_{\text{total}}(\mathbf{p}, \mathbf{a}, \mathbf{x}) = \alpha\,\mathcal{L}_{\text{content}}(\mathbf{p}, \mathbf{x}) + \beta\,\mathcal{L}_{\text{style}}(\mathbf{a}, \mathbf{x}) \]

\(\alpha\) and \(\beta\) are hyperparameters that balance matching the content against matching the style.

Optimizing the pixels with this objective (plus a bunch of other tricks) renders Gandalf in the given style. Code for it is available to try. Standard name: this is neural style transfer (Gatys et al., 2015). The broader point is that once you can write an objective over pixels, you can combine two images in whatever way you can imagine.

Fooling deep convolutional networks

We have changed images to suit three needs: to make a class fire, to make neurons see patterns that were not really there (Deep Dream), and to produce artistic images (Deep Art). The same idea of optimizing over the input can also fool a CNN.

Feed an image of a bus to a trained network. Instead of setting up the cross-entropy loss for the correct class, set it up for an incorrect class, say ostrich. Backpropagate, leave every weight alone, and change only the image so that the likelihood of "ostrich" increases:

\[ I \leftarrow I + \eta\,\frac{\partial \log P(\text{ostrich} \mid I)}{\partial I} \qquad (\text{weights fixed}) \]

With very minimal changes to the image, the network is soon convinced it is looking at an ostrich (Szegedy et al., 2013). The slide shows triples: the original, the change (magnified to be visible) and their sum. The original and the sum are indistinguishable to a human, yet the network calls every sum an ostrich. The same happens for the other examples: a soap dispenser, a bird, an insect, a temple and a dog.

You do not even need a real starting image. Start from random images and optimize them until the network predicts some class: the results look like noise to us, yet the classifier labels them robin, cheetah, armadillo and so on, and it is 99.6% confident in all these cases (Nguyen, Yosinski and Clune, 2014). That is worse than a random classifier, which over 1000 classes would give each about 1/1000.

An intuitive explanation M10 · 4:51

The explanation is due to Andrej Karpathy.

  • Images are extremely high-dimensional objects, points in \(\mathbb{R}^{227\times227}\) on the slide. That space holds an enormous number of points.
  • Only a tiny fraction of them are natural images; most are random matrices that look like the noise images above. Even 1 or 10 million training images are a small sample of the natural ones.
  • From the training images we fit decision boundaries, for some 1000 classes.
  • In doing so we also make decisions for the vast number of points we never saw. Whole regions without a single training point get a class. A boundary drawn around a few cheetah images also encloses many random points that have nothing to do with cheetahs.
  • To fool the network, start from a random image and move just far enough to land inside such a region. The classifier was aggressive in fitting the training data and has already labelled it.
Decisions far from the data cheetah cheetah other classes other classes noise image: "cheetah" A tiny step across a boundary bus ostrich δ x: bus photo x + δ: "ostrich" δ is tiny in every pixel, invisible to a person
Why fooling is easy (illustrative cartoon). Left, after the slide: filled dots are cheetah training images, hollow dots other classes. The fitted boundaries also hand out huge empty regions, so a noise image deep inside one is called "cheetah" with high confidence. Right: a real photo near a boundary needs only a tiny change \(\delta\) to cross it.

One recipe, six objectives

Every method from Module 13.6 onward freezes the network and takes gradient steps on the pixels. Only the objective changes:

MethodStart fromObjective over the pixels
Class image (13.6)zero imagemaximize \(S_c(I) - \lambda\Omega(I)\)
Neuron image (13.6)zero or other initializationsmaximize the activation \(A(I)\)
Inversion (13.7)random or zero imageminimize \(\lVert\phi(X) - \phi_0\rVert^2\) + regularizer
Deep Dream (13.8)a natural imagemaximize \(h_{ij}^2\)
Deep Art (13.9)a new imageminimize \(\alpha\mathcal{L}_{\text{content}} + \beta\mathcal{L}_{\text{style}}\)
Fooling (13.10)a real image, or noisemaximize the probability of a wrong class

This ends the discussion of convolutional networks. Lecture 10 turns to learning vector representations of words.

Key terms

Receptive field
The input patch a neuron depends on; it can be traced back deterministically through the layers.
Maximally activating input
For weights \(\mathbf{w}\) and a unit-norm input, \(\mathbf{w}^\top\mathbf{x}\) is largest at \(\mathbf{x}^* = \mathbf{w}/\lVert\mathbf{w}\rVert\).
Filter visualization
Plotting each \(K\times K\) first-layer filter as an image to see the pattern it detects.
Occlusion experiment
Gray out a patch, reclassify, and map the probability of the true class over patch positions.
Gradient (saliency) map
\(\partial h_j/\partial x_i\) for every pixel, shown as an image; gray means no influence.
Dead ReLU
A ReLU whose forward input was not positive; no gradient flows back through it.
Guided backpropagation
Backprop that also zeroes negative gradients from the layer above at every ReLU; gives sharper maps.
Deconvnet rule
Passes only positive gradients and ignores the forward sign (in the slide's source figure, not the video).
Optimization over images
Weights frozen, pixels treated as parameters, gradient steps on an objective such as \(S_c(I) - \lambda\Omega(I)\).
Embedding inversion
Finding an image whose embedding at some layer matches a given one; works well only from early layers.
Deep Dream
Gradient ascent on \(h_{ij}^2\) from a natural image, amplifying patterns the neuron already half-sees.
Content loss
\(\sum_{ijk}(p_{ijk} - x_{ijk})^2\) between the activations of the content image and the new image.
Gram matrix
\(V^\top V\) of a layer's flattened channels: channel-by-channel inner products, used as the style.
Fooling (adversarial) image
An image changed imperceptibly, or built from noise, that the network classifies wrongly with high confidence.

Test yourself

Q1A neuron has weights \(\mathbf{w} = [3, 4]^\top\). Which unit-norm input excites it most, and what is its pre-activation then?

\(\lVert\mathbf{w}\rVert = 5\), so \(\mathbf{x}^* = \mathbf{w}/\lVert\mathbf{w}\rVert = [0.6, 0.8]^\top\) and \(\mathbf{w}^\top\mathbf{x}^* = 1.8 + 3.2 = 5 = \lVert\mathbf{w}\rVert\). By Cauchy–Schwarz no other unit vector does better.

Q2Why is plotting filters as images only interpretable for the first convolution layer?

The lecture leaves this as a quiz; the standard answer: a first-layer filter multiplies pixels directly, so \(W/\lVert W\rVert\) is a pixel patch you can look at. A deeper filter multiplies the previous layer's feature maps (for example a 3 × 3 × 256 block of activations), so its \(W/\lVert W\rVert\) is a pattern over feature channels with no direct meaning as an image.

Q3In an occlusion map for "Afghan hound", the biggest drop comes from covering the woman's face. What does that tell you?

The network relies on a region that is not the dog: probably a spurious association learned because many training images showed the dog with a woman. A network whose largest drop sits on the dog has learned something more meaningful, and you should prefer it.

Q4At one ReLU, the forward inputs are \(f = [3, -2, 0.5, 4]\) and the gradients from above are \(R = [-1, 5, 2, -3]\). What do backprop, the deconvnet rule and guided backprop pass down? Which one is the true gradient?

Backprop keeps \(R\) where \(f > 0\): \([-1, 0, 2, -3]\). Deconvnet keeps \(R\) where \(R > 0\): \([0, 5, 2, 0]\). Guided keeps \(R\) where both hold: \([0, 0, 2, 0]\). Only backprop gives the true derivative; guided backprop is a heuristic that drops the negative influences to avoid cancellation and get a sharper map.

Q5When generating a "dumbbell" image, what are the parameters, what stays fixed, and which way does the update go?

The pixels \(i_k\) are the parameters and the trained weights stay fixed. Maximize \(S_c(I) - \lambda\Omega(I)\), with \(S_c\) the pre-softmax score, by stepping along the gradient: \(i_k \leftarrow i_k + \eta\,\partial S_c/\partial i_k\) (descent on \(-S_c\)). The result contains many dumbbells in different orientations because that raises the score; it does not need to look like a real dumbbell.

Q6In Deep Dream, what is \(\partial\mathcal{L}/\partial i_{mn}\) for \(\mathcal{L} = h_{ij}^2\), and why does a sky photo fill with castles?

\(\partial\mathcal{L}/\partial i_{mn} = 2h_{ij}\,\partial h_{ij}/\partial i_{mn}\). In the training data castles usually appear against the sky, so sky makes castle-detecting neurons fire a little. Each ascent step strengthens whatever made them fire, they fire more on the next pass, and the loop paints castles in.

Q7A layer has 128 channels of size 56 × 56. What are the sizes of \(V\) and of its Gram matrix, and how does the style loss differ from the content loss?

With one column per channel, \(V\) is \(3136 \times 128\) (since \(56 \cdot 56 = 3136\)) and \(G = V^\top V\) is \(128 \times 128\). The content loss compares activations position by position; the style loss compares Gram matrices, whose entries are summed over all positions, so it matches which features co-occur and not where they are.

Q8Why can a CNN label a noise image "cheetah" with 99.6% confidence?

Images live in a huge space (\(\mathbb{R}^{227\times227}\) on the slide) and natural images, let alone training images, fill a tiny part of it. Decision boundaries fitted to the training points also assign classes to vast regions that contain no data. Optimizing a noise image just moves it into the cheetah region, where the classifier is confident although it has never seen anything like it.