<jupyter_start><jupyter_text>Neural Style TransferIf you are a photography enthusiast,you may be familiar with the filter.It can change the color style of photosso that landscape photos become sharperor portrait photos have whitened skins.However,one filter usually only changesone aspect of the photo.To apply an ideal styleto a photo,you probably need totry many different filter combinations.This process isas complex as tuning the hyperparameters of a model.In this section, we willleverage layerwise representations of a CNNto automatically apply the style of one imageto another image, i.e., *style transfer* :cite:`Gatys.Ecker.Bethge.2016`.This task needs two input images:one is the *content image* andthe other is the *style image*.We will use neural networksto modify the content imageto make it close to the style image in style.For example,the content image in :numref:`fig_style_transfer` is a landscape photo taken by usin Mount Rainier National Park in the suburbs of Seattle, while the style image is an oil paintingwith the theme of autumn oak trees.In the output synthesized image,the oil brush strokes of the style imageare applied, leading to more vivid colors,while preserving the main shape of the objectsin the content image.![Given content and style images, style transfer outputs a synthesized image.](../img/style-transfer.svg):label:`fig_style_transfer` Method:numref:`fig_style_transfer_model` illustratesthe CNN-based style transfer method with a simplified example.First, we initialize the synthesized image,for example, into the content image.This synthesized image is the only variable that needs to be updated during the style transfer process,i.e., the model parameters to be updated during training.Then we choose a pretrained CNNto extract image features and freeze itsmodel parameters during training.This deep CNN uses multiple layersto extracthierarchical features for images.We can choose the output of some of these layers as content features or style features.Take :numref:`fig_style_transfer_model` as an example.The pretrained neural network here has 3 convolutional layers,where the second layer outputs the content features,and the first and third layers output the style features.![CNN-based style transfer process. Solid lines show the direction of forward propagation and dotted lines show backward propagation. ](../img/neural-style.svg):label:`fig_style_transfer_model`Next, we calculate the loss function of style transfer through forward propagation (direction of solid arrows), and update the model parameters (the synthesized image for output) through backpropagation (direction of dashed arrows).The loss function commonly used in style transfer consists of three parts:(i) *content loss* makes the synthesized image and the content image close in content features;(ii) *style loss* makes the synthesized image and style image close in style features;and (iii) *total variation loss* helps to reduce the noise in the synthesized image.Finally, when the model training is over, we output the model parameters of the style transfer to generatethe final synthesized image.In the following,we will explain the technical details of style transfer via a concrete experiment. [**Reading the Content and Style Images**]First, we read the content and style images.From their printed coordinate axes,we can tell that these images have different sizes.<jupyter_code>%matplotlib inline
import torch
import torchvision
from torch import nn
from d2l import torch as d2l

d2l.set_figsize()
content_img = d2l.Image.open('../img/rainier.jpg')
d2l.plt.imshow(content_img);
style_img = d2l.Image.open('../img/autumn-oak.jpg')
d2l.plt.imshow(style_img);<jupyter_output><empty_output><jupyter_text>[**Preprocessing and Postprocessing**]Below, we define two functions for preprocessing and postprocessing images.The `preprocess` function standardizeseach of the three RGB channels of the input image and transforms the results into the CNN input format.The `postprocess` function restores the pixel values in the output image to their original values before standardization.Since the image printing function requires that each pixel has a floating point value from 0 to 1,we replace any value smaller than 0 or greater than 1 with 0 or 1, respectively.<jupyter_code>rgb_mean = torch.tensor([0.485, 0.456, 0.406])
rgb_std = torch.tensor([0.229, 0.224, 0.225])

def preprocess(img, image_shape):
    transforms = torchvision.transforms.Compose([
        torchvision.transforms.Resize(image_shape),
        torchvision.transforms.ToTensor(),
        torchvision.transforms.Normalize(mean=rgb_mean, std=rgb_std)])
    return transforms(img).unsqueeze(0)

def postprocess(img):
    img = img[0].to(rgb_std.device)
    img = torch.clamp(img.permute(1, 2, 0) * rgb_std + rgb_mean, 0, 1)
    return torchvision.transforms.ToPILImage()(img.permute(2, 0, 1))<jupyter_output><empty_output><jupyter_text>[**Extracting Features**]We use the VGG-19 model pretrained on the ImageNet dataset to extract image features :cite:`Gatys.Ecker.Bethge.2016`.<jupyter_code>pretrained_net = torchvision.models.vgg19(pretrained=True)<jupyter_output><empty_output><jupyter_text>In order to extract the content features and style features of the image, we can select the output of certain layers in the VGG network.Generally speaking, the closer to the input layer, the easier to extract details of the image, and vice versa, the easier to extract the global information of the image. In order to avoid excessivelyretaining the details of the content image in the synthesized image,we choose a VGG layer that is closer to the output as the *content layer* to output the content features of the image.We also select the output of different VGG layers for extracting local and global style features.These layers are also called *style layers*.As mentioned in :numref:`sec_vgg`,the VGG network uses 5 convolutional blocks.In the experiment, we choose the last convolutional layer of the fourth convolutional block as the content layer, and the first convolutional layer of each convolutional block as the style layer.The indices of these layers can be obtained by printing the `pretrained_net` instance.<jupyter_code>style_layers, content_layers = [0, 5, 10, 19, 28], [25]<jupyter_output><empty_output><jupyter_text>When extracting features using VGG layers,we only need to use all thosefrom the input layer to the content layer or style layer that is closest to the output layer.Let us construct a new network instance `net`, which only retains all the VGG layers to beused for feature extraction.<jupyter_code>net = nn.Sequential(*[pretrained_net.features[i] for i in
                      range(max(content_layers + style_layers) + 1)])<jupyter_output><empty_output><jupyter_text>Given the input `X`, if we simply invokethe forward propagation `net(X)`, we can only get the output of the last layer.Since we also need the outputs of intermediate layers,we need to perform layer-by-layer computation and keepthe content and style layer outputs.<jupyter_code>def extract_features(X, content_layers, style_layers):
    contents = []
    styles = []
    for i in range(len(net)):
        X = net[i](X)
        if i in style_layers:
            styles.append(X)
        if i in content_layers:
            contents.append(X)
    return contents, styles<jupyter_output><empty_output><jupyter_text>Two functions are defined below:the `get_contents` function extracts content features from the content image,and the `get_styles` function extracts style features from the style image.Since there is no need to update the model parameters of the pretrained VGG during training,we can extract the content and the style featureseven before the training starts.Since the synthesized imageis a set of model parameters to be updatedfor style transfer,we can only extract the content and style features of the synthesized image by calling the `extract_features` function during training.<jupyter_code>def get_contents(image_shape, device):
    content_X = preprocess(content_img, image_shape).to(device)
    contents_Y, _ = extract_features(content_X, content_layers, style_layers)
    return content_X, contents_Y

def get_styles(image_shape, device):
    style_X = preprocess(style_img, image_shape).to(device)
    _, styles_Y = extract_features(style_X, content_layers, style_layers)
    return style_X, styles_Y<jupyter_output><empty_output><jupyter_text>[**Defining the Loss Function**]Now we will describe the loss function for style transfer. The loss function consists ofthe content loss, style loss, and total variation loss. Content LossSimilar to the loss function in linear regression,the content loss measures the differencein content featuresbetween the synthesized image and the content image viathe squared loss function.The two inputs of the squared loss functionare bothoutputs of the content layer computed by the `extract_features` function.<jupyter_code>def content_loss(Y_hat, Y):
    # We detach the target content from the tree used to dynamically compute
    # the gradient: this is a stated value, not a variable. Otherwise the loss
    # will throw an error.
    return torch.square(Y_hat - Y.detach()).mean()<jupyter_output><empty_output><jupyter_text>Style LossStyle loss, similar to content loss,also uses the squared loss function to measure the difference in style between the synthesized image and the style image.To express the style output of any style layer,we first use the `extract_features` function tocompute the style layer output.Suppose that the output has1 example, $c$ channels,height $h$, and width $w$,we can transform this output intomatrix $\mathbf{X}$ with $c$ rows and $hw$ columns.This matrix can be thought of asthe concatenation of$c$ vectors $\mathbf{x}_1, \ldots, \mathbf{x}_c$,each of which has a length of $hw$.Here, vector $\mathbf{x}_i$ represents the style feature of channel $i$.In the *Gram matrix* of these vectors $\mathbf{X}\mathbf{X}^\top \in \mathbb{R}^{c \times c}$, element $x_{ij}$ in row $i$ and column $j$ is the dot product of vectors $\mathbf{x}_i$ and $\mathbf{x}_j$.It represents the correlation of the style features of channels $i$ and $j$.We use this Gram matrix to represent the style output of any style layer.Note that when the value of $hw$ is larger,it likely leads to larger values in the Gram matrix.Note also that the height and width of the Gram matrix are both the number of channels $c$.To allow style loss not to be affectedby these values,the `gram` function below dividesthe Gram matrix by the number of its elements, i.e., $chw$.<jupyter_code>def gram(X):
    num_channels, n = X.shape[1], X.numel() // X.shape[1]
    X = X.reshape((num_channels, n))
    return torch.matmul(X, X.T) / (num_channels * n)<jupyter_output><empty_output><jupyter_text>Obviously,the two Gram matrix inputs of the squared loss function for style loss are based onthe style layer outputs forthe synthesized image and the style image.It is assumed here that the Gram matrix `gram_Y` based on the style image has been precomputed.<jupyter_code>def style_loss(Y_hat, gram_Y):
    return torch.square(gram(Y_hat) - gram_Y.detach()).mean()<jupyter_output><empty_output><jupyter_text>Total Variation LossSometimes, the learned synthesized imagehas a lot of high-frequency noise,i.e., particularly bright or dark pixels.One common noise reduction method is*total variation denoising*.Denote by $x_{i, j}$ the pixel value at coordinate $(i, j)$.Reducing total variation loss$$\sum_{i, j} \left|x_{i, j} - x_{i+1, j}\right| + \left|x_{i, j} - x_{i, j+1}\right|$$makes values of neighboring pixels on the synthesized image closer.<jupyter_code>def tv_loss(Y_hat):
    return 0.5 * (torch.abs(Y_hat[:, :, 1:, :] - Y_hat[:, :, :-1, :]).mean() +
                  torch.abs(Y_hat[:, :, :, 1:] - Y_hat[:, :, :, :-1]).mean())<jupyter_output><empty_output><jupyter_text>Loss Function[**The loss function of style transfer is the weighted sum of content loss, style loss, and total variation loss**].By adjusting these weight hyperparameters,we can balance amongcontent retention,style transfer,and noise reduction on the synthesized image.<jupyter_code>content_weight, style_weight, tv_weight = 1, 1e3, 10

def compute_loss(X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram):
    # Calculate the content, style, and total variance losses respectively
    contents_l = [content_loss(Y_hat, Y) * content_weight for Y_hat, Y in zip(
        contents_Y_hat, contents_Y)]
    styles_l = [style_loss(Y_hat, Y) * style_weight for Y_hat, Y in zip(
        styles_Y_hat, styles_Y_gram)]
    tv_l = tv_loss(X) * tv_weight
    # Add up all the losses
    l = sum(10 * styles_l + contents_l + [tv_l])
    return contents_l, styles_l, tv_l, l<jupyter_output><empty_output><jupyter_text>[**Initializing the Synthesized Image**]In style transfer,the synthesized image is the only variable that needs to be updated during training.Thus, we can define a simple model, `SynthesizedImage`, and treat the synthesized image as the model parameters.In this model, forward propagation just returns the model parameters.<jupyter_code>class SynthesizedImage(nn.Module):
    def __init__(self, img_shape, **kwargs):
        super(SynthesizedImage, self).__init__(**kwargs)
        self.weight = nn.Parameter(torch.rand(*img_shape))

    def forward(self):
        return self.weight<jupyter_output><empty_output><jupyter_text>Next, we define the `get_inits` function.This function creates a synthesized image model instance and initializes it to the image `X`.Gram matrices for the style image at various style layers, `styles_Y_gram`, are computed prior to training.<jupyter_code>def get_inits(X, device, lr, styles_Y):
    gen_img = SynthesizedImage(X.shape).to(device)
    gen_img.weight.data.copy_(X.data)
    trainer = torch.optim.Adam(gen_img.parameters(), lr=lr)
    styles_Y_gram = [gram(Y) for Y in styles_Y]
    return gen_img(), styles_Y_gram, trainer<jupyter_output><empty_output><jupyter_text>[**Training**]When training the model for style transfer,we continuously extractcontent features and style features of the synthesized image, and calculate the loss function.Below defines the training loop.<jupyter_code>def train(X, contents_Y, styles_Y, device, lr, num_epochs, lr_decay_epoch):
    X, styles_Y_gram, trainer = get_inits(X, device, lr, styles_Y)
    scheduler = torch.optim.lr_scheduler.StepLR(trainer, lr_decay_epoch, 0.8)
    animator = d2l.Animator(xlabel='epoch', ylabel='loss',
                            xlim=[10, num_epochs],
                            legend=['content', 'style', 'TV'],
                            ncols=2, figsize=(7, 2.5))
    for epoch in range(num_epochs):
        trainer.zero_grad()
        contents_Y_hat, styles_Y_hat = extract_features(
            X, content_layers, style_layers)
        contents_l, styles_l, tv_l, l = compute_loss(
            X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram)
        l.backward()
        trainer.step()
        scheduler.step()
        if (epoch + 1) % 10 == 0:
            animator.axes[1].imshow(postprocess(X))
            animator.add(epoch + 1, [float(sum(contents_l)),
                                     float(sum(styles_l)), float(tv_l)])
    return X<jupyter_output><empty_output><jupyter_text>Now we [**start to train the model**].We rescale the height and width of the content and style images to 300 by 450 pixels.We use the content image to initialize the synthesized image.<jupyter_code>device, image_shape = d2l.try_gpu(), (300, 450)  # PIL Image (h, w)
net = net.to(device)
content_X, contents_Y = get_contents(image_shape, device)
_, styles_Y = get_styles(image_shape, device)
output = train(content_X, contents_Y, styles_Y, device, 0.3, 500, 50)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0794743824625687, 'fraction_numerical': 0.007960073283214353, 'mean_word_length': 4.93328335832084, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '395720', 'license': 'MIT', 'n_tokens_mistral': 4543, 'n_tokens_neox': 4337, 'n_words': 1786, 'path': 'd2l/pytorch/chapter_computer-vision/neural-style.ipynb', 'repo_name': 'nilesh-patil/dive-into-deeplearning', 'size': '15829'}