ArticleslgStudy

computer science

Neural style transfer

Neural style transfer is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Neural style transfer rather than just read about it. In short: Neural style transfer (NST) software algorithms are able to manipulate digital images, or videos, in order to adopt the appearance or visual style of another image. NST algorithms are characterized by their use of deep neural networks for the sake of image transformation.

Neural style transfer — main illustration
Neural style transfer — illustration

Key takeaways

  • Neural style transfer belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Neural style transfer to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Neural style transfer from memory before moving on to harder problems.

Reference excerpt

Neural style transfer (NST) software algorithms are able to manipulate digital images, or videos, in order to adopt the appearance or visual style of another image. NST algorithms are characterized by their use of deep neural networks for the sake of image transformation. Common uses for NST are the creation of artificial artwork from photographs, for example by transferring the appearance of famous paintings to user-supplied photographs. Several notable mobile apps use NST techniques for this purpose, including DeepArt and Prisma. This method has been used by artists and designers around the globe to develop new artwork based on existent style(s).

History NST is an example of image stylization, a problem studied for over two decades within the field of non-photorealistic rendering. The first two example-based style transfer algorithms were image analogies and image quilting. Both of these methods were based on patch-based texture synthesis algorithms. Given a training pair of images–a photo and an artwork depicting that photo–a transformation could be learned and then applied to create new artwork from a new photo, by analogy. If no training photo was available, it would need to be produced by processing the input artwork; image quilting did not require this processing step, though it was demonstrated on only one style. NST was first published in the paper "A Neural Algorithm of Artistic Style" by Leon Gatys et al., originally released to ArXiv 2015, and subsequently accepted by the peer-reviewed CVPR conference in 2016. The original paper used a VGG-19 architecture that has been pre-trained to perform object recognition using the ImageNet dataset. In 2017, Google AI introduced a method that allows a single deep convolutional style transfer network to learn multiple styles at the same time. This algorithm permits style interpolation in real-time, even when done on video media.

Mathematics

This section closely follows the original paper.

Overview The idea of Neural Style Transfer (NST) is to take two images—a content image p→ and a style image a→—and generate a third image x→ that minimizes a weighted combination of two loss functions: a content loss ℒcontent (p→,x→) and a style loss ℒstyle (a→,x→). The total loss is a linear sum of the two:ℒNST(p→,a→,x→)=αℒcontent(p→,x→)+βℒstyle(a→,x→)By jointly minimizing the content and style losses, NST generates an image that blends the content of the content image with the style of the style image. Both the content loss and the style loss measures the similarity of two images. The content similarity is the weighted sum of squared-differences between the neural activations of a single convolutional neural network (CNN) on two images. The style similarity is the weighted sum of Gram matrices within each layer (see below for details). The original paper used a VGG-19 CNN, but the method works for any CNN.

Symbols Let x→ be an image input to a CNN. Let Fl∈ℝNl×Ml be the matrix of filter responses in layer l to the image x→, where:

Nl is the number of filters in layer l ; Ml is the height times the width (i.e. number of pixels) of each filter in layer l ; Fijl(x→) is the activation of the ith filter at position j in layer l. A given input image x→ is encoded in each layer of the CNN by the filter responses to that image, with higher layers encoding more global features, but losing details on local features.

Content loss Let p→ be an original image. Let x→ be an image that is generated to match the content of p→. Let Pl be the matrix of filter responses in layer l to the image p→. The content loss is defined as the squared-error loss between the feature representations of the generated image and the content image at a chosen layer l of a CNN: ℒcontent (p→,x→,l)=12∑i,j(Aijl(x→)−Aijl(p→))2 where Aijl(x→) and Aijl(p→) are the activations of the ith filter at position j in layer l for the generated and content images, respectively. Minimizing this loss encourages the generated image to have similar content to the content image, as captured by the feature activations in the chosen layer. The total content loss is a linear sum of the content losses of each layer: ℒcontent (p→,x→)=∑lvlℒcontent (p→,x→,l), where the vl are positive real numbers chosen as hyperparameters.

Style loss The style loss is based on the Gram matrices of the generated and style images, which capture the correlations between different filter responses at different layers of the CNN: ℒstyle (a→,x→)=∑l=0LwlEl, where El=14Nl2Ml2∑i,j(Gijl(x→)−Gijl(a→))2.Here, Gijl(x→) and Gijl(a→) are the entries of the Gram matrices for the generated and style images at layer l. Explicitly, Gijl(x→)=∑kFikl(x→)Fjkl(x→) Minimizing this loss encourages the generated image to have similar style characteristics to the style image, as captured by the correlations between feature responses in each layer. The idea is that activation pattern correlations between filters in a single layer captures the "style" on the order of the receptive fields at that layer. Similarly to the previous case, the wl are positive real numbers chosen as hyperparameters.

Hyperparameters In the original paper, they used a particular choice of hyperparameters. The style loss is computed by wl=0.2 for the outputs of layers conv1_1, conv2_1, conv3_1, conv4_1, conv5_1 in the VGG-19 network, and zero otherwise. The content loss is computed by wl=1 for conv4_2, and zero otherwise. The ratio α/β∈[5,50]×10−4.

Training Image x→ is initially approximated by adding a small amount of white noise to input image p→ and feeding it through the CNN. Then we successively backpropagate this loss through the network with the CNN weights fixed in order to update the pixels of x→. After several thousand epochs of training, an x→ (hopefully) emerges that matches the style of a→ and the content of p→. As of 2017, when implemented on a GPU, it takes a few minutes to converge.

… excerpt ends here. Continue reading the full article.

Illustrations

Neural style transfer illustration
Neural style transfer illustration
Neural style transfer illustration
Neural style transfer: Solid lines show the direction of forward propagation of data. Dotted lines show the backward propagation of loss gradient.[7]
Solid lines show the direction of forward propagation of data. Dotted lines show the backward propagation of loss gradient.[7]

Worked examples

Example 1 — a first encounter with Neural style transfer

Start with the simplest possible case. Write down what Neural style transfer claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Neural style transfer before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Neural style transfer ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Neural style transfer

In research
Neural style transfer appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Neural style transfer in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Neural style transfer is common in secondary-school and first-year university syllabi. It links to neighbouring topics Algorithms, Deep learning, so understanding it makes those chapters shorter.
In everyday life
Look for Neural style transfer outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Neural style transfer” →

How to study Neural style transfer in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Neural style transfer means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Neural style transfer out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Neural style transfer in simple terms?

Neural style transfer (NST) software algorithms are able to manipulate digital images, or videos, in order to adopt the appearance or visual style of another image. NST algorithms are characterized by their use of deep neural networks for the sake of image transformation.

Why does Neural style transfer matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Neural style transfer?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Neural style transfer.

Tags

  • Algorithms
  • Deep learning

Keep exploring