Installing Caffe for Ubuntu

You must install the Caffe deep-learning framework and related packages. Caffe is used for model training and defect classification.

Procedure

  1. Install the packages that are required for Caffe by using the following commands:
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install -y build-essential cmake git pkg-config
    sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
    sudo apt-get install -y libatlas-base-dev libjasper-dev
    sudo apt-get install -y --no-install-recommends libboost-all-dev
    sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
    sudo apt-get install -y python-pip
    sudo apt-get install -y python-dev
    sudo apt-get install -y python-numpy python-scipy
    sudo apt-get install -y libopencv-dev
    sudo pip install opencv-python
    sudo pip install flask_httpauth
    sudo pip install gevent
    sudo pip install pyinotify
    sudo pip install tornado
  2. Download the Caffe source code by using the following command:
    wget https://github.com/BVLC/caffe/archive/1.0.zip
  3. Unpack the package and enter the package directory by using the following commands:
    unzip 1.0.zip
    cd ./caffe-1.0
  4. Make a copy of the make configuration file by using the following command:
    cp Makefile.config.example Makefile.config
  5. Add the following variables in the Makefile.config file:
    USE_CUDNN := 1
    CUDA_DIR := /usr/local/cuda
    PYTHON_INCLUDE := /usr/include/python2.7 \
    /usr/lib/python2.7/dist-packages/numpy/core/include
    PYTHON_LIB := /usr/lib/x86_64-linux-gnu
    WITH_PYTHON_LAYER := 1
    INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include \
    /usr/include/hdf5/serial
    LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib \
    /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
  6. In the caffe-1.0 directory, run the following command:
    find . -type f -exec sed -i -e 's^"hdf5.h"^"hdf5/serial/hdf5.h"^g' -e 's^"hdf5_hl.h"^"hdf5/serial/hdf5_hl.h"^g' '{}' \;
  7. Run the following commands:
    cd /usr/lib/x86_64-linux-gnu
    sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so
    sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so
  8. Install the required Python packages in the caffe-1.0/python directory by using the following commands:
    cd {caffe-installation-path}/caffe-1.0/python
    for req in $(cat requirements.txt); do sudo -H pip install $req --upgrade; done
    where {caffe-installation-path} is the Caffe deployment path.
  9. Open the makefile in the {caffe-installation-path} directory and change the parameter NVCCFLAGS to the following setting:
    NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
  10. In the main Caffe directory caffe-1.0, begin the Caffe build and installation by using the following commands:
    make all
    make test
    make runtest
    make pycaffe
    make distribute
  11. Add the following line to the ~/.bashrc script:
    export PYTHONPATH="/usr/lib/python2.7:{caffe-installation-path}/caffe-1.0/python:$PYTHONPATH"
    where {caffe-installation-path} is the Caffe deployment path.