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
- Creating excellent artistic looking images using different style images.
- Can be used for Image Data augmentation purposes.
- Websites like DeepArt and Prisma use Neural Style Transfer to create artistic images.
In [1]:
!wget https://cainvas-static.s3.amazonaws.com/media/user_data/Yuvnish17/neural_style_transfer_data.zip
!unzip -qo neural_style_transfer_data.zip
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')
print(img.shape)
(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
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.imshow(style_img)
plt.axis('off')
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.imshow(content_img)
plt.axis('off')
plt.title('Content Image')
Out[4]: