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.

In this article we will show how to create such artwork using open source implementation of A Neural Algorithm of Artistic Style and Octa.Space.

First, you need to rent a machine with Nvidia GPU using Console.

Go to Services->CPU/GPU rental and choose the one of available machines which has at least one Nvidia GPU. In the Image section select TensorFlow 2.9.1 GPU and set the disk size depends of amount of images you want to process, in our case for the testing the 2 Gb of disk would be enough.

png

Press the button Confirm and rent and wait several minutes until machine will be ready.

In the menu General->Service sessions press the UUID column to show information about how to connect to the created machine using SSH client:

png

Connect to the machine using SSH client:

Tip

On the Windows you can use putty or any other SSH client available for Windows OS

png

Then we need to prepare environment - install some software, download pre-trained model and upload source images which we want to use:

The steps below you need to do once

  • Install GIT software:
apt install -y git
  • Clone neural-style software:
git clone --depth 1 https://github.com/anishathalye/neural-style.git && cd neural-style
  • Install dependencies
pip install -r requirements.txt
  • Download pre-trained model, it has size around 510Mb:
curl -o imagenet-vgg-verydeep-19.mat https://www.vlfeat.org/matconvnet/models/imagenet-vgg-verydeep-19.mat

Ok, all preparing are done and now we able to do some magic and get the cool artwork images.

In this example we will use the following images:

  • Source image:

jpg

  • Style image:

jpg

Download them from the internet to the machine:

Tip

You can also upload any images from your local PC using scp command on Linux or winscp or similar software on Windows

curl -o src.png https://raw.githubusercontent.com/anishathalye/neural-style/master/examples/2-content.jpg
curl -o style.png https://raw.githubusercontent.com/anishathalye/neural-style/master/examples/2-style1.jpg

Let’s use neural style software to generate the final image, run the following command:

python neural_style.py --content src.png --styles style.png --output output.png

Wait until it’s finished, the resulted image will be saved as output.png:

png

That’s all, try to experiment with other source and style images.

Useful links: