Tuesday, March 21, 2017

Install Caffe with Python3.6 in Anaconda on a Ubuntu 14.04 machine

1. Install Anaconda

2. Install 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
  • sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
3. Make
  • cd {path to your caffe}
  • cp Makefile.config.example Makefile.config
  • In Makefile.config, modify the following:
    • If only CPU mode, uncomment
      • CPU_ONLY := 1
    • Comment:
      • PYTHON_INCLUDE := /usr/include/python2.7 \
            /usr/lib/python2.7/dist-packages/numpy/core/include
    • Uncomment:
      • ANACONDA_HOME := $(HOME)/anaconda3
      • PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
           $(ANACONDA_HOME)/include/python3.6m \
           $(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
    • check the boost_python version library in /usr/lib/x86_64-linux-gnu/. For example, if there exists libboost_python-py34.so in this directory, uncomment and modify the line of PYTHON_LIBRARIES as
      • PYTHON_LIBRARIES := boost_python-py34 python3.6m
    • Comment 
      • PYTHON_LIB := /usr/lib
    • Uncomment
      • PYTHON_LIB := $(ANACONDA_HOME)/lib
      • WITH_PYTHON_LAYER := 1
  • In Makefile, if you meet compilation error about 'undefined reference to cv::' things, you should modifies the corresponding line as:
    • LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio
  • make all
  • make test
  • export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{path to your anaconda}/anaconda3/lib:{path to your anaconda}/anaconda3/pkgs/python-3.6.0-0/lib
  • make runtest
  • cd python
  • Modify requirements.txt: python-dateutil>=2.0
  • for req in $(cat requirements.txt); do pip install $req; done
  • make pycaffe
4. Sanity check by import caffe to python
  • export PYTHONPATH={path to your caffe}/caffe-master/python/:$PYTHONPATH
  • python3.6
  • import caffe
  • Also, you may want run some examples in jupyter notebook.