irasin / Pytorch_AdaIN

Pytorch implementation from scratch of [Arbitrary Style Transfer in Real-time with Adaptive Instance Normalization [Huang+, ICCV2017]]
GNU General Public License v3.0
133 stars 26 forks source link

About the denorm when save the images #5

Closed hello-trouble closed 4 years ago

hello-trouble commented 4 years ago

Hello , irasin. Thank you very much for your excellent project with pytorch . I am very interested in this project. However , I am puzzled about the denorm operation in training , with torch.no_grad(): out = model.generate(content, style) content = denorm(content, device) style = denorm(style, device) out = denorm(out, device) res = torch.cat([content, style, out], dim=0) res = res.to('cpu') save_image(res, f'{image_dir}/{e}epoch{i}_iteration.png', nrow=args.batch_size) I found that the out is not satisfactory without denorm operation. But I can't understand why use the denorm here ? when we should add this operation? Thank you for your project and looking for your help . Good weekend for you.

irasin commented 4 years ago

Hi, @13462877152.

Thank you for your attention.

As you know, AdaIN uses vgg as encoder to extract features from input images, and uses inversed vgg as decoder to generate the final image. However, because you use vgg here, you must do normalization on the input images as bellow.

# should normalize the tensor after transforms.ToTensor()
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

So, as a result, before the output image is going to be saved, it need to be denormalized. Because we calculate the loss based on the normalized images like bellow during training.

loss_c = self.calc_content_loss(output_features, t)
loss_s = self.calc_style_loss(output_middle_features, style_middle_features)
loss = loss_c + lam * loss_s

That is the reason why we need denorm here.

If you have any other question, please feel free to let me know. Have a good day~

hello-trouble commented 4 years ago

Thank you very much for your help . That's great .