What is Neural Style Transfer?

Neural style transfer is an optimization technique used to take two images—a content image and a style reference image (such as an artwork by a famous painter)—and blend them together so the output image looks like the content image, but “painted” in the style of the style reference image.

This is implemented by optimizing the output image to match the content statistics of the content image and the style statistics of the style reference image. These statistics are extracted from the images using a convolutional network. In order to get both the content and style representations of our image, we will look at some intermediate layers within our model. Intermediate layers represent feature maps that become increasingly higher ordered as you go deeper. In this case, we are using the network architecture VGG19, a pretrained image classification network. These intermediate layers are necessary to define the representation of content and style from our images. For an input image, we will try to match the corresponding style and content target representations at these intermediate layers.

nst

In neural style transfer we are minimizing the content and style loss functions. The content loss function is a measure of how much the content of the input image matches the content of the content target image. The style loss function is a measure of how much the style of the input image matches the style of the style target image.

After the optimization, the output image will look like the input image, but with the style of the style reference image.

Code Implementation of Neural Style Transfer

Importing Necessary Library

import tensorflow_hub as hub
import tensorflow as tf
from matplotlib import pyplot as plt
import numpy as np
import cv2

Preprocessing And Loading Our Image.

def load_image(img_path):
    img = tf.io.read_file(img_path)
    # Making our image 3 channels
    img = tf.image.decode_image(img, channels=3)
    # Changing the image datatype to float32
    img = tf.image.convert_image_dtype(img, tf.float32)
    img = img[tf.newaxis, :]
    return img
content_image = load_image('dip2.jpg')
style_image = load_image('s7.jpg')
content_image.shape
TensorShape([1, 315, 315, 3])

Downloading Our Pretrained Neural Style Transfer Model from tensorflow hub.

model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')

Visualizing our Style_images and content images.

plt.imshow(np.squeeze(style_image))
plt.show()

Here np.squeeze is extracting images from those TensorShape([1, 315, 315, 3])

plt.imshow(np.squeeze(content_image))
plt.show()

This code will perform Neural Style Transfer.

stylized_image = model(tf.constant(content_image), tf.constant(style_image))[0]

This code will save our style transfered Images to the current working directory.

cv2.imwrite('generated_img14.jpg', cv2.cvtColor(np.squeeze(stylized_image)*255, cv2.COLOR_BGR2RGB))
True

- In this way we can transfer of styled images to content image.