**16-726
Neural Style Transfer** **Anish Jain (anishaja)** Overview =========================================================================== In this project, we implement the Neural Style Transfer algorithm proposed by Gatys et al. in the paper [A Neural Algorithm of Artistic Style](https://arxiv.org/abs/1508.06576). The algorithm uses a pre-trained Convolutional Neural Network (CNN) to extract features from the content and style images. The content image is used to extract the content features and the style image is used to extract the style features. The algorithm then generates a new image that combines the content features of the content image and the style features of the style image. The algorithm uses an optimization-based approach to generate the new image. The optimization objective is to minimize the content loss between the content features of the new image and the content features of the content image, and the style loss between the style features of the new image and the style features of the style image. The algorithm uses a pre-trained CNN to extract the content and style features. We use the VGG19 model pre-trained on the ImageNet dataset to extract the features. The pretrained VGG19 model has multiple convolutional layers. Below is the architecture of the VGG19 model. ![VGG19 Architecture](vgg_19.png) Content Reconstruction =========================================================================== I optimize a random noise image to match the content features of the content image. The content features are extracted from the VGG19 model at the 4th convolutional layer. The content loss is computed as the mean squared error between the content features of the content image and the content features of the new image. The optimization objective is to minimize the content loss. Vary content loss layer --------------------------------------------------------------------------- I experimented with different layers to extract the content features. ![conv1_1](output/content_img_wally_conv_0/Reconstructed_Image.png) ![conv1_2](output/content_img_wally_conv_1/Reconstructed_Image.png) ![conv2_1](output/content_img_wally_conv_2/Reconstructed_Image.png) ![conv2_2](output/content_img_wally_conv_3/Reconstructed_Image.png) ![conv3_1](output/content_img_wally_conv_4/Reconstructed_Image.png) ![conv3_2](output/content_img_wally_conv_5/Reconstructed_Image.png) ![conv3_3](output/content_img_wally_conv_6/Reconstructed_Image.png) ![conv3_4](output/content_img_wally_conv_7/Reconstructed_Image.png) ![conv4_1](output/content_img_wally_conv_8/Reconstructed_Image.png) ![conv4_2](output/content_img_wally_conv_9/Reconstructed_Image_0.png) ![conv4_3](output/content_img_wally_conv_10/Reconstructed_Image.png) ![conv4_4](output/content_img_wally_conv_11/Reconstructed_Image.png) The layers in conv5 block do not help much. Looking at the above results, I decided to use conv4_2 layer to extract the content features. ![Original image](images/content/wally.jpg) ![Optimized noise #1](output/content_img_wally_conv_9/Reconstructed_Image_0.png) ![Optimized noise #2](output/content_img_wally_conv_9/Reconstructed_Image_1.png) Texture synthesis =========================================================================== I optimize a random noise image to match the style features of the style image. The style features are extracted from the VGG19 model at multiple convolutional layers. The style loss is computed as the mean squared error between the Gram matrices of the style features of the style image and the style features of the new image. The optimization objective is to minimize the style loss. Vary style loss layer --------------------------------------------------------------------------- I experimented with different layers to extract the style features. ![conv1_1, conv1_2, conv2_1, conv2_2, conv3_1](output/style_img_starry_night_conv_0conv_1conv_2conv_3conv_4/Synthesized_Texture.png) ![all conv layers except conv4_2](output/style_img_starry_night_conv_0conv_1conv_2conv_3conv_conv_4conv_5conv_6conv_7conv_8conv_10conv_11conv_12conv_13conv_14/Synthesized_Texture.png) ![conv1_1, conv2_1](output/style_img_starry_night_conv_0conv_2/Synthesized_Texture.png) ![conv1_1, conv2_1, conv3_1, conv3_4, conv4_3](output/style_img_starry_night_conv_0conv_2conv_4conv_7conv_10/Synthesized_Texture.png) ![conv3_2, conv3_3, conv3_4, conv4_1, conv4_2](output/style_img_starry_night_conv_5conv_6conv_7conv_8conv_9/Synthesized_Texture.png) ![conv4_3, conv4_4, conv5_1, conv5_2, conv5_3](output/style_img_starry_night_conv_10conv_11conv_12conv_13conv_14/Synthesized_Texture.png) ![conv2_1, conv3_1, conv3_4](output/style_img_starry_night_conv_2conv_4conv_7/Synthesized_Texture_0.png) Looking at the above results, I decided to use conv2_1, conv3_1, and conv3_4 layers to extract the style features. ![Style Image](output/style_img_starry_night_conv_2conv_4conv_7/Style_Image.png) ![Optimized noise #1](output/style_img_starry_night_conv_2conv_4conv_7/Synthesized_Texture_0.png) ![Optimized noise #2](output/style_img_starry_night_conv_2conv_4conv_7/Synthesized_Texture_1.png) Style Transfer =========================================================================== Noise Initialization --------------------------------------------------------------------------- Using content layers and style layers as discussed in aboce sections, I set the content loss weight to 1 and tune the style loss weight. ![Style weight = 1](output/style_transfer_starry_night_to_phipps_sw_1/Style_Transfer.png) ![Style weight = 100](output/style_transfer_starry_night_to_phipps_sw_100/Style_Transfer.png) ![Style weight = 1000](output/style_transfer_starry_night_to_phipps_sw_1000/Style_Transfer.png) ![Style weight = 10000](output/style_transfer_starry_night_to_phipps_sw_10000/Style_Transfer.png) ![Style weight = 30000](output/style_transfer_starry_night_to_phipps_sw_30000/Style_Transfer.png) ![Style weight = 100000](output/style_transfer_starry_night_to_phipps_sw_100000/Style_Transfer.png) Some more results with tuned hyperparameters. | |![](images/style/picasso.jpg) | ![](images/style/frida_kahlo.jpeg) | |----------------------------------|-----------------------------------------------------------------|----------------------------------------------------------------------| |![](images/content/tubingen.jpeg) |![](output/style_transfer_picasso_to_tubingen/Style_Transfer.png)| ![](output/style_transfer_frida_kahlo_to_tubingen/Style_Transfer.png)| |![](images/content/dancing.jpg) |![](output/style_transfer_picasso_to_dancing/Style_Transfer.png) |![](output/style_transfer_frida_kahlo_to_dancing/Style_Transfer.png) | Content Initialization --------------------------------------------------------------------------- I use the content image as the initialization for the new image. | |![](images/style/picasso.jpg) | ![](images/style/frida_kahlo.jpeg) | |----------------------------------|------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------| |![](images/content/tubingen.jpeg) |![](output/style_transfer_picasso_to_tubingen/Style_Transfer_from_Content_Image.png)| ![](output/style_transfer_frida_kahlo_to_tubingen/Style_Transfer_from_Content_Image.png)| |![](images/content/dancing.jpg) |![](output/style_transfer_picasso_to_dancing/Style_Transfer_from_Content_Image.png) |![](output/style_transfer_frida_kahlo_to_dancing/Style_Transfer_from_Content_Image.png) | Results on my own images --------------------------------------------------------------------------- | |![](images/style/starry_night.jpeg) | ![](images/style/the_scream.jpeg) | |---------------------------------------------------------|-------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------| |![Mellon Park, Pittsburgh](images/content/rainbow.jpeg) |![](output/style_transfer_starry_night_to_rainbow/Style_Transfer_from_Content_Image.png) | ![](output/style_transfer_the_scream_to_rainbow/Style_Transfer_from_Content_Image.png) | |![Frick Park, Pittsburgh](images/content/frickpark.jpeg) |![](output/style_transfer_starry_night_to_frickpark/Style_Transfer_from_Content_Image.png) |![](output/style_transfer_the_scream_to_frickpark/Style_Transfer_from_Content_Image.png) | Bells and Whistles =========================================================================== Return of cats --------------------------------------------------------------------------- ![Stylized Russian Blue](output/style_transfer_the_scream_to_cat_a/Style_Transfer_from_Content_Image.png) ![Stylized grumpy cat](output/style_transfer_the_scream_to_cat_b/Style_Transfer_from_Content_Image.png) Stylized videos --------------------------------------------------------------------------- I tried stylizing a video from the 3DOVS dataset. ![](output/style_transfer_starry_night_to_bed/video.mp4) If you are not able to view the video, you can view it [here](https://drive.google.com/file/d/1C0DOYx4eXbd5IxrIXOhReCPrTkS2v391/view?usp=sharing) Feedforward neural style transfer --------------------------------------------------------------------------- I implement a feedforward neural style transfer network using the architecture of CycleGAN used in last assignment. The network is trained on the cat datatset. The results are not as good as the optimization-based approach. | | | | |---------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------| | || | | || |