Would you like to create unique art like the painting above? You can do that very easily and the results are very interesting. You can create masterpieces that can be compared to the work of some of the greatest painters ever lived such as Pablo Picasso, Michelangelo, Vincent van Gogh and others. You can do that using a framework that uses Google’s deep learning framework called Tensorflow. In this tutorial, we are going to use Tensorflow in conjunction with a small library on GitHub that utilizes it and makes it “learn” the patterns (style) from one image and apply it on another (content). I have to note that this tutorial is only for MAC users because the library that we are going to use supports only Mac OS. The above image was generated by the framework using this style image:
This is the original image:
As you can see that similarities are astonishing, Tensorflow learned the patterns from the style image and applied them over the content picture (me).
Ok enough talking let’s jump straight into the tutorial:
- Install Python3 from this link. Make sure that you download version equal or bigger than 3.4
- After that install virtualenv by initiating this command: pip3 install virtualenv
- When that is done we are ready to install Tenserflow. First, create a new directory for the project by typing
– mkdir NeuralStyle and then
– cd NeuralStyle
- After that create a virtual environment by
– virtualenv neuralStyleProject
- Next, we need to activate the virtual environment in order to use it so in the terminal entire the command
– source neuralStyleProject/bin/activate. After this command, your terminal should look similar to that “(NeuralStyleProjec) Georgi:NeuralStyle ”
- Finally, we are ready to install Tensorflow
– export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py3-none-any.whl
– pip3 install –upgrade $TF_BINARY_URL
That will install the framework, it should take a couple of seconds (depending on your internet connection and computer)
- (Optional) In order to test if the installation was successful do the following
>>> import tensorflow as tf
>>> hello = tf.constant(‘Hello, TensorFlow!’)
>>> sess = tf.Session()
- clone the Neuro style repository: git clone https://github.com/anishathalye/neural-style.git
- Enter the command “open .” it will open the home directory that is relative to the project
- Move the content “neural-style” folder and paste it into the folder “neuralStyleProject”
- Afer that you can delete the empty “neural-style” folder. Now your project directory should look like that:
- We need to download an already traded model in order for the need learning to work. Go to this link, and download this file “Pre-trained VGG network” at the bottom of the page. When downloaded cut and paste the file in the “neuralStyleProject” folder with the rest of the files.
- Find two good images one that is the one that you want to apply the style to and other that is the style. I name mines such as “georgi_content.jps” and “1-style.jpg”
- Now in your terminal do cd neuralStyleProject to enter to the project directory.
- We are almost there, we need to install just a couple of needed libraries in order to run the neural-style program. install “numpy” by entering the command in the terminal enter:
“pip3 install numpy” press enter
“pip3 install scipy” press enter
“pip3 install pillow” press enter
- Enter the following command to initiate the script by entering the following command:
“python3 neural_style.py –content georgi_content.jpg –styles 1-style.jpg –output result.jpg”
- The program will start and your terminal will look like that:
- If you want to achieve better results you can open the “neural_style.py” file with your program of choice (I used xCode) and you can change this variable:
1000 iterations tend to achieve good results, but you can experiment with increasing this value. Bear in mind that this is a very long process and it could take (depending on the resolution of the content and the number of iterations) about 9 hours or more. I would suggest resizing your content and style images to about 300 pixels width – keep the resolution low and after you are certain that the style will produce the desirable effect you can run the algorithm with bigger resolution images for content and style. Forgot to mention that your output image will keep the same resolution as your content image.
If you do not have a Mac OS machine but you want to try the framework there is a website called deepart.io. It works on the same principle, you have to provide a content image (the one that you want to apply a style to) and a style image (the one that will be used to generate the style).