Neural Style Transfer in 3 Simple Steps

Anyim Amarachi
2 min readJan 7, 2022

Neural style transfer is a technique that makes use of neural networks to blend two images, a content image and a style image (usually an artwork), in such a way that the generated image still looks like the initial content, but with the expression of the style image. You could think of it as “unleashing your inner Picasso”, just using neural nets and some images.

In this quick exercise, I would be using this pre-trained model found on Tensorflow Hub to demonstrate image stylization in three simple steps. I recommend checking out other cool trained models on the Tensorflow community later, but for now, let’s get right into it!

The first step is to download the pre-trained model.

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

The next step is to load the images using someTensorflow preprocessing

def load_image(img_path):
img = tf.io.read_file(img_path)
img = tf.image.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
img = img[tf.newaxis, :]
return img
content_img = load_image('content.jpg')
style_img = load_image('style.PNG')
Style and Content images respectively

Finally, apply style transfer and visualize the results.

outputs = model(tf.constant(content_img), tf.constant(style_img))
stylized_img = outputs[0]
Stylized image

The complete code used can be found on Github. Feel free to try it out on other images.

--

--