Caffe 安装

安装步骤

依赖库

apt-get install  libboost-all-dev liblmdb-dev libatlas-base-dev libhdf5-serial-dev libleveldb-dev libsnappy-dev

安装 python 所需包

python 环境中只安装 numpy

for req in $(cat requirements.txt) pydot; do pip install $req; done

cmake 编译

cmake -DBUILD_docs=OFF \
    -DBUILD_python=ON \
    -Dpython_version=3 \
    -DPYTHON_LIBRARIES=/anaconda3/envs/caffe-1.0/lib \
    -DPYTHON_INCLUDE_DIR=/anaconda3/envs/caffe-1.0/include/python3.8 \
    -DPYTHON_EXECUTABLE=/anaconda3/envs/caffe-1.0/bin/python ..

安装

make all -j$(nproc)

make pycaffe -j$(nproc)

测试

make runtest -j$(nproc)

# 测试 python 接口
python -c "import sys;sys.path.append('/workspace/local/caffe-1.0/python');import caffe;from caffe import layers as L;print(caffe.__version__, caffe.__file__)"

添加到 python 搜索路径

cd caffe/python
pwd > /anaconda3/envs/caffe-1.0/lib/python3.8/site-packages/caffe.pth

# 测试 python 接口
python -c "import caffe;from caffe import layers as L;print(caffe.__version__, caffe.__file__)"

安装问题

  1. Error: libboost_python.so: undefined reference to `PyClass_Type

    原因分析:libboost_python.so 系统默认链接的是 libboost_python-py2.7.so,而编译 pycaffe 使用的是 python3。

    解决方法:

     cd /usr/lib/x86_64-linux-gnu/
     unlink libboost_python.so
     ln -s libboost_python-py35.so libboost_python.so
    
  2. protobuf error: ‘static_assert’ was not declared in this scope

    解决方法:使用 c++11 编译,添加编译参数 -std=c++11

  3. cannot find -lboost_python3

    解决方法:将链接的 boost_python3 库,修改为 boost_python-py35

Table of Contents