Inceptionism setup

Published on Author malmLeave a comment


This page details all the steps taken to setup an environment capable of generating ‘inceptionist’ images using the bat-country model as described in outline by its author Adrian Rosebrock here. The host OS for this exercise was a vanilla Linux Mint 17.2 64-bit ISO installation in VirtualBox.

Installing up to date baseline + tools + virtualenv

Assuming a vanilla Mint 17.2 install, it is necessary to prepare the environment for building the various libraries and wrappers we will need.  First of all we update the platform to the latest packages and install a few key ones we’ll need including git and pip.  scite is being used as a basic text editor – you can opt to use another one as preferred.  python-dev and build-essential are needed to build from source:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install mercurial
$ sudo apt-get install git
$ sudo apt-get install scite
$ sudo apt-get install python-pip
$ sudo pip install pip --upgrade
$ sudo apt-get install python-dev 
$ sudo apt-get install build-essential

We will be creating our ‘dreaming’ setup in a dedicated, isolated Python environment.  virtualenv is a tool to create these.  The virtualenv created below is called ‘dreaming’:

$ sudo apt-get install virtualenv
$ sudo apt-get install virtualenvwrapper
# add the following to .bashrc
if [ `id -u` != '0' ]; then
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/
$ source .bashrc

Removing urllib3 warning

On the setup used, urllib3 popped up an annoying InsecureRequestWarning as described here.  The following can be done to remove it.   If it doesn’t bother you, please skip this section:

# Get rid of annoying urllib3 warning
$ sudo apt-get install libffi-dev
$ sudo apt-get install libssl-dev
$ workon dreaming
(dreaming)$ pip install pyopenssl
(dreaming)$ pip install ndg-httpsclient
(dreaming)$ pip install pyasn1
# tell urllib3 to use pyopenssl backend
(dreaming)$ python
> import urllib3.contrib.pyopenssl
> urllib3.contrib.pyopenssl.inject_into_urllib3()
> import requests
> requests.packages.urllib3.disable_warnings()
> exit()
(dreaming)$ deactivate

Installing numpy

numpy is the standard Python linear algebra and array-handling module.  This will pip install into the ‘dreaming’ virtualenv:

$ mkvirtualenv dreaming
$ workon dreaming
(dreaming)$ pip install numpy # v1.9.2
(dreaming)$ deactivate

Installing matplotlib

matplotlib is the Python 2D plotting library.  It’s not actually strictly needed for inceptionism but you may want it down the line.  This pip installs it into our ‘dreaming’ virtualenv:

$ sudo apt-get install libpng-dev
$ sudo apt-get install libfreetype6-dev
$ workon dreaming
(dreaming)$ pip install matplotlib # v1.4.3
(dreaming)$ deactivate

Installing scipy, scikit-image and pandas

scipy contains a wide range of numerical functions and transforms. It is the “fundamental library for scientific computing” at the heart of the stack:

You’ll also need scikit-image for image processing and may want to use pandas down the line for data analysis.  Again, these all need to be pip installed into ‘dreaming’ virtualenv thus:

$ sudo apt-get install gfortran
$ sudo apt-get install libopenblas-dev
$ sudo apt-get install liblapack-dev
$ workon dreaming
(dreaming)$ pip install scipy    # v0.15.1, takes ages
(dreaming)$ pip install scikit-image # v0.11.3 skimage
(dreaming)$ pip install pandas  
(dreaming)$ deactivate

Installing Caffe

Caffe is a modular and efficient deep learning framework originally developed at Berkeley Vision and Learning Center (BVLC).  It is widely used for image classification.   It has a number of important dependencies:

  • OpenBLAS:  backend for caffe matrix and vector computations
  • Boost: C++ libraries
  • OpenCV: computer vision library
  • CUDA: GPU compute framework.  Only needed if you want to take advantage of corresponding performance speedup.  For our purposes we are using CPU compute only so don’t need it.
$ sudo apt-get install cmake
$ sudo apt-get install libprotobuf-dev
$ sudo apt-get install libleveldb-dev
$ sudo apt-get install libsnappy-dev
$ sudo apt-get install libopencv-dev
$ sudo apt-get install libhdf5-serial-dev
$ sudo apt-get install --no-install-recommends libboost-all-dev
Google Logging support (glog)
$ wget
$ tar zxvf glog-0.3.3.tar.gz
$ cd glog-0.3.3
$ ./configure
$ make
$ sudo make install
$ cd ..
gflags support
$ wget
$ unzip
$ cd gflags-master
$ mkdir build && cd build
$ export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1
$ make
$ sudo make install
$ cd ..
lmdb support
$ git clone
$ cd lmdb
$ make
$ sudo make install
Building caffe and pycaffe wrapper from source

Note the important modifications to caffe’s Makefile.config that are needed to enable CPU_ONLY mode and BLAS set to use openBLAS. You also probably want to move the two exports into .bashrc so they are enacted each time you log into your VM:

$ git clone
$ cd caffe
$ cp Makefile.config.example Makefile.config
# Edit Makefile.config and set:
# CPU_ONLY := 1
# BLAS := open
$ make clean
$ make all -j8    # parallel make
$ make pycaffe
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
$ export PYTHONPATH=~/path/to/caffe/python:$PYTHONPATH
$ make runtest
$ workon dreaming
(dreaming)$ python
> import caffe
(dreaming)$ deactivate

Installing bat-country

bat-country is Adrian Rosebrock’s module wrapper for the Google inceptionism notebook that turns it into a module.  You need to configure your caffe environment first before pip installing it thus:

# 1. Set up CAFFE_ROOT variable - ideally in .bashrc
$ export CAFFE_ROOT=~/path/to/caffe
# 2. Download the pretrained GoogLeNet model:
$ ./scripts/ models/bvlc_googlenet/
$ workon dreaming
(dreaming)$ pip install bat-country # v0.1.1
(dreaming)$ pip install pyaml
(dreaming)$ python
(dreaming)$ deactivate

Using bat-country

Use bat-country in a Python script as follows.  Obviously this assumes you have an input image called cat.jpg like the one below:

from batcountry import BatCountry
import numpy as np
from PIL import Image

bc = BatCountry("caffe/models/bvlc_googlenet")
image = bc.dream(np.float32(
result = Image.fromarray(np.uint8(image))"output.jpg")
print("Finished processing %s" % inputimage)

Et voilà:



Leave a Reply