Install Caffe on MAC
Recently tried MAC (OS X 10.11 El Capitan) I encountered some problems when installing Caffe and Python interfaces, but I did not find a solution to these problems in the official installation tutorial. I searched for a long time (mainly on the Python Interface) and finally found a solution.
In fact, Caffe's installation involves two steps: Install dependency + compile source code
First, install the dependency:
[Necessary dependencies ]:
Homebrew is officially recommended for installation:
Brew install-vd snappy leveldb gflags glog szip lmdb # add science sources to install OpenCV and hdf5brew tap homebrew/sciencebrew install hdf5 opencv
Alternatively, the Anaconda Python tool contains a large number of Python toolkit to solve the above dependency problems and it is particularly easy to install new software packages. If you use Anaconda Python, you can skip this step if HDF5 is included. And there are two lines in opencv that need to be changed:
brew edit opencv
Then, find a line similar to the following and change it to the following (the changed Python PATH uses Anaconda's Python instead of the system's built-in Python path)
-DPYTHON_LIBRARY=#{py_prefix}/lib/libpython2.7.dylib -DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7
Then, install BOOST and BOOST-Python to compile the Python interface later.
# with Python pycaffe needs dependencies built from sourcebrew install --build-from-source --with-python -vd protobufbrew install --build-from-source -vd boost boost-python
BLAS part: some people on the Internet say that the system's built-in instability is recommended to be changed to MKL, but I can also use it if I don't change it.
Python interface: Download Caffe from Github, decompress it, and use the command line to enter the Python folder. Run the following command to install Python dependencies:
for req in $(cat requirements.txt); do pip install $req; done
Use the MAKE command to compile:
Before compilation, we need to modify make. config.
First, delete the. example Suffix of make. config. example.
ThenCPU_ONLY := 1
Because my computer is an AMD video card and cannot use CUDA, GPU acceleration cannot be implemented.
Then there is the following Python path. Use Anaconda Python to remove the following annotations:
ANACONDA_HOME := $(HOME)/anaconda
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
In this case, you must pay attention to the correct path according to the configuration of your machine.
Now you can MAKE
make allmake testmake runtest
make pycaffe
If no error is reported and a green pass is displayed after runtest is run, you can use the CPP interface after installing it. However, Python may not.
Put the caffe/python path in your PYTHONPATH. Note that/path/to/caffe/python is your caffe address. Make sure to change it.
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
Enter Python in the command line and try to import caffe. If no error is reported, you can use the Python interface normally.
Error:
- If no module is displayed when you run import caffe, it indicates that your PYTHONPATH has problems.
- If segmentation fault is encountered, it is likely that your system also has multiple versions of Python, and it is not a version during compilation and running (usually the Python with boost-Python link is the built-in version of the system ). python instead of anaconda ). At this time, you can force them to use the same Python during compilation. Use the otool-L command to check:
$ brew info boost...$ otool -L /usr/local/Cellar/boost/1.57.0/lib/libboost_system.dylib # this is one of the libs that caffe links against.../usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)...$ brew info boost-python...$ otool -L /usr/local/Cellar/boost-python/1.57.0/lib/libboost_python.dylib.../usr/local/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
Note that the version and path of boost and boost-python must be changed.
I found another solution on Github. First, check _ caffe. so:
otool -L python/caffe/_caffe.so
If he hasn't linked to your anaconda Python, then use the following command to force him to link up without the segmentation fault error:
install_name_tool -change "libpython2.7.dylib" "$HOME/anaconda/lib/libpython2.7.dylib" python/caffe/_caffe.so
Reference:
Caffe offical installation
Github:Theory of Building Caffe on OS X
Github Issue#591
This article permanently updates the link address: