in machine learning, programming, python, tutorial

Generating Deep Dreams

Share Button

The previous post in this series, Understanding Deep Dreams, explained what deep dreams are, and what they can be used for. In this second post you’ll learn how to create them, with a step by step guide.

Introduction

In August 2015, Google Research released the full source code they have been using to generate their pictures. My guide is strongly inspired by this one, which unfortunately didn’t work for many users. The following instructions will show how to virtualise Ubuntu 14.04.02 with VirtualBox, and how to run your very own deep dreams on it.

You can test this very technique using DeepDreamThis, a twitter bot that will create deep dreams out of the images you send to it. You can check a detailed guide on how to use it here. You can also check DeepForger, which uses neural networks to transfer artistic styles from a painting to a picture.

The Instructions

  • Virtualise Ubuntu 14.04.02
    • Install VirtualBox
    • Download the Ubuntu 14.04.02 iso
    • Create a new Virtual machine for “Linux / Ubuntu 64bit“.
    • You’ll need at least 10Gb of space and at least 2048Mb of RAM
    • In the Settings > Storage properties, add the downloaded iso of Ubuntu in the CD drive
    • Run the virtual machine
    • Install Ubuntu
  • Install Guest Additions
    • From the VirtualBox windows: Device > “Insert Guest Additions CD image
    • Install
    • Restart the machine
  • Install build essentials
    • sudo apt-get install build-essential
    • sudo apt-get install linux-headers-`uname -r`
  • Install caffe and deepdream dependencies
    • sudo apt-get install -y curl libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev protobuf-compiler gfortran libjpeg62 libfreeimage-dev libatlas-base-dev git python-dev python-pip libgoogle-glog-dev libbz2-dev libxml2-dev libxslt-dev libffi-dev libssl-dev libgflags-dev liblmdb-dev python-yaml imagemagick python-opencv
    • sudo easy_install pillow
  • Download caffe
    • cd ~
    • git clone https://github.com/BVLC/caffe.git
  • Install caffe python dependencies
    • cd caffe
    • cat python/requirements.txt | xargs -L 1 sudo pip install
    • sudo ln -s /usr/include/python2.7/ /usr/local/include/python2.7
    • sudo ln -s /usr/local/lib/python2.7/dist-packages/numpy/core/include/numpy/ /usr/local/include/python2.7/numpy
  • Configure caffe
    • cp Makefile.config.example Makefile.config
    • nano Makefile.config
    • Uncomment the line # CPU_ONLY := 1
    • Under PYTHON_INCLUDE, replace /usr/lib/python2.7/dist-packages/numpy/core/include with /usr/local/lib/python2.7/dist-packages/numpy/core/include
  • Compile caffe
    • make pycaffe
    • make all
    • make test
    • make runtest
    • Be sure the output of make runtest doesn’t include any error
    • make distribute
  • Make pycaffe available to python
    • sudo cp distribute/lib/libcaffe.so distribute/python/caffe
    • sudo cp -r distribute/python/caffe /usr/local/lib/python2.7/dist-packages/caffe
    • sudo nano /etc/ld.so.conf.d/caffe.conf
    • LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/caffe/distribute/python/caffe Write the full path to the caffe/distribute/lib/  folder; for instance: /home/alanzucconi/caffe/distribute/lib/ . Do not use ~.
    • sudo ldconfig
    • You should also put these lines in the .bashrc file, so that they are executed when your computer starts (remember to use the path to your folders):
      export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/caffe/distribute/python/caffe export PYTHONPATH=$PYTHONPATH:~/caffe/distribute/python/
  • Download the GoogLeNet model
    • cd ~/caffe/models/bvlc_googlenet
    • curl -O "http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel"
  • Download deepdream
    • cd ~
    • git clone https://github.com/google/deepdream.git
  • The code
    • In the deepdream folder create a file called dream.py
    • Fill it with the code below
    • cd ~/deepdream
    • sudo python dream.py

The code

Lines 98 and 101 uses the deepdream function to generate different dreams. The end parameter is used to determine which layer we want to analyse. You can check which layers can be analysed by printing net.blobs.keys().

deepdream

I’ve created a repository of all the layers you can query. Find an effect you like, and use the name of the file (without the extension) and the end parameter. Just remember to replace any __ (double underscore) with a / (Unix slash).

Conclusion

This post explained how to use the machine learning framework Caffe to generate deep dreams. Using a virtualised environment, however, might not be the best option. Neural networks require a lot of computational power, which is often unavailable in a virtual machine. For best performances, you should run deep dreams using a GPU.

This tutorial referred to the first, original tutorial released by Google Research. Other techniques have been evolved in the past months, including algorithms that works on videos.

Other resources

  • deepdream: the GIT project used by Google Research;
  • Caffe: the framework used to implement neural networks. Valid alternatives are Theano and Torch;
  • Running the deep dream: an alternative tutorial to run deep dreams;
  • neural-style: a Torch variation of deep dreams, used to transfer artistic styles;
  • neural-doodle: a tool that allows to draw in the style of a particular artist.
Support this blog! ♥

In the past two years I've been dedicating more and more of my time to the creation of quality tutorials, mainly about game development and machine learning. If you think these posts have either helped or inspired you, please consider supporting this blog.

PatreonBecome a Patron Oo4th_patreon_name
PaypalDonate on PayPal
Twitter_logoFollow on Twitter

Don't miss the next tutorial!

There's a new post every Wednesday: leave your email to be notified!


Write a Comment

Comment

  1. Hi Alan,
    thanks for this – I think there are a couple of errors:

    under Make pycaffe available to python
    sudo nano /etc/ ld.so.conf.d/caffe.conf
    — there is a space between /etc/ and ld.so….

    and in your code sample:
    line 17 i f showimages:

    I get errors when I try to import caffe in python. Trying to figure that out and will keep you posted.

    thanks!

    • Hi Jan! Believe it or not, those mistakes were intentional.
      At the time I posted the article there was a security problem with the server. Basically it wasn’t loading pages with certain pieces of code it believed to be “dangerous”. For some reason those two lines were considered suspicious. The issue is not solved (for now!) for I forgot to fix them! 😀

      Thank you! 😀

  2. When I switch to gpu mode, I get this error:
    F1129 00:06:07.387610 31472 math_functions.cu:79] Check failed: error == cudaSuccess (77 vs. 0) an illegal memory access was encountered
    *** Check failure stack trace: ***
    This happens in both linux and windows .
    The irony is it happens randomly, meaning for some it just happens at the very begining of the process, for some others it happens at the very end.
    Did you encounter this? or do you have any idea why this is happening and how I could fix it?
    Thanks in advance

    • Hey!

      If you’re using VirtualBox, you can’t access the GPU.
      Did you check if your GPU is actually supported by CUDA?

      You say this is happening randomly. When it works, do you actually get a substantial speed up?

Webmentions

  • Understanding Deep Dreams – Alan Zucconi November 29, 2016

    Hey!

    If you’re using VirtualBox, you can’t access the GPU.
    Did you check if your GPU is actually supported by CUDA?

    You say this is happening randomly. When it works, do you actually get a substantial speed up?