AI · 12 Artificial Intelligence

How Machines See: Convolutional Networks

How Machines See: Convolutional Networks

Shift every handwritten digit one pixel, and a flat network falls from 97.3% to 47%; a convolutional network keeps 98%. Convolution by hand, padding, stride, weight sharing, pooling, AlexNet, inside a real ResNet-18, skip connections, transfer learning, and a real attack that makes it see a giant panda. Every number on screen is real output from code you can run.

Chapters

Cheat sheet

Code

Try it yourself (pip install torch torchvision scikit-learn pillow; downloads ResNet-18's weights, about 45 MB, once)
import torch
from PIL import Image
from sklearn.datasets import load_sample_image
from torchvision.models import resnet18, ResNet18_Weights
from torchvision.utils import save_image

weights = ResNet18_Weights.IMAGENET1K_V1
model = resnet18(weights=weights).eval()
classes = weights.meta["categories"] # 1,000 class names
print(f"ResNet-18: {sum(p.numel() for p in model.parameters()):,} knobs, {len(classes):,} classes")

for name in ("china.jpg", "flower.jpg"):
photo = Image.fromarray(load_sample_image(name)) # 3 colors, each 0-255
x = weights.transforms()(photo).unsqueeze(0) # crop the center 224 x 224
with torch.no_grad():
probs = model(x).softmax(dim=1)[0]
top = probs.topk(5)
print(f"{name}: " + ", ".join(f"{classes[i]} {p:.1%}" for p, i in zip(top.values, top.indices)))

print("input", tuple(x.shape[1:]))
with torch.no_grad():
h = model.relu(model.bn1(model.conv1(x)))
print("conv1", tuple(h.shape[1:]))
h = model.maxpool(h)
for stage in ("layer1", "layer2", "layer3", "layer4"):
h = getattr(model, stage)(h)
print(stage, tuple(h.shape[1:])) # (channels, height, width)

filters = model.conv1.weight # 64 filters, 3 colors, 7 x 7
save_image(filters, "filters.png", nrow=8, normalize=True, scale_each=True)
print("first-layer filters", tuple(filters.shape), "saved to filters.png")

What we got: 11,689,512 knobs; china.jpg: bell cote 53.6%; flower.jpg: honeycomb 66.3%; 224 x 224 shrinks to 7 x 7 as channels grow from 3 to 512.

Sources

Transcript

Last episode, our digit reader saw every image as a flat list of sixty-four numbers, and scored ninety-seven point three percent. Now move every test digit one pixel to the right. To you, nothing changed. To the network, a lot did: it falls to forty-seven percent. A convolutional network, with about a fifteenth of the knobs, scores ninety-eight percent.

Shift the digits, and it still scores ninety-eight. That's a classic interview question: why do convolutional networks beat fully connected ones on images? Let's see how machines see, and then, how to fool them.

This is Just Known. From unknown, to known. Episode twelve: how machines see, with convolutional networks. To a computer, an image is a grid of numbers. Our seven is eight by eight, each pixel an ink level from zero to sixteen.

A color photo is three grids, red, green and blue, each from zero to two hundred fifty-five. This one holds over eight hundred thousand numbers. A flat network has two problems with that. Size: one layer of five hundred twelve neurons, on even a small crop of this photo, needs seventy-seven million knobs. And it never knows which pixels are neighbors.

Scramble every image's pixels the same way, and our flat network learns just as well: ninety-seven point six percent. To it, a picture is just a list. A convolutional network looks at small patches instead. Here's a filter: a three-by-three grid of weights, minus one down the left, plus one down the right. Lay it on part of the seven, multiply each pixel by the weight on top of it, and add the nine products: sixteen, plus ten, plus fifteen, is forty-one.

Right side inky, left side blank: that's a vertical edge. Now slide the filter across the image, one pixel at a time, row by row. Each stop gives one number, and together they make a new, smaller grid, a feature map. It's high along a stroke's left edge, and negative along its right edge. Change the weights, and the filter finds something else.

Turn it on its side, and it finds horizontal edges, like the seven's crossbar. Make all nine weights one-ninth, and it blurs. On a patch of the photo, the same three filters pick out the tower's columns, its roof lines, and a softer copy. The map is also smaller: an eight-by-eight image gave six-by-six. Pad the border with zeros, and it stays eight.

Jump two pixels per step, a stride of two, and it halves, to four. The rule: the input, plus twice the padding, minus the filter size, divided by the stride, plus one. The key: one filter slides everywhere. That's weight sharing: one edge detector, nine weights, reused at every position, so it finds an edge wherever it is. Move the seven one pixel right, and its feature map moves right too, with the same numbers.

So a layer needs very few knobs. Our first layer has thirty-two filters, and just three hundred twenty knobs. The idea came from biology. In nineteen fifty-nine, Hubel and Wiesel found cells in a cat's visual cortex that respond to lines and edges at one particular angle. Fukushima built a network on that idea in nineteen eighty, and in nineteen eighty-nine, Yann LeCun's team trained one with backpropagation to read zip codes.

After each convolution comes a ReLU, which zeroes the negatives, and often pooling: keep only the largest value in each two-by-two block. The map halves, and keeps its strongest signals. Now our digit network. Two convolution layers: thirty-two filters, then sixty-four. Then each of the sixty-four maps is pooled down to its single largest value.

Did this pattern appear anywhere? Then come ten scores. That's under twenty thousand knobs, and ninety-eight percent on the test set. Move every digit one pixel sideways, and it still scores ninety-eight. The flat network drops to forty-five or forty-seven.

Convolutional networks stayed small until the data and the hardware caught up. In two thousand nine, Fei-Fei Li and colleagues released ImageNet: millions of labeled photos. Its yearly competition used a thousand classes and about one point three million training images. In twenty twelve came AlexNet, from Alex Krizhevsky, Ilya Sutskever and Geoffrey Hinton. It had five convolution layers, sixty million knobs, ReLU and dropout, and it trained for five to six days on two graphics cards, as episode eleven promised.

Their entry missed the right answer in its top five guesses fifteen point three percent of the time. The runner-up: twenty-six point two. Then networks got deeper, fast. Let's open one up: ResNet-eighteen, from the same family as ImageNet's twenty fifteen winner, with eleven point seven million knobs. On our photo of Beijing's Summer Palace, its top guess is a bell cote, a small shelter for bells on a roof, at fifty-four percent, then palace, at thirty-one.

On the flower, it has to pick from its thousand classes: honeycomb, sixty-six percent. Its first layer has sixty-four filters, each seven by seven pixels, in color. Nobody designed them; training found them. Many are edges at different angles, others are blobs of color, much like Hubel and Wiesel's cells. Eight ended up nearly blank.

Stage by stage, the maps shrink, from two hundred twenty-four pixels across to seven, while their number grows, from three colors to five hundred twelve. Early maps light up on edges. Deeper ones respond to textures, then to parts of objects, and each cell sees more of the photo. By the last stage, one cell's view is wider than the whole image. Here, the three last-stage maps that push hardest for bell cote light up right on the tower.

Finally, each map is averaged to one number, and a last layer turns those five hundred twelve numbers into a thousand scores. Deeper should be better, but it wasn't. In twenty fifteen, a team at Microsoft Research showed a fifty-six-layer network with higher training error than a twenty-layer one. Not overfitting: the deeper one was simply harder to train. We saw it too, even with batch normalization.

Their fix was the skip connection: each block adds its input back to its output. A block only has to learn a change, and the blame gets a direct path backward. Our thirty-two-layer plain network, without normalization, passed less than a trillionth of the blame to its first layers, and learned nothing. With skips, it reached ninety-one percent. Their ResNets, up to a hundred fifty-two layers deep, won ImageNet in twenty fifteen.

Transfer learning reuses a trained network: freeze its layers and train only a new last layer on your own few labeled images, or fine-tune them all, gently. It works best when your images look like the ones it learned from. Ours don't: they're blocky, eight-by-eight digits. With ten labeled examples of each digit, ResNet's frozen ImageNet features scored about eighty-four percent, and fine-tuning all its knobs, ninety-three. Our tiny CNN, trained from scratch on the same hundred images, scored ninety-six.

That fits a twenty fourteen study: the further the new task is from the old, the less the features transfer. Now, the attacker. In episode ten, backpropagation found how to nudge each weight to lower the loss. Run it one step further, back to the pixels, and it tells you how to nudge each pixel to change the answer. We picked a target, giant panda, and nudged every color value of our Summer Palace photo a tiny step toward it, twenty times, never by more than two levels out of two hundred fifty-five.

To us, the photo looks the same. ResNet-eighteen now says giant panda, ninety-nine point nine nine percent sure. And it's not just any noise: random changes of the same size still leave it saying bell cote. It isn't just our model. In twenty thirteen, researchers found such hardly visible changes, and in twenty fourteen, Ian Goodfellow's team turned a panda into a gibbon, ninety-nine point three percent sure, with a single step.

On our photo, one step turned bell cote into palace. Each change is tiny, but there are a hundred fifty thousand of them, all pushing the same way. And it works in the physical world: in twenty eighteen, stickers made a road-sign classifier read a stop sign as a speed-limit sign. The best-known defense is adversarial training: attack your own model while it learns. It helps, but it's expensive, and still no complete fix.

You can run this yourself: under thirty lines of PyTorch, in well under a minute on a laptop CPU. The code is in the description. It loads ResNet-eighteen with its ImageNet weights, prints its top five guesses for both photos, follows a photo through the stages, from two hundred twenty-four pixels across down to seven, and saves the sixty-four first-layer filters as an image. A second script has everything else in this episode, including the attack. Interview corner.

Why do convolutional networks beat fully connected ones on images? How many knobs does a convolution layer have? And what do skip connections fix? Pause here, and try answering in thirty seconds.

Here's a strong answer. A convolution layer slides small filters over the image, so it sees neighboring pixels together, and it shares the same weights at every position: far fewer knobs, and a pattern is found wherever it appears. The count is filter height times width times input channels, plus one bias, times the number of filters. Three by three, from thirty-two channels to sixty-four, is eighteen thousand four hundred ninety-six. Pooling and depth add tolerance to small shifts, and build up from edges to objects.

Skip connections add each block's input to its output. They fix degradation, where deeper plain networks train worse, and give gradients a direct path back. To stand out, add that downsampling breaks shift invariance. With one two-by-two pool, our CNN fell from ninety-eight to eighty-four percent on the one-pixel shift. Let's recap.

To a network, an image is a grid of numbers. A convolution slides a small filter over it to make a feature map, and sharing that filter everywhere keeps the knobs few. With ReLU, pooling and depth, the layers build up from edges to objects. Skip connections let networks go over a hundred layers deep, and transfer learning reuses them when the new images look like the old. But a tiny, well-aimed nudge can still fool them.

So far, our networks learned from labeled examples. Next, one learns by trial and error, from rewards. That's episode thirteen: learning by reward, with reinforcement learning. This is Just Known. From unknown, to known.

Photos: scikit-learn sample images, CC BY 2.0 (creativecommons.org/licenses/by/2.0). china.jpg by danielbuechele on Flickr; flower.jpg from Flickr user vultilion (per the links in scikit-learn's README). Resized and cropped to 224 x 224; in the attack, every color value changed by at most 2 of 255.
Note: results are from one run on our laptop's CPU; the fine-tuning and attack numbers vary slightly between runs. Pictures marked "illustration" are not data.
Narration voice generated with Google Gemini text-to-speech.