Paint Like a Professional With Neural Style Transfer
Amidst all of the amazing machine learning tools, neural style transfer is one of the most interesting ones. This is because it allows us to process images in a style of another image. For example, you can create photographs in a painting style of Van Gogh.
Coding Neural Style Transfer
In the following tutorial, I’m going to show you one of the simplest ways to implement this method.
First of all, we need to import all the necessary libraries. Furthermore, we’ll be working with Tensorflow machine learning library.
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import PIL
In order to make this work, we also need only 2 images. Content image will be the photo of a turtle, and style image will be a japanese painting.
Therefore, we need to define paths to them.
content_path = 'Neural Style Transfer/Green_Sea_Turtle_grazing_seagrass.jpg'
style_path = 'Neural Style Transfer/The_Great_Wave_off_Kanagawa.jpg'
In the following step, we’re going to define a function that will load and preprocess these images, so the machine learning model accepts them as input.
def load_img(path_to_img):
max_dim = 512
img = tf.io.read_file(path_to_img)
img = tf.image.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
shape = tf.cast(tf.shape(img)[:-1], tf.float32)
long_dim = max(shape)
scale = max_dim / long_dim
new_shape = tf.cast(shape * scale, tf.int32)
img = tf.image.resize(img, new_shape)
img = img[tf.newaxis, :]
return img
Okay, next thing we need to do is define a function that will transform the processed image data back into an image and save it.
def tensor_to_image(tensor):
tensor = tensor * 255
tensor = np.array(tensor, dtype=np.uint8)
if np.ndim(tensor) > 3:
assert tensor.shape[0] == 1
tensor = tensor[0]
image = PIL.Image.fromarray(tensor)
image.save("Neural Style Transfer/result image.jpg")
return image
And finally, we need to import a model for arbitrary image stylization from Tensorflow Hub. In the next step, we’re going to put everything together, where we load in the model, load in the images and process them to get the final result.
content_image = load_img(content_path)
style_image = load_img(style_path)
hub_model = hub.load('https://tfhub.dev/google/magenta/arbitrary-image-stylization-v1-256/2')
stylized_image = hub_model(tf.constant(content_image), tf.constant(style_image))[0]
tensor_to_image(stylized_image)
From this, our final result should look something like the following image.
Conclusion
In conclusion, we made a short algorithm that generates one image in a style of another. Furthermore, I hope this tutorial helped you gain a better understanding of neural style transfer technique. Even more, I hope you’ll have some fun experimenting with it and get astonishing results.