Image analogies

Half a year ago I read a tweet mentioning this interesting Image-analogies repository.

It’s an implementation of Image Analogies and Combining Markov Random Fields and Convolutional Neural Networks for Image Synthesis papers and it allows to achieve some interesting visual effects so I decided to check it out and here are some of the results.

hope-res

vang1-res

If you want to play with the Image analogies you need to use ether Tensorflow or Theano (I tried both but used Theano at the end) installed plus Kears which can work with both. I decided to use CUDA based calculations since I’ve got Nvidia GeForce GTX 960. Setting up all these things together on Ubuntu 14.05 is not a trivial process, at least it wasn’t for me, so I decided to summarize it in the following instruction.

All the installation steps are valid for a fresh Ubuntu 14.04 TLS installation you can download from here.

Installing Cuda Toolkit 7.5 and cuDNN 5.0

$ cd ~/Downloads
$ wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_7.5-18_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb 
$ sudo apt-get update

If you try to execute the next command:

$ sudo apt-get install cuda

You’ll probably get the following error:

The following packages have unmet dependencies.
cuda : Depends: cuda-7-5 (= 7.5-18) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

If this happens try to add additional repository, install some packages including cuda explicitly.

$ sudo add-apt-repository ppa:xorg-edgers/ppa 
$ sudo apt-get update
$ sudo apt-get install cuda libcheese-gtk23 libcheese7 libclutter-1.0-0 libclutter-gtk-1.0-0 libcogl15

You also need to set the LD_LIBRARY_PATH and in order to remember this change you probably what to add the line into your ~/.bashrc profile (not shown here)

$ export LD_LIBRARY_PATH=/usr/local/cuda-7.5/targets/x86_64-linux/lib/:$LD_LIBRARY_PATH

Note: This is little bit unfortunate that you have to use LD_LIBRARY_PATH but without it you’ll definitely get the following error if you try to compile something with cuda.

Failed to compile cuda_ndarray.cu: libcublas.so.7.5: cannot open shared object file: No such file or directory

You need to reboot your system in order to use Nvidia driver which gets installed with your Cuda package.

$ sudo reboot

After reboot check Nvidia driver status and you should get similar output.

$ nvidia-smi

+------------------------------------------------------+                       
| NVIDIA-SMI 352.99     Driver Version: 352.99         |                       
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 960     Off  | 0000:01:00.0      On |                  N/A |
|  0%   33C    P8     7W / 160W |    373MiB /  4095MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID  Type  Process name                               Usage      |
|=============================================================================|
|    0      1180    G   /usr/bin/X                                     185MiB |
|    0      2105    G   compiz                                         172MiB |
|    0      2659    G   /usr/lib/firefox/firefox                         1MiB |
+-----------------------------------------------------------------------------+

You might also want to check Nvidia kernel and client versions (they should match).

$ sudo dpkg --list | grep nvidia

Register here, download cuDNN (I used 5.0 for CUDA 7.5 because at this moment Theano doesn’t officially support 5.1) and install it using the following steps:

$ cd ~/Downloads
$ tar xvzf cudnn-7.5-linux-x64-v5.0-ga.tgz
$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

Installing Tensorflow

You can skip this part if you want to use Theano but if you want to proceed with Tensorflow you need to install JDK 8.

$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer

For compiling Tesorflow from the source you need to install Bazel.

$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add -
$ sudo apt-get update && sudo apt-get install bazel
$ sudo apt-get upgrade bazel

Install some additional dependencies.

$ sudo apt-get install python-numpy swig python-dev python-wheel git

Clone Tensorflow repository from github.

$ cd ~
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow

Configure the build as recommended here and build Tensorflow.

$ ./configure
$ ./bazel build -c opt --config=cuda

You can check if the build was successful by running some example.

$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu

Build pip package and install it

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
$ sudo pip install /tmp/tensorflow_pkg/tensorflow-0.10.0rc0-cp27-none-linux_x86_64.whl 

Check if Tensorflow pip is discoverable

$ cd ~
$ python -c "import tensorflow;print(tensorflow.__version__)"

Installing Theano

$ sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev git
$ export THEANO_FLAGS='cuda.root=/usr/local/cuda/,device=gpu,floatX=float32'

THEANO_FLAGS won’t be persisted so you might need to modify .theanorc as recommended here.

Installing Keras

$ sudo apt-get install libblas-dev liblapack-dev libyaml-cpp-dev gfortran
$ sudo pip install keras 

Now you have to configure Keras to use ether Theano

$ echo '{"epsilon": 1e-07, "floatx": "float32", "backend": "theano"}' >> ~/.keras/keras.json

or Tensorflow

$ echo '{"epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow"}' >> ~/.keras/keras.json

Installing Image analogies

$ sudo apt-get install libhdf5-dev
$ sudo pip install neural-image-analogies

Clone image analogies repository.

$ cd ~
$ git clone https://github.com/awentzonline/image-analogies.git

Upload VGG16 weights.

$ cd image-analogies/examples
$ wget https://github.com/awentzonline/image-analogies/releases/download/v0.0.5/vgg16_weights.h5

Try to run an example.

$ ./sugar-skull.sh ./images/sugarskull-B.jpg example

In order to check if the script uses GPU you can run the following command in another console window and see your NVidia graphics card stats.

$ watch -n 0.5 nvidia-smi

You may also like...