jeudi 14 juillet 2016

Practical guide to neural style with docker

Google has recently made some noises with it's amazing neural dream images, images generated by neural networks. Another popular use of neural networks for image processing is called "neural style", the use of neural algorithms to apply the style of an image to another.

I'll explain here how to quickly use neural style algorithms to transform your photos. I'll assume you aren't interested into the theory behind the method and just want to get started quickly. To avoid a cumbersome installation and possible incompatibilities of the technologies used with your machine, I'll make you use a virtual machine called Docker and download an image with all the needed libraries already installed.

First, you'll need to have Docker installed on your computer. I'll assume that you will manage to do it by youself.

With docker installed, pull the image containing the code:
docker pull heschoon/neural-style

It will take some time, as you are downloading a 1Gb virtual machine. While it downloads, collect at least two images in one folder on your computer. We will together learn how to apply the style of the first image ( let's call it style.png ) to the second image ( content.png ).

Tip: choose as style.jpg an image with a memorable style!

A popular style image is the starry night painting of Van Gogh:


In this tutorial, I'll use the picture of an handsome programmer as content.jpg.


Put both image in the same folder on your computer. Now you can run the virtual machine:
docker run -it -v <path/to/your/images/folder>:/root/input-images/ \
heschoon/neural-style /bin/bash

In case you wonder what' does the command: "docker run" will run the virtual machine, "-it" attaches an interactive terminal on your virtual machine, "-v <path/to/your/images/folder>input-images" mounts the folder containing the images on the virtual machine, "heschoon/neural-style" is the name of the virtual machine to run. This docker image is based on the jcjohnson/neural-style implementation of the neural-style algorihtm.

You can now move to the folder that contains the code:
cd ~/neural-style/

Ensure that the image folder has been successfully mounted on your docker:
ls ../input-images

If the command list the names of the images you have in your folder; probably style.jpg and content.jpg, the folder has been successfully mounted.

You can generate a new image:
th neural_style.lua -gpu -1 -style_image ../input-images/style.jpg \
-content_image ../input-images/content.jpg -image_size 256

The algorithm is now running. It my laptop it took 2 hours, so be patient.You might want to give more RAM to Docker if the algorithms crashes.

When the wait is over, copy the generated images into the shared folder, to make them persistent and accessible from your computer:
cp out* ../input-images

You can now exit the virtual machine by type the exit command, and look into your image folder. The generated output images should be available.



Now, you can try with other style and content images, and play with the other flags and commands described on the code repository.


Aucun commentaire:

Enregistrer un commentaire