Neural Style Transfer

Neural style transfer renders the content of a photograph in the style of an artwork. It comes from Gatys, Ecker, Bethge (arXiv 2015), “A Neural Algorithm of Artistic Style”. The method optimizes the pixels of a new image through a fixed pretrained CNN (VGG). Content means the spatial arrangement of deep CNN features. Style means the texture statistics of the Gatys texture model, the Gram matrices.

Let hold the feature maps of at layer , each flattened to positions, and let be their Gram matrix. Let be the photo’s feature maps and the artwork’s Gram matrices. The two losses are

The content loss uses one deep layer (a conv4 layer on the slide, conv4_2 in the paper). The style loss sums over several layers (conv1_1 to conv5_1 in the paper) with weights . The image minimizes

where set the balance between the two goals. Only the ratio affects the minimizer. The optimization starts from white noise and runs gradient descent on with respect to the pixels.

The content loss compares features position by position, so it keeps the photo’s layout. The style loss sums over positions, so it ignores layout and keeps colors, strokes and local patterns. With the method reduces to Gatys texture synthesis of the artwork. The lecture places it next to the slide title “ImageNet recognition is just texture recognition” (Gatys et al., 2017). The point is that CNN classifiers lean heavily on the same texture statistics.

Constants in the Losses

The slide writes both losses without constants. The paper puts in front of the content loss and divides each by . These constants change the scale of each term, so an value from the paper only makes sense with the paper’s normalization.

Intuition

Texture synthesis discards all positions, so it can reproduce a painting’s brush strokes but not a scene. A photograph’s deep CNN features keep positions. They record that a roof is here and a river is there, and they tolerate changes in the exact pixel colors. Style transfer asks for one image that satisfies both. Its deep features must line up with the photo’s, and its Gram matrices must match the painting’s. The two demands conflict, because the photo’s own Gram matrices differ from the painting’s. The weights and decide how that conflict is resolved.

The content layer is deep on purpose. Shallow features stay close to the pixels, so matching them reproduces the photo’s colors and fine texture and leaves no room for the painting. Deep features encode the layout of objects and allow local appearance to vary. The painting’s strokes can then repaint the surface without moving the houses. The figure shows the three images, the fixed network, and the two losses.

\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\definecolor{sty}{HTML}{D9483B}
\definecolor{con}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[>=latex, img/.style={draw, minimum width=1.9cm, minimum height=1.2cm, align=center}, net/.style={draw, mid, fill=mid!12, minimum width=1.9cm, minimum height=0.8cm, align=center}]
\node[img, sty] (a) at (0,0) {artwork\\ (style)};
\node[img] (x) at (4,0) {image $\hat{x}$\\ (optimized)};
\node[img, con] (p) at (8,0) {photo\\ (content)};
\node[net] (na) at (0,1.8) {fixed CNN};
\node[net] (nx) at (4,1.8) {fixed CNN};
\node[net] (np) at (8,1.8) {fixed CNN};
\draw[->, thick] (a) -- (na);
\draw[->, thick] (x) -- (nx);
\draw[->, thick] (p) -- (np);
\node[sty] (G) at (0,3.4) {$G^l$, all style layers};
\node (Gh) at (2.9,3.4) {$\hat{G}^l$};
\node (Fh) at (5.1,3.4) {$\hat{F}^l$};
\node[con] (F) at (8,3.4) {$F^l$, one deep layer};
\draw[->, thick, sty] (na) -- (G);
\draw[->, thick] (nx) -- (Gh);
\draw[->, thick] (nx) -- (Fh);
\draw[->, thick, con] (np) -- (F);
\node[sty, draw, rounded corners] (Ls) at (1.5,4.8) {$\mathcal{L}_{\text{style}} = \sum_l w_l E_l$};
\node[con, draw, rounded corners] (Lc) at (6.5,4.8) {$\mathcal{L}_{\text{content}}$};
\draw[sty, thick] (G) -- (Ls); \draw[sty, thick] (Gh) -- (Ls);
\draw[con, thick] (F) -- (Lc); \draw[con, thick] (Fh) -- (Lc);
\node[draw, rounded corners] (Lt) at (4,6.0) {$\mathcal{L}_{\text{total}} = \alpha\,\mathcal{L}_{\text{content}} + \beta\,\mathcal{L}_{\text{style}}$};
\draw[thick] (Ls) -- (Lt); \draw[thick] (Lc) -- (Lt);
\draw[->, thick, dashed, mid] (Lt.east) -- (10.3,6.0) -- (10.3,-1.1) -- (4,-1.1) -- (x.south);
\node[mid, anchor=west] at (4.2,-0.85) {gradient descent on the pixels of $\hat{x}$};
\end{tikzpicture}
\end{document}

Properties

(Content Layer Depth) The deeper the content layer, the more freedom the style term has to change local appearance.

The paper reconstructs the photo from noise using the content loss alone, at different layers. Reconstructions from the lower layers are almost identical to the photo. Reconstructions from the higher layers keep the arrangement of objects and lose exact pixel values. A high content layer therefore lets the artwork’s colors and strokes replace the photo’s surface while the scene layout survives.

(Style Layer Range) Including deeper style layers transfers larger-scale structure from the artwork.

Gram matrices from conv1_1 alone capture colors and fine grain. Adding conv2_1 through conv5_1 adds structures such as brush strokes and swirls. A feature in a deeper layer sees a larger patch of the image, so its co-occurrence statistics describe larger patterns. The paper weights the five style layers equally, .

(Reading and ) Multiplying and by the same positive factor leaves the best image unchanged.

The minimizer depends only on . A larger ratio stays closer to the photo, and a smaller ratio gives stronger stylization. The size of each loss depends on the normalization constants, the number of layers and the image size. A ratio tuned for one setup therefore needs retuning in another.

(Slow Per-Image Optimization) Every output needs its own optimization of many forward and backward passes through the network.

Gatys style transfer has no trained generator, so the pixel optimization restarts for every photo and artwork. A feed-forward network can instead be trained once to minimize a feature-based loss and then run in a single pass. The lecture shows this for super-resolution with a “semantic feature loss” that matches VGG feature covariances (Johnson et al. 2016). Losses built from CNN features are one answer to the loss-design problem raised in 8.3. Image Colorization. A learned discriminator, as in 8.4. Pix2Pix, is another.

(Texture Bias of Classifiers) A CNN can classify an image correctly from its texture statistics alone.

The lecture’s slide (Gatys et al., 2017) shows an image that keeps the local texture of a sheepdog photo but scrambles its layout. Fur and face fragments appear all over the image, with no coherent dog. The classifier still answers “Shetland Sheepdog”. Such a network relies mostly on local texture statistics, with global shape playing a smaller role. This is also why the features of a classification network make a good texture model.

Examples

(Easy) Turning the Two Knobs

A student runs style transfer with a photo of a riverside town and Van Gogh’s The Starry Night. The content layer is conv4_2 and the style layers are conv1_1 to conv5_1.

(Medium) Why the Style Loss Ignores Position

Use one toy layer with two feature maps over four positions for both losses. The photo gives and the painting gives , with Gram matrix :

The three candidates for are the photo’s , the painting’s , and shifted circularly by one position.