Saturday, July 11, 2015

Torch 7 Deep Learning Installation (Ubuntu 14.04)


As an alternative to Caffe framework, Torch 7, which is maintained by Facebook Research provides a more flexible framework for machine learning algorithms, especially for popular Deep Learning. Difference from the Caffe that is developed by C++ & CUDA (see here for source codes) and wrapped with multiple script language such as python and matlab, Torch 7 is developed using C & CUDA (see here for source code) under the hood while the interface is provided through LuaJIT, an easy and efficient script language.  In this post, I install the Torch 7 framework on my Ubuntu 14.04 laptop and run the basic test programs on it, using only CPU.

To find out what is Lua, you can refer this article (http://cellux.github.io/articles/introduction-to-luajit-part-1/)


Torch 7:

Website:
http://torch.ch/
Github:
https://github.com/torch/torch7


Installation Instruction:
http://torch.ch/docs/getting-started.html#_

According to this instruction:

# in a terminal, run the commands curl -sk https://raw.githubusercontent.com/torch/ezinstall/master/install-deps | bash

The first line is to install dependencies that are required for Torch 7,
Go into this bash file (https://raw.githubusercontent.com/torch/ezinstall/master/install-deps), you can see that the dependencies are (part of it them listed):
OpenBLAS:  an optimized BLAS library, BLAS, called Basic Linear Algebra Subprograms, provides standard building blocks for performing basic vector and matrix operations. 
gcc/g++, cmake, git, gnuplot, python, etc.

This will take several minutes (mine for 20 mins) according to your computer. 

git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch; ./install.sh
Second and third lines install LuaJIT, LuaRocks, and use LuaRocks to install Torch and other packages. You can see some of the packages in their github (https://github.com/torch/distro).  Torch is installed on your computer under the path: "~/torch/".

This will take several minutes (mine for ~2 min clone from git and ~15 min to install)


Nicely Done! 

After several minutes installation, you can just type:

"th" in terminal to run torch. 

Note that if it shows "th: command not found", please refresh the bashrc file by command:
"source ~/.bashrc"


Then command "th" will give you this:





Test Torch 7:
Install torch/demos  from github by command:

"git clone https://github.com/torch/demos"

Go into the demos folder, then just run one of the demos:
"th demos/train-on-cifar/train-on-cifar.lua"

Done! You will see the demo runs to train different models on the CIFAR dataset. The screenshot is like this:
















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: