Saturday, January 3, 2015

Caffe Deep Learning Framework Installation (Ubuntu 12.04, CPU only)

Caffe Deep Learning Framework Installation

Generally, the installation of the popular opensource deep learning framework "Caffe" is pretty straightforward on Ubuntu. If you are using the latest (or 12.04/12.10) version of Ubuntu, and familiar with the basic package installing method, usually it will be not hard for the installation of "Caffe".

According to the installation instructions, the general steps are:
  1. Install the prerequisite packages.
  2. Download the "Caffe" source code
  3. Modify the makefile.config file in the Caffe folder
  4. Make and install "Caffe"


Install the prerequisite packages

(1) Atlas:  

This is used for basic matrix and vector computation
(http://math-atlas.sourceforge.net/)
(http://www.netlib.org/blas/)

Ubuntu: sudo apt-get install libatlas-base-dev
Installs to: /usr/include/atlas

(2) Boost:

This is a C++ library. (http://www.boost.org/)

Ubuntu: sudo apt-get install libboost-all-dev
Installs into  "/usr/include/boost"

(3) OpenCV(If you have already installed opencv, no need to use this):

Ubuntu: sudo apt-get install libopencv-dev

(4) hdf5:

This a popular data format for scientific computing (http://www.hdfgroup.org/HDF5/)

Ubuntu: sudo apt-get install libhdf5-serial-dev
Install into "/usr/lib/"

(5) Python (if you don't have it and want to use the python wrapper):

Ubuntu: sudo apt-get install the python-dev


If you are using either Ubuntu 12 or 14, the above packages can be installed once in the terminal:

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev

(6) glog:

This is a logging library for C++ (http://code.google.com/p/google-glog/)
To install on Ubuntu 12:


  • Download the package: 
    • wget https://google-glog.googlecode.com/files/glog-0.3.3.tar.gz 
  • Unzip the package: 
    • tar zxvf glog-0.3.3.tar.gz
  • Install the package:
    • cd glog-0.3.3
    • ./configure
    • make
    • sudo make install

(7) gflags


      Commandline flags processing library (https://code.google.com/p/gflags/?redir=1)

  • Download the package:
    • wget https://github.com/schuhschuh/gflags/archive/master.zip
  • Unzip the package:
    • unzip master.zip
  • Install the package:
    • cd gflags-master
    • mkdir build && cd build
    • export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1
    • make
    • sudo make install

I have met some errors when doing the "make" step, which shows some errors like:

In file included from ../src/logging_unittest.cc:58:0:../src/googletest.h:177:35: error: expected ‘;’ before ‘fs’
     static void Run() { FlagSaver fs; RunTest(); }                                         ^
../src/logging_unittest.cc:866:1: note: in expansion of macro ‘TEST’
 TEST(SafeFNMatch, logging) { ^
../src/logging_unittest.cc: In static member function ‘static void Test_Strerror_logging::Run()’:
../src/googletest.h:177:25: error: ‘FlagSaver’ was not declared in this scope
     static void Run() { FlagSaver fs; RunTest(); }      \"

The solution of this error is adding :

#ifdef HAVE_LIB_GFLAGS
   using namespace gflags;
#endif

in the following files:
glog-0.3.3/src/demangle_unittest.cc
glog-0.3.3/src/logging_unittest.cc
glog-0.3.3/src/signalhandler_unittest.cc
glog-0.3.3/src/symbolize_unittest.cc
glog-0.3.3/src/utilities_unittest.cc

The insertion position is below the "using namespace xxx" in the source code files.

(8) lmdb

Memory Mapped Database (http://symas.com/mdb/)

  • Clone the package from git (must install git firstly):
    • git clone git://gitorious.org/mdb/mdb.git
  • Install the package:
    • cd mdb/libraries/liblmdb
    • make
    • sudo make install


If you are using Ubuntu 14, it would be much easier to install these packages by:
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler


After install all the packages required, don't forget to run "ldconfig",  otherwise it is possible to have "library cannot be found" error when install Caffe.




Download the "Caffe" source code

You can either use the "git clone https://github.com/BVLC/caffe.git" or just click the "Download ZIP" button in the github page (https://github.com/BVLC/caffe)
to download the Caffe source code.




Modify the makefile.config file in the Caffe folder

After unzip the download caffe package, go into the folder, copy and rename the file "Makefile.config.example"  to "Makefile.config".

Open the Makefile.config file and modify:


  • Uncomment the cpu_only flag (in my case I just use CPU for now)

                  # CPU-only switch (uncomment to build without GPU support).
                     CPU_ONLY := 1



  • Check the matlab or python path if you want to compile in either interface
                  # This is required only if you will compile the matlab interface.
                  # MATLAB directory should contain the mex binary in /bin.
                  # MATLAB_DIR := /usr/local
                  # MATLAB_DIR := /Applications/MATLAB_R2012b.app

                  # NOTE: this is required only if you will compile the python interface.
                  # We need to be able to find Python.h and numpy/arrayobject.h.
                 PYTHON_INCLUDE := /usr/include/python2.7 \
               /usr/lib/python2.7/dist-packages/numpy/core/include
                 # Anaconda Python distribution is quite popular. Include path:
                 # PYTHON_INCLUDE := $(HOME)/anaconda/include \
 # $(HOME)/anaconda/include/python2.7 \
 # $(HOME)/anaconda/lib/python2.7/site-packages/numpy/core/include
  • Save the config file.

 

Make and install "Caffe"

Just type the following commands to make the Caffe:

make all
make test
make runtest

Then you will see the following if everything goes well: