调用caffe
1、编译caffe
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
有几种caffe库,注意挑选使用
安装caffe-opencl,要安装依赖包opencl
sudo apt install ocl-icd-opencl-dev
安装caffe-master,要安装依赖包atlas、openblas
sudo apt-get install libatlas-dev
sudo apt-get install libopenblas-dev
要安装boost-libraries,下列语句可查看boost版本
sudo apt-cache search boost
安装boost和依赖库
sudo apt-get install libboost-dev
sudo apt-get update
sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build-essential libbz2-dev libboost-all-dev
安装GFLAG和GLOG
sudo apt-get install libgflags-dev
可以编译纯CPU版本的,也可以编译支持GPU版本的
CPU版本使用时要修改pose_solver.prototxt里的mode
2、如果有编译好的caffe,可直接在环境变量中导入pycaffe
在终端输入
sudo echo export PYTHONPATH="~/caffe/python" >> ~/.bashrc
source ~/.bashrc
3、编译通过后,检查是否能import caffe
>>>python
>>>import caffe
出现报错:ImportError: No module named skimage.io
sudo apt-get install python-skimage
出现报错:No module named google.protobuf.internal
pip install protobuf
出现报错:Could NOT find Atlas (missing: Atlas_CBLAS_LIBRARY Atlas_BLAS_LIBRARY Atlas_LAPACK_LIBRARY)
解决方案是修改变量BLAS的值,该变量在CMakeFiles.txt中有定义,将编译指令改为
cmake -DBLAS=open ..
别的库调用opencv
3、以下报错是因为opencv的版本问题,要将2改成3。
链接到OpenCV3编译好的库,可在caffe库的CMakeLists.txt中加入一句话:
set(OpenCV_DIR /root/data/OpenCV-3.4.0/share/OpenCV)
注意opencv编译库的路径位置
本地环境配置opencv
环境变量一般是在默认的library路径下找编译好的cv2.so文件,所以需要将编译好的文件放到默认路径下
直接把cv2.so文件放在/home/zt/.local/lib/python2.7/site-packages下,不要加文件夹
例如
cp OpenCV-3.4.0/lib/python2.7/dist/packages/cv2.so usr/lib/python2.7/dist/packages/cv2.x86_64-linux-gnu.so
cp OpenCV-3.4.0/lib/python2.7/dist-packages/cv2.so usr/lib/python2.7/dist-packages/cv2.x86_64-linux-gnu.so
检查链接到的OpenCV版本
>>>python
>>>import cv2
>>>print cv2.__version__
最后可以打印OpenCV的路径
>>>print cv2.__file__
查询本地opencv
pkg-config --modversion opencv
切换opencv版本
gedit ~/.bashrc
export PKG_CONFIG_PATH=/usr/local/opencv/3.10/lib/pkgconfig
export LD_LIBRARY_PATH=/usr/local/opencv/3.10/lib
source ~/.bashrc
本地配python
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH