Path: blob/master/examples/generative/md/neural_style_transfer.md
3508 views
Neural style transfer
Author: fchollet
Date created: 2016/01/11
Last modified: 2020/05/02
Description: Transferring the style of a reference image to target image using gradient descent.
Introduction
Style transfer consists in generating an image with the same "content" as a base image, but with the "style" of a different picture (typically artistic). This is achieved through the optimization of a loss function that has 3 components: "style loss", "content loss", and "total variation loss":
The total variation loss imposes local spatial continuity between the pixels of the combination image, giving it visual coherence.
The style loss is where the deep learning keeps in --that one is defined using a deep convolutional neural network. Precisely, it consists in a sum of L2 distances between the Gram matrices of the representations of the base image and the style reference image, extracted from different layers of a convnet (trained on ImageNet). The general idea is to capture color/texture information at different spatial scales (fairly large scales --defined by the depth of the layer considered).
The content loss is a L2 distance between the features of the base image (extracted from a deep layer) and the features of the combination image, keeping the generated image close enough to the original one.
Reference: A Neural Algorithm of Artistic Style
Setup
Image preprocessing / deprocessing utilities
Compute the style transfer loss
First, we need to define 4 utility functions:
gram_matrix
(used to compute the style loss)The
style_loss
function, which keeps the generated image close to the local textures of the style reference imageThe
content_loss
function, which keeps the high-level representation of the generated image close to that of the base imageThe
total_variation_loss
function, a regularization loss which keeps the generated image locally-coherent
Next, let's create a feature extraction model that retrieves the intermediate activations of VGG19 (as a dict, by name).
Add a tf.function decorator to loss & gradient computation
To compile it, and thus make it fast.
The training loop
Repeatedly run vanilla gradient descent steps to minimize the loss, and save the resulting image every 100 iterations.
We decay the learning rate by 0.96 every 100 steps.