/Neural-style-transfer-pytorch

This project is basic neural style transfer implement with pytorch

Primary LanguageJupyter Notebook

Neural-style-transfer-pytorch

Overview

1. Build model to extract features

  • Using pretrained VGG19 model
  • Using only layer index 0, 5, 10, 19, 28

2. Load images

  • Load content image
  • Load style image
  • Generate random white noise target image

=> Resize style and target image to size of content image

3. Init hyperparameters and optimizer:

  • style_weight: 1000,
  • content_weight: 1,
  • learning_rate: 0.001
  • epochs: 6000

Using Adam optimizer with parameters is target_images

4. Training:

  • Calculate content loss

$$content_loss = \frac{1}{2}(content_img - target_img)^2$$

  • Calculate style loss:

Gram matrix:

$$G = \frac{1}{chw}(target_img.target_img^T)$$

$$A = \frac{1}{chw}(style_img.style_img^T)$$

$$style_loss = \frac{1}{2ch*w}.(G - A)^2$$