In this tutorial, I will detail the steps for installing Caffe on a non-GPU(CPU) machine with Ubuntu 14.04 OS. Once we are done with the installation, we’ll also test the GOOGLE INCEPTIONISM (
deepdream) code which uses Caffe. This is a follow up post to my last post – Generating images with Google’s “INCEPTIONISM” – deepdream where I had shared some awesome images generated using
deepdream code. So, let’s get our hands dirty straight away .
Firstly, let’s make a directory named
deep-learning in the
$HOME folder where we will download all the packages.
cd ~ mkdir deep-learning cd deep-learning
1. Installing the dependencies
To successfully compile Caffe we need to install a few packages first. Execute the below command in the terminal. (These commands will require root access)
NOTE -> Since we will not be using GPUs, we do not require CUDA installation.
# General Dependencies sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install --no-install-recommends libboost-all-dev # BLAS -- for better CPU performance sudo apt-get install libatlas-base-dev # Python -- It comes preinstalled on Ubuntu 14.04 # Required if you want to use Python wrappers for Caffe sudo apt-get install the python-dev # Remaining dependencies sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
2. Compile and Install Caffe
The Caffe repository is hosted on GitHub. To clone the repository enter the below command in the terminal.
cd ~/deep-learning git clone http://github.com/BVLC/caffe.git cd caffe
Makefile.config.example contains the template for configuration file. We’ll use it to write the
Makefile.config configuration file.
cp Makefile.config.example Makefile.config
Makefile.config file we need to set options if we want to use a CPU or a GPU. Open your favorite editor and uncomment the line
CPU_ONLY := 1(most probably, line number 8) as shown below.
8 CPU_ONLY := 1
After setting the configuration, the next step is to make Caffe or brew Caffe .
make all make test make runtest
If you get an OpenCV
undefined reference error in
make all as shown below follow the instructions below else move to the section 3.
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)' .build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)' .build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)' collect2: error: ld returned 1 exit status make: *** [.build_release/tools/caffe.bin] Error 1
(Check this issue) for more details. In order to fix the errror, append
opencv_imgcodecs on line 176(most probably) as shown below.
174 LIBRARIES += glog gflags protobuf leveldb snappy \ 175 mdb boost_system hdf5_hl hdf5 m \ 176 opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
Now clear of the contents of
build. This is important else the error will persist.
rm -rf ./build/*
Now rerun the previous commands.
make all make test make runtest
3. Adding PYTHON support
To add PYTHON support, run the below commands in the terminal. We need Python support to run the
cd ~/deep-learning/caffe make pycaffe echo export PYTHONPATH=~/deep-learning/caffe/python:$PYTHONPATH >> ~/.bashrc
4. Running the INCEPTIONISM code
Read the INCEPTIONISM post by Google Research here
To run the INCEPTIONISM code follow the below instructions.
4.1. Download the GoogLeNet model and clone the deepdream GitHub repository.
cd ~/deep-learning wget http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel mv bvlc_googlenet.caffemodel caffe/models/bvlc_googlenet/ git clone https://github.com/google/deepdream.git
4.2. Install the dependencies
sudo pip install "ipython[all]" sudo pip install numpy sudo pip install scipy sudo pip install protobuf sudo pip install skimage
4.3. Run the IPython Notebook
cd ~/deep=learning/deepdream ipython notebook
Now the IPython notebook dashboard will open up in your default browser as shown below.
In the list of files, click on
dream.ipynb and you will get an output as shown in the figure below.
So, this is it ! Use this IPython Notebook to play with the
deepdream code to generate some awesome images. I hope you liked the post and THANK YOU for reading.