model-insights · ResNet-50 · ImageNet

Inside ResNet-50

A year after VGG16, ResNet went from 16 layers to 50 (and 152) and got more accurate with a fifth of the parameters. The trick is a shortcut: each block only learns a correction that is added to its input. It was trained on the same 1.2 million ImageNet photos, so this page asks two questions. What does the deeper network do with them? And does it pick up the same traces of those photos?

Loading model weights…

1 Fifty layers, sixteen shortcuts

A stem (a 7×7 convolution and a max-pool) shrinks the photo to 56×56. Then come four stages of 3, 4, 6 and 3 bottleneck blocks. Each block squeezes its input to a quarter of the channels with a 1×1 convolution, does a 3×3 convolution there, and expands back with another 1×1 convolution. The result, F(x), is added to the block's input x. A block that has nothing to add can output almost zero and pass its input through unchanged. That is why so many layers can be stacked without the signal fading. At the end, the 7×7×2048 map is averaged into one vector, and a single linear layer turns it into 1,000 class scores.

Every convolution is followed by batch normalisation, which rescales each channel to the statistics it had over the training set. In the exported graphs it is folded into the convolution weights, as it is in any deployed model. 25.6 million parameters in total, against VGG16's 138 million.

Layer by layer

2 Dissect an image

Pick, drop, paste or upload an image. The bars on the right show one bottleneck block each: how large its correction ‖F(x)‖ is compared with what reaches it through the shortcut. In the first two stages every block only adjusts its input, adding a correction half to nine-tenths its size. From layer3 on the corrections grow, and in layer4 most blocks add as much as they pass on, or more. The network keeps the early description of the image largely intact and rewrites it near the end, where it turns into an answer. That pattern held on all four samples we measured.

drop / paste an image

What ResNet-50 sees

    Where it looks (class activation map)

    Because the head is just an average followed by one linear layer, the class score is exactly the average of this map plus a bias. For ResNet, Grad-CAM and the original CAM are the same thing. Try the astronaut: ResNet-50 also calls her a bobsled.

    What each block adds ‖F(x)‖ / ‖shortcut‖

    3 Texture is often enough

    ImageNet can largely be solved by recognising stuff (fur, feathers, chrome, crema) rather than the shapes of things. Geirhos et al. (2019) showed that ImageNet-trained CNNs, ResNet-50 included, side with texture when shape and texture disagree. You can see it here: cut the image into tiles, shuffle them, and watch how little the prediction cares.

    Shuffled

      Measured on the six samples with five shuffles each: the cat in a 4×4 shuffle is still "Egyptian cat" every time, at 83% instead of 75% unshuffled. The moped survives a 4×4 shuffle in all five runs for ResNet-50 but only once for VGG16. At 8×8 the tiles are 28 pixels across and every prediction breaks, partly because the new tile edges are themselves strong features. The rocket and the brick building, recognised by their outline, already break at 2×2.

      4 Same photos, same residues

      Here is the comparison that matters for the traces of the training data. Below, each class is visualised twice, by maximising its logit in VGG16 (left) and in ResNet-50 (right), with the same recipe. The architectures share nothing but the training photos. Whatever shows up in both comes from the photos.

      What individual channels detect, stage by stage

      Dream it live

      The gradient for this comes from an ONNX graph that PyTorch traced, backward pass included. Unlike VGG16, nothing about ResNet's backward pass was written by hand (see the README). Each step is one forward and one backward pass through all 50 layers.

      Offline render

      When the gallery has a render of this objective, it appears here.