2.**Loading the model**: Oxford's VGG created a model architecture that performs outstandingly well on style transfer problems, known as the VGG network. They also made the model's parameters available to anyone so that the training process of the model could be shortened or skipped (this is what a pretrained model is for).
4.**Defining the optimization problem**: As with any other supervised problem, it is necessary to define a loss function, which will have the responsibility of measuring the difference between the output and inputs. Unlike other supervised problems, the task of style transfer requires you to define three different loss functions, all of which should be minimized during the training process. The three loss functions are explained here:
2.Set the image size to be used for both images. Also, set the transformations to be performed over the images, which should include resizing the images, converting them into tensors, and normalizing them:
3. Define a function that will receive the image path as input and use **PIL** to open the image. Next, it should apply the transformations to the image:
4.Call the function to load the content and style images. Use the dog image as the content image and the Matisse image as the style image, both of which are available in this book's GitHub repository:
2.`vgg19.classifier`: This refers to the linear layers (also known as fully connected layers) that are located at the end of the network, including their parameters. These layers are the ones that perform the classification of the image into one of the label classes, for instance, recognizing the type of animal in an image.
2.Print the architecture of the model we loaded in the previous exercise. This will help us identify the relevant layers so that we can perform the style transfer task:
3.Create a dictionary for mapping the index of the relevant layers (keys) to a name (values). This will facilitate the process of calling relevant layers in the future:
4.Create a function that will extract the relevant features (features extracted from the relevant layers only) from an input image. Name it **features_extractor** and make sure it takes the image, the model, and the dictionary we created previously as inputs:
6.Perform the gram matrix calculation over the style features. Consider that the style features were obtained from different layers, which is why different gram matrices should be created, one for each layer's output:
7.Create an initial target image. This image will be compared against the content and style images later and be changed until the desired similarity is achieved:
8.Using the **tensor2image** function we created during the first exercise of this chapter, plot the target image, which should look the same as the content image:
3.Define the weights associated with the content and style losses:
3.定义与内容和风格损失相关的权重。
```py
alpha = 1
beta = 1e5
```
4.Define the number of iteration steps, as well as the optimization algorithm. We can also set the number of iterations if we want to see a plot of the image that has been created at that point:
6.Plot both the content and target images to compare the results. This can be achieved by using the **tensor2image** function, which we created in the previous exercises, in order to convert the tensors into PIL images that can be printed using **matplotlib**: