Learn Before
Synthesized Image as Model Parameter in Style Transfer
In neural style transfer, the synthesized image acts as the sole optimizable variable during the training process. Instead of updating the weights of a neural network, the synthesized image itself is treated as the model parameter. We can define a simple model, such as a SynthesizedImage class, where forward propagation simply returns these optimizable image parameters.
Before training begins, this synthesized image model must be initialized. It is typically initialized as a copy of the content image to preserve the original structural elements from the start. Additionally, static features required for the loss computation, such as the Gram matrices of the style image at various layers, are precomputed prior to the training loop to reduce computational overhead.
# PyTorch import torch from torch import nn 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 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
0
1
Tags
D2L
Dive into Deep Learning @ D2L