Ubuntu 14.04 configure Caffe

These days, I am in the middle of the configuration of Caffe in Ubuntu14.04 due to the need of a small project. Here I want to write down the steps to show how to install it.

If you follow the installation instructions of the official site, I think you will be crazy. The documents are not in order. I succeeded to install it according to a Chinese blog. Apart from simple translation, I will also add some contents.

The whole process is as follows:

Environments:

  • Ubuntu 14.04 64bit
  • 8G Memory
  • GeForce GT 705 Graphics Card
  • CUDA 7.5
  • caffe from github

Steps:

  • Install all the dependencies
  • Install CUDA 7.5
  • Install Atlas
  • Install OpenCV
  • Install Anaconda2
  • Install Caffe
  • Compile Python wrapper
  • Test Caffe

Install all the dependencies

1
2
sudo apt-get install build-essential  # basic requirement  
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler #required by caffe

Install CUDA 7.5

You can follow the office website instructions install it. Before you install the packages, you’d better check it whether the package is intact through md5.

1
md5sum package_name

After you finish the installation, please reboot the computer.

1
sudo reboot

Later, we need to configure the environmental variable for cuda.

1
2
3
4
5
6
sudo gedit /etc/profile
# add the content into /etc/profile
export PATH=/usr/local/cuda/bin:$PATH

# make the profile into effect
source /etc/profile

At the same time, we need to add the library path to /etc/ld.so.conf.d folder.

1
2
3
4
5
6
7
cd /etc/ld.so.conf.d
sudo vim cuda.conf
# add the contents into cuda.conf file
/usr/local/cuda/lib64

# make the file into effect
sudo ldconfig

Now we install CUDA SAMPLE

1
2
3
4
5
6
cd /usr/local/cuda/samples
sudo make all -j4 # 4 means the number of your cpu cores
# It will take about 10 mins to complete installation

cd samples/bin/x86_64/linux/release
./deviceQuery
  1. ./deviceQuery Starting…
  2. CUDA Device Query (Runtime API) version (CUDART static linking)
  3. Detected 1 CUDA Capable device(s)
  4. Device 0: “GeForce GTX 670”
  5. CUDA Driver Version / Runtime Version 6.5 / 6.5
  6. CUDA Capability Major/Minor version number: 3.0
  7. Total amount of global memory: 4095 MBytes (4294246400 bytes)
  8. ( 7) Multiprocessors, (192) CUDA Cores/MP: 1344 CUDA Cores
  9. GPU Clock rate: 1098 MHz (1.10 GHz)
  10. Memory Clock rate: 3105 Mhz
  11. Memory Bus Width: 256-bit
  12. L2 Cache Size: 524288 bytes
  13. Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
  14. Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
  15. Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
  16. Total amount of constant memory: 65536 bytes
  17. Total amount of shared memory per block: 49152 bytes
  18. Total number of registers available per block: 65536
  19. Warp size: 32
  20. Maximum number of threads per multiprocessor: 2048
  21. Maximum number of threads per block: 1024
  22. Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  23. Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
  24. Maximum memory pitch: 2147483647 bytes
  25. Texture alignment: 512 bytes
  26. Concurrent copy and kernel execution: Yes with 1 copy engine(s)
  27. Run time limit on kernels: Yes
  28. Integrated GPU sharing Host Memory: No
  29. Support host page-locked memory mapping: Yes
  30. Alignment requirement for Surfaces: Yes
  31. Device has ECC support: Disabled
  32. Device supports Unified Addressing (UVA): Yes
  33. Device PCI Bus ID / PCI location ID: 1 / 0
  34. Compute Mode:
  35. < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
  36. deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 6.5, CUDA Runtime Version = 6.5, NumDevs = 1, Device0 = GeForce GTX 670
  37. Result = PASS

If you command window shows the similar result, that means your display card driver is installed successfully, otherwise there will be something wrong with your installation.

Install Atlas

1
sudo apt-get install libatlas-base-dev

Install OpenCV-2.4.10

  1. Download the installation script
  2. Go to the Install-OpenCV/Ubuntu/2.4 folder
    1
    sudo sh ./opencv2_4_10.sh

Install Anaconda2

  • Download Anaaconda2 from official website
  • Unzip and go to the folder

    1
    sudo sh ./Anaconda2-2.4.1-Linux-x86_64.sh
  • Add the Anaconda Library Path

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    sudo vim /etc/ld.so.conf
    # add the contents at the end of the file
    /home/your_name/anaconda2/lib
    # make the file into effect
    sudo ldconfig

    sudo vim ~/.bashrc
    export LD_LIBARY_PATH="/home/your_username/anaconda2/lib:$LD_LIBRARY_PATH"

    export PATH="/home/your_username/anaconda2/bin:$PATH"

    source ~/.bashrc

    #test python version

pythonversion

Install Caffe

  • Download Caffe from github

    then we need to download python dependencies

    1
    2
    3
    cd caffe-master/python
    for req in $(cat requirements.txt);
    do sudo pip install $req; done

    make sure all the dependencies have installed properly, otherwise we will meet many issues when compiling caffe

  • Compile Caffe

1
2
3
cd caffe-master
cp Makefile.config.example Makefile.config
sudo vim Makefile.config

We should make some changes to the configuration.

1
2
3
4
5
6
7
8
# Comment out these lines
ANACONDA_HOME := $(HOME)/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib
1
2
3
4
# make sure your current foler is caffe-master
sudo make all -j4
sudo make test
sudo runtest

Compile Python Wrapper

1
2
# make sure your current folder is caffe-master
sudo make pycaffe

Ok, you do a very good job! Congratulations! You have finished all the steps to install caffe. Now let’s make a small test to feel the performance of caffe.

Test

We will use the mnist example to make a test. I also followed a Chinese blog

1
sudo sh data/mnist/get_mnist.sh     # get training data and testing data from the internet

You will see four files in the folder of data/mnist

  • train-images-idx3-ubyte (training samples)
  • train-labels-idx1-ubyte (training samples’ labels)
  • t10k-images-idx3-ubyte (testing samples)
  • t10k-labels-idx1-ubyte (testing samples’ labels)
1
2
3
sudo sh examples/mnist/create_mnist.sh  # transfer the format of original datasets

sudo time sh examples/mnist/train_lenet.sh # make it run(cpu 13mins; GPU 4mins; GPU+cudnn 40 s)