Implementing neural style transfer using PyTorch
Having discussed the internals of a neural style transfer system, we are all set to build one using PyTorch. In the form of an exercise, we will load a style and a content image. Then, we will load the pre-trained VGG model. After defining which layers to compute the style and content loss on, we will trim the model so that it only retains the relevant layers. Finally, we will train the neural style transfer model in order to refine the generated image epoch by epoch.
Loading the content and style images
In this exercise, we will only show the important parts of the code for demonstration purposes. To access the full code, go to https://github.com/PacktPublishing/Mastering-PyTorch/blob/master/Chapter07/neural_style_transfer.ipynb. Follow these steps:
- Firstly, we need to import the necessary libraries by running the following lines of code:
from PIL import Image import matplotlib.pyplot as pltimport torch import torch.nn...