Image Analogies on Ubuntu 14.04 with CUDA and Keras

This is a cleaned-up archival note from 2016. The commands below belong to the CUDA 7.5, cuDNN 5.0, Theano and early TensorFlow era, so treat them as historical context rather than current setup advice.

In 2016, I came across a tweet that mentioned the image-analogies repository.

It was an implementation of the Image Analogies paper and the Combining Markov Random Fields and Convolutional Neural Networks for Image Synthesis paper. It could produce some interesting visual effects, so I decided to try it and record the setup.

Three-panel image analogy example: an original portrait, an Obama Hope poster reference and a stylized portrait result.
Three-panel image analogy example: an original portrait, an Obama Hope poster reference and a stylized portrait result.
Three-panel image analogy example: an original portrait, a Van Gogh-style portrait reference and a stylized portrait result.
Three-panel image analogy example: an original portrait, a Van Gogh-style portrait reference and a stylized portrait result.

If you wanted to experiment with image analogies at the time, you needed either TensorFlow or Theano, plus Keras, which could work with both backends. I tried both TensorFlow and Theano, but used Theano in the end. I also decided to use CUDA because I had an NVIDIA GeForce GTX 960.

Setting all of this up on Ubuntu 14.04 was not a trivial process, at least not for me, so I wrote down the steps.

All installation steps below were tested on a fresh Ubuntu 14.04 LTS installation, available from the Ubuntu 14.04 release archive.

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 will 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, add the additional repository and install the needed packages 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 LD_LIBRARY_PATH. To persist the change, add the same line to your ~/.bashrc profile.

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

This is a little unfortunate, but without LD_LIBRARY_PATH you will get the following error when 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

Reboot the system so it uses the NVIDIA driver installed with the CUDA package.

$ sudo reboot

After rebooting, check the NVIDIA driver status. 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 the NVIDIA kernel and client versions. They should match.

$ sudo dpkg --list | grep nvidia

Register on the NVIDIA cuDNN page, download cuDNN and install it using the following steps. I used cuDNN 5.0 for CUDA 7.5 because, at the time, Theano did not officially support cuDNN 5.1.

$ 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 section if you want to use Theano. To proceed with TensorFlow, first install JDK 8.

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

To compile TensorFlow from source, 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 the additional dependencies.

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

Clone the TensorFlow repository from GitHub.

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

Configure the build as recommended in the old TensorFlow 0.10 setup guide and build TensorFlow.

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

Check whether the build was successful by running an example.

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

Build a 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 that the TensorFlow package 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 will not be persisted, so you may need to modify .theanorc as recommended in the Theano configuration documentation.

Installing Keras

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

Now configure Keras to use either 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 the image-analogies repository.

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

Download the 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

To check whether the script uses the GPU, run the following command in another terminal window and watch the NVIDIA graphics card stats.

$ watch -n 0.5 nvidia-smi
Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *