编译支持 python3 的caffe 和 pycaffe:
1、cd ${caffe_root}
2、vi Makefile.config
去掉 71-73 行前的#
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include
去掉 84 行前的#
WITH_PYTHON_LAYER := 1
3、make clean
4、make all -j8
如果有错误 /usr/bin/ld: cannot find -lboost_python3
则执行 ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so
继续 make
5、make pycaffe
如果有错误 /usr/bin/ld: cannot find -lboost_python-py37
/usr/bin/ld: cannot find -lpython3.7m 等等,按4的方法建立软链接,用 py37 指向 py35 没关系。
如果有错误 python/caffe/_caffe.cpp:11:31: fatal error: numpy/arrayobject.h: No such file or directory
使用 sudo apt-get install python-numpy
6、make distribute
7、vi ~/.bashrc
在最后加入
export PYTHONPATH=${caffe_root}/distribute/python:$PYTHONPATH
export LD_LIBRARY_PATH=${caffe_root}/distribute/lib:$LD_LIBRARY_PATH
8、source ~/.bashrc
测试方法:
在 python3 中 import caffe,没有错误就可以了。
2021-12-24 更新:
1、如果 make all 出现和 boost 相关的错误,则需要重装 boost,参考:ubuntu 安装boost 1.58
2、如果 import caffe, 出现 “libcaffe-nv.so.0.17: undefined symbol: _ZNK5boost9re_detail31cpp_regex_traits_implementationIcE9transformB5cxx11EPKcS4_” 这样的错误,
2a、先 ldd libcaffe-nv.so.0.17,找到其使用的 libboost_regex.so 的位置。
2b、用 find / -name libboost_regex* 找到系统中所有的 libboost_regex 库。
2c、用 nm -D libboost_regexXXX.so | grep _ZNK5boost9re_detail31cpp_regex_traits_implementationIcE9transformB5cxx11EPKcS4_逐个查看,找到正确的版本。
2d、从2a位置建立软链接到2c位置。