Assignment 4 - Neural Style Transfer
(Bells and Whistles at the end)
Part 1 : Content Reconstruction
Optimizing Content Loss at Different Layers
Here we attempt to reconstruct the original image by defining an L2 loss between the source (noise) and target (input) image. However, instead of directly minimizing the loss over the images, we pass the images through a pre-trained network and attempt to minimize the loss at different stages of the network.
Procedure:
- Define source and target images
- Download pre-trained network which acts as feature extractor
- Get network output at L’th layer
- Define L2 loss between L’th layer output of source and target image
L2 Loss
Experiment : Optimizing Content Loss at Different Layers
![]() |
|---|
| Original Content Image |
The L2 minimization was done between model output of noise and content image at different convolution layers as shown below:
| Conv 2 | Conv 4 |
|---|---|
![]() |
![]() |
| Conv 8 | Conv 12 |
|---|---|
![]() |
![]() |
As seen from above, reconstruction works best at Conv2 and Conv4. In deeper layers, the some high frequency details are lost and the image appears grainy.
Experiment : Reconstruction from Random Noise
Taking two random images generated from a normal distribution as input, we reconstructed the original image using the L2 loss at Conv2.
| Random Noise Image 1 | Content Image | Reconstructed Content Image |
|---|---|---|
![]() |
![]() |
![]() |
| Random Noise Image 1 | Content Image | Reconstructed Content Image |
![]() |
![]() |
![]() |
As seen above, in both cases, optimizing at Conv2 allows us to retreive almost the original image.
Part 2 : Texture Synthesis
Texture synthesis relies on style loss, which quantifies the dissimilarity between the styles of two images. In the context of this assignment, the Gram matrix serves as a measure of style. The Gram matrix captures the correlation between channel dimensions for each pixel location (H, W).
Specifically, for the
Resulting in a shape of (N, K, K). The goal is to minimize the difference between the Gram matrices of our optimized and predicted features.
Experiment : Texture loss at different layers
![]() |
|---|
| Original Style Image |
The Style loss was then applied applied by combining the outpts at different layers. The results are shown below:
| Conv1 + Conv2 + Conv3 + Conv4 + Conv5 | Conv3 + Conv4 + Conv5 + Conv6 + Conv7 |
|---|---|
![]() |
![]() |
| Conv5 + Conv6 + Conv7 + Conv8 + Conv9 | Conv1 + Conv2 + Conv5 + Conv8 + Conv9 |
|---|---|
![]() |
![]() |
- From above images, it can be seen that
Conv1 + Conv2 + Conv3 + Conv4 + Conv5yields the best results. - When combining earlier and deeper layers such as
Conv1 + Conv2 + Conv5 + Conv8 + Conv9we get similar style, but some colors appear incorrect
Experiment : Reconstruction from Noise based on Style Loss
- Starting from two images generated as random noise, we try to minimize the style loss
- I used the
Conv1 + Conv2 + Conv3 + Conv4 + Conv5configuration for style loss
| Random Noise Image 1 | Style Image | Noise Image After Optimization |
|---|---|---|
![]() |
![]() |
![]() |
| Random Noise Image 2 | Style Image | Noise Image After Optimization |
![]() |
![]() |
![]() |
The sythesized styles (‘noise image after optimization’) are globally similar but lack any pixel-to-pixel correspondence.
Part 3 : Style Transfer
Style Transfer is implemented by adding the Reconstruction and Style losses while optimizing the input image.
Experiment : Hyperparameter Tuning
For this experiment, I tried different weights on style loss while keeping content loss fixed. The results are shown below:
| Hyperparameters | Content Loss at Conv2 | Content Loss at Conv4 |
|---|---|---|
| content weight = |
![]() |
![]() |
| content weight = |
![]() |
![]() |
| content weight = |
![]() |
![]() |
From the above examples, I think:
- Content weight =
- Style weight =
Would work best for style transfer
Experiment : Style Transfer Results on Different Images
| Content / Style | ![]() |
![]() |
|---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Experiment : Style Transfer with Different Initializations
Initializing the input image was tested in two ways:
- Input image was same as content image
- Input image was random noise
| Content Image | Style Image | Content Initialization | Style Initialization |
|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
| Runtime = 13.46s | Runtime : 16.33s |
Content initialization is clearly better in maintaining structure. Further the runtime was also considerably better for content intialization.
Experiment : Style Transfer on Favorite Images
| Content Image | Style Image | Style Transfer |
|---|---|---|
![]() |
![]() |
![]() |
| Content Image | Style Image | Style Transfer |
![]() |
![]() |
![]() |
Bells and Whistles
Applying Style Transfer on Outputs of Previous Homeworks
I used the images from the poisson blending task below:
| Content Image | Style Image | Style Transfer |
|---|---|---|
![]() |
![]() |
![]() |













.png)

.png)

.png)


















.png)

