Neural style transfer: separating the content of a picture from its style
10 questions on content and style error terms, style matrices built from channel co-occurrence, the choice of layers and generating an image by gradient descent.
- 10 questions
- Stopwatch
- Instant feedback
Sample questions
Play now ↑Answers stay hidden. Start the quiz to play them.
Sample 1
A chosen layer has 64 channels. What are the dimensions of its style matrix?
- 64 by 1
- The size of the image
- 64 by 64, one entry for each pair of channels
How it works
- One question per screen. An answer is final once sent.
- You see right away whether you were right, with a short explanation.
- The stopwatch runs: on equal scores, the faster player ranks first.
- Only your best attempt counts, so replaying is never risky.
About this quiz
Imagine handing a photograph of your street to a painter and asking for it back in the manner of a famous oil painting, with the same buildings and trees but the brushwork and colours of the master. For a human artist this is a skilled commission. A convolutional network can be made to do something similar, using no new training at all. The trick is to take a network that has already learned to recognise objects, to measure how a candidate image differs from the photograph in its content and from the painting in its style, and to adjust the candidate until both differences are small.
This quiz explains how that works. You will see that the picture being made is not produced by a network at all, but found by gradient descent on its pixels, starting from random noise, with the trained network serving only as a measuring device. You will see how the error is built from two terms and how the answer to the question what is content and what is style comes from the form of those terms and not from any definition. Content is measured by comparing the responses of a chosen layer at corresponding positions in the two images. Style is measured by looking at which features tend to appear together, anywhere in the picture, collected in a table with one row and one column for every channel.
The questions ask what the shape of that table is, why content is position-specific while style is not, and what the choice of layer does to the result, since early layers care about edges and fine texture and later ones about whole objects. You will see why several layers are usually combined for the style term, and how the weights that balance everything are chosen, which turns out to be by looking at the results.
Some points often surprise people. The network is not trained to do style transfer, and nothing in it understands art. The weights of the trained network stay untouched throughout. And the settings are not derived from theory but tuned by eye, which is why two people can get different results from the same two pictures. The method is a neat demonstration of a general principle: a network trained for one purpose can supply the measuring stick for another. Features that a recogniser has learned in order to tell objects apart turn out to describe both what is in a picture and how it looks, and this is exploited by defining an error on those features and then optimising a picture against it. The same idea, using a trained network's features as a measure of similarity, underlies many later techniques for judging and generating images, so the example repays careful study.
The quiz suits learners curious about creative uses of convolutional networks, and practitioners interested in how features can define a loss. It uses only simple counting. There are 10 questions, each with an explanation, and it takes about four minutes.
Questions and answers
›How long does the quiz take?
About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation follows every answer, so the quiz works as a short lesson on the method.
›Do I need to know how convolutional networks work?
It helps to know that a convolutional network has layers that produce feature maps with several channels, and that early layers respond to simple features while later ones respond to complex entities. The quiz recalls these ideas as it goes.
›What is neural style transfer?
It is a method of producing an image whose content comes from one picture and whose style comes from another. Starting from a randomly initialised image, gradient descent changes the pixels until an error function made of a content term and a style term is small, using a trained convolutional network to measure both.
›How is content measured?
By choosing a convolutional layer and comparing its pre-activations for the generated image with those for the content image, using a sum-of-squares error. Because the comparison is made at the same positions, the generated image is encouraged to have the same features in the same places as the content image.
›How is style measured?
By the co-occurrence of features across channels. For a chosen layer, you multiply the activations of each pair of channels at every position and add up over all positions, which gives a square table with a row and a column for every channel. Two images have similar style if their tables are similar. Summing over positions makes the measure independent of where features appear.
›Why is style averaged over positions while content is not?
For content, the generated image should have particular features in particular places, matching the original picture. For style, it only needs the same kinds of characteristics, such as certain edges accompanying certain colours, wherever they occur. Averaging over locations discards the layout and keeps the statistical flavour of the picture.
›What does the choice of layer do?
Earlier layers respond to low-level features such as edges, so a content term built on them asks the output to match those details. Later layers respond to complex structures or whole objects, so a content term built on them allows more freedom in the fine detail while keeping the overall arrangement of objects.
›How are the weights in the total error chosen?
The coefficients that balance the layers within the style term, and the style term against the content term, are adjusted empirically using subjective judgement, since there is no objective measure of a pleasing image. Using several layers for the style term tends to give more attractive results than using just one.