Install Caffe on?Linux Ubuntu 16.04
My Configurations
- Linux: Ubuntu 16.04 LTS
- Anaconda: Anaconda 2 (64-bit),?Anaconda2-4.3.0-Linux-x86_64.sh
- CPU or GPU: CPU
Install Caffe
1. Install CUDA 8 (Optional. I failed this installation.)
Go to the CUDA download page(https://developer.nvidia.com/cuda-downloads).
Go to the directory where the downloaded file cuda_8.0.44_linux.run?is located.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ sudo sh cuda_8.0.44_linux.run ... Installing the NVIDIA display driver... It appears that an X server is running. Please exit X before installation. If you're sure that X is not running, but are getting this error, please delete any X lock files in /tmp. =========== = Summary = =========== Driver: Installation Failed Toolkit: Installation skipped Samples: Installation skipped $ |
I got an error.
http://askubuntu.com/questions/149206/how-to-install-nvidia-run
Hit CTRL+ALT+F1 and login using your credentials.
1 2 |
$ sudo service lightdm stop $ sudo init 3 |
2. Install Anaconda
Go to the Anaconda download website(https://www.continuum.io/downloads).
Download Anaconda Python 2.7 version for Linux. I downloaded?Anaconda2-4.3.0-Linux-x86_64.sh in?/home/sjcho/Downloads. My user name is sjcho.
Then, install Anaconda.
1 2 |
$ cd /home/sjcho/Downloads $ bash Anaconda2-4.3.0-Linux-x86_64.sh |
Update Anaconda with default settings.
1 |
$ conda update conda |
Install OpenCV3.
1 |
$ conda install -c menpo opencv3 |
Add the following lines in the end of the file.bashrc. .bashrc?is located in the home folder /home/sjcho.
1 2 3 4 |
# added for Anaconda2 4.3.0 export PATH="/home/sjcho/anaconda2/bin:$PATH" # added for Caffe. export LD_LIBRARY="/home/sj/anaconda2/lib:$LD_LIBRARY_PATH" |
3. Build Caffe
Install dependencies.
1 2 3 4 5 6 7 |
$ 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 $ sudo apt-get install -y --no-install-recommends libboost-all-dev $ sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev |
Clone the Caffe directory in GitHub. My Caffe directory is going to be /home/sjcho/caffe.
1 2 |
$ cd $ git clone https://github.com/BVLC/caffe.git |
Set build settings of Caffe.
1 2 |
$ cd caffe $ cp Makefile.config.example Makefile.config |
I edited the file?Makefile.config as follows using gedit.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
## Refer to http://caffe.berkeleyvision.org/installation.html # Contributions simplifying and improving our build system are welcome! # cuDNN acceleration switch (uncomment to build with cuDNN). # USE_CUDNN := 1 # CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1 # uncomment to disable IO dependencies and corresponding data layers # USE_OPENCV := 0 # USE_LEVELDB := 0 # USE_LMDB := 0 # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) # You should not set this flag if you will be reading LMDBs with any # possibility of simultaneous read and write # ALLOW_LMDB_NOLOCK := 1 # Uncomment if you're using OpenCV 3 OPENCV_VERSION := 3 # To customize your choice of compiler, uncomment and set the following. # N.B. the default for Linux is g++ and the default for OSX is clang++ # CUSTOM_CXX := g++ # CUDA directory contains bin/ and lib/ directories that we need. CUDA_DIR := /usr/local/cuda # On Ubuntu 14.04, if cuda tools are installed via # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: # CUDA_DIR := /usr # CUDA architecture setting: going with all of them. # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility. # For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility. CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ -gencode arch=compute_20,code=sm_21 \ -gencode arch=compute_30,code=sm_30 \ -gencode arch=compute_35,code=sm_35 \ -gencode arch=compute_50,code=sm_50 \ -gencode arch=compute_52,code=sm_52 \ -gencode arch=compute_60,code=sm_60 \ -gencode arch=compute_61,code=sm_61 \ -gencode arch=compute_61,code=compute_61 # BLAS choice: # atlas for ATLAS (default) # mkl for MKL # open for OpenBlas BLAS := atlas # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. # Leave commented to accept the defaults for your choice of BLAS # (which should work)! # BLAS_INCLUDE := /path/to/your/blas # BLAS_LIB := /path/to/your/blas # Homebrew puts openblas in a directory that is not on the standard search path # BLAS_INCLUDE := $(shell brew --prefix openblas)/include # BLAS_LIB := $(shell brew --prefix openblas)/lib # 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: # Verify anaconda location, sometimes it's in root. ANACONDA_HOME := $(HOME)/anaconda2 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ $(ANACONDA_HOME)/include/python2.7 \ $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include # Uncomment to use Python 3 (default is Python 2) # PYTHON_LIBRARIES := boost_python3 python3.5m # PYTHON_INCLUDE := /usr/include/python3.5m \ # /usr/lib/python3.5/dist-packages/numpy/core/include # We need to be able to find libpythonX.X.so or .dylib. # PYTHON_LIB := /usr/lib PYTHON_LIB := $(ANACONDA_HOME)/lib # Homebrew installs numpy in a non standard path (keg only) # PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include # PYTHON_LIB += $(shell brew --prefix numpy)/lib # Uncomment to support layers written in Python (will link against Python libs) WITH_PYTHON_LAYER := 1 # Whatever else you find you need goes here. 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 # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies # INCLUDE_DIRS += $(shell brew --prefix)/include # LIBRARY_DIRS += $(shell brew --prefix)/lib # NCCL acceleration switch (uncomment to build with NCCL) # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0) # USE_NCCL := 1 # Uncomment to use `pkg-config` to specify OpenCV library paths. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) # USE_PKG_CONFIG := 1 # N.B. both build and distribute dirs are cleared on `make clean` BUILD_DIR := build DISTRIBUTE_DIR := distribute # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 # DEBUG := 1 # The ID of the GPU that 'make runtest' will use to run unit tests. TEST_GPUID := 0 # enable pretty build (comment to see full commands) Q ?= @ |
Build Caffe.
1 2 3 4 5 6 7 |
$ mkdir build $ cd build $ make clean $ cmake .. $ make all $ make install $ make runtest |
I bumped into the errors as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
[ 1%] Built target proto [ 59%] Built target caffe [ 60%] Built target gtest [100%] Built target test.testbin /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /home/sjcho/caffe/build/test/test.testbin) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /home/sjcho/caffe/build/test/test.testbin) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /home/sjcho/caffe/build/lib/libcaffe.so.1.0.0-rc4) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /home/sjcho/caffe/build/lib/libcaffe.so.1.0.0-rc4) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.58.0) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libboost_filesystem.so.1.58.0) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libglog.so.0) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/lib/x86_64-linux-gnu/libprotobuf.so.9) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libprotobuf.so.9) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libgflags.so.2) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by /usr/lib/x86_64-linux-gnu/libgflags.so.2) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /usr/lib/x86_64-linux-gnu/libleveldb.so.1) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libleveldb.so.1) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0) /home/sjcho/caffe/build/test/test.testbin: /home/sjcho/anaconda2/lib/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/libsnappy.so.1) src/caffe/test/CMakeFiles/runtest.dir/build.make:57: recipe for target 'src/caffe/test/CMakeFiles/runtest' failed make[3]: *** [src/caffe/test/CMakeFiles/runtest] Error 1 CMakeFiles/Makefile2:328: recipe for target 'src/caffe/test/CMakeFiles/runtest.dir/all' failed make[2]: *** [src/caffe/test/CMakeFiles/runtest.dir/all] Error 2 CMakeFiles/Makefile2:335: recipe for target 'src/caffe/test/CMakeFiles/runtest.dir/rule' failed make[1]: *** [src/caffe/test/CMakeFiles/runtest.dir/rule] Error 2 Makefile:240: recipe for target 'runtest' failed make: *** [runtest] Error 2 |
These errors were resolved by the following command.
1 |
$ conda install libgcc |
Rerun the command?make runtest.
1 2 3 4 5 6 7 |
$ make runtest ...omitted ... [----------] Global test environment tear-down [==========] 1104 tests from 150 test cases ran. (50063 ms total) [ PASSED ] 1104 tests. [100%] Built target runtest |
The results represent the command?make runtest was successfully operated.
Add the following line in the end of the file.bashrc.
1 |
export PYTHONPATH="/home/sjcho/caffe/python:$PYTHONPATH" |
Although I was?not sure whether pyCaffe is necessary, I built pyCaffe.
1 2 |
$ cd /home/sjcho/caffe $ make pycaffe |
Although I was not sure whether the following updates are necessary, I updated the packages.
1 2 3 |
$ cd python $ conda install cython scikit-image ipython h5py nose pandas protobuf pyyaml jupyter $ for req in $(cat requirements.txt); do pip install $req; done |
Check whether Caffe has been successfully installed.
1 2 |
$ python -c "import caffe;print caffe.__version__" 1.0.0-rc4 |
The version of Caffe was returned as?1.0.0-rc4.
4. Test an Example
First, I tested the example?Image Classification and Filter Visualization?in the Caffe homepage.? Download the iPython notebook into the directory /home/sjcho/caffe/python. Then, execute Jupyter Notebook.
1 |
$ jupyter notebook |
Open the downloaded jupyter notebook and test each instruction.
My test result of?00-classification.ipynb
How to Roll Back Installations
1. Roll Back Caffe?Build
Just delete the folder /home/sjcho/caffe/build.
1 2 |
$ cd /home/sjcho/caffe $ rm -rf build |
2. Roll Back Anaconda Installation
anaconda-clean reverts to the origin system settings before the installation.
1 2 3 |
$ conda install anaconda-clean $ anaconda-clean --yes $ rm -rf ~/anaconda2 |
References
- Install Caffe With Anaconda ? Changjiang’s blog ? My notebook for programming and others.
- I mainly followed this instruction. With Anaconda, the installation became easier than without it.
- Ubuntu 16.04 or 15.10 Installation Guide · BVLC/caffe Wiki · GitHub
- I got ideas of how to set?Makefile.config.