Neural Style Tranfer

Neural Style Transfer is stylizing an image in style of another image. Here we try to implement two methods of Neural Style Transfer namely - Optimization-based Neural Style Transfer and Fast Neural Style Transfer.

Aplications of Neural Style Transfer

  1. Creating excellent artistic looking images using different style images.
  2. Can be used for Image Data augmentation purposes.
  3. Websites like DeepArt and Prisma use Neural Style Transfer to create artistic images.
In [1]:
!unzip -qo
--2021-07-20 05:38:09--
Resolving (
Connecting to (||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 64932428 (62M) [application/x-zip-compressed]
Saving to: ‘’

neural_style_transf 100%[===================>]  61.92M  74.4MB/s    in 0.8s    

2021-07-20 05:38:10 (74.4 MB/s) - ‘’ saved [64932428/64932428]

Importing Libraries

In [2]:
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.applications import vgg16
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.regularizers import Regularizer
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import concatenate
from tensorflow.keras.models import Model,Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from neural_style_transfer_data.layers import InputNormalize,VGGNormalize,ReflectionPadding2D,Denormalize,conv_bn_relu,res_conv,dconv_bn_nolinear
import time

Optimization-based Neural Style Transfer

Loading the Input Image and the Style Image

In [3]:
img = cv2.imread('neural_style_transfer_data/content.png')
(height, width, channels) = img.shape
img_rows = 400
img_cols = int(width * img_rows / height)
result_prefix = "neural_style_transfer_generated"

# Weights of the different loss components
total_variation_weight = 1e-6
style_weight = 5e-6
content_weight = 1e-8
(556, 572, 3)

Visualizing the The Style Image and the Input Image

In [4]:
figure = plt.figure(figsize=(10, 10))
style_img = cv2.imread('neural_style_transfer_data/style_image3.jpg')
style_img = cv2.cvtColor(style_img, cv2.COLOR_BGR2RGB)
plt.title('Style Image')

figure2 = plt.figure(figsize=(10, 10))
content_img = cv2.imread('neural_style_transfer_data/content.png')
content_img = cv2.cvtColor(content_img, cv2.COLOR_BGR2RGB)
plt.title('Content Image')
Text(0.5, 1.0, 'Content Image')