安装caffe,CPU版本的一些参考和问题的解决
1.安装前的预备工作
依次按照一下命令安装:
1 sudo apt-get install libprotobuf-dev 2 sudo apt-get install libleveldb-dev 3 sudo apt-get install libsnappy-dev 4 sudo apt-get install libopencv-dev 5 sudo apt-get install libhdf5-serial-dev 6 sudo apt-get install protobuf-compiler 7 sudo apt-get install --no-install-recommends libboost-all-dev 8 sudo apt-get install libatlas-base-dev 9 sudo apt-get install python-dev 10 sudo apt-get install libgflags-dev 11 sudo apt-get install libgoogle-glog-dev 12 sudo apt-get install liblmdb-dev
2.下载caffe源码
在github上下载
git clone https://github.com/BVLC/caffe.git
接着装caffe要求装的一些依赖库(如requirements.txt【$caffe_root/python】中的内容所示):
Cython>=0.19.2 numpy>=1.7.1 scipy>=0.13.2 scikit-image>=0.9.3 matplotlib>=1.3.1 ipython>=3.0.0 h5py>=2.2.0 leveldb>=0.191 networkx>=1.8.1 nose>=1.3.0 pandas>=0.12.0 python-dateutil>=1.4,<2 protobuf>=2.5.0 python-gflags>=2.0 pyyaml>=3.10 Pillow>=2.3.0 six>=1.1.0
可以自己一个一个装,也可以通过以下命令:
1 pip install -r requirements.txt
3.编译caffe
3.1 使用Cmake编译
进入caffe根目录,创建一个build文件夹并进入
1 mkdir build && cd build
进行cmake
cmake -DCPU_ONLY=1 ..
接着make
make -j"$(nproc)"
3.2 使用make编译(我使用的这种)
首先因为我们需要的是CPU版本的caffe,因此需要对Makefile进行修改,我们先创建一个副本
1 cp Makefile.config.example Makefile.config
接着对内容进行修改
我们去掉CPU_ONLY前面的注释
# CPU-only switch (uncomment to build without GPU support). # CPU_ONLY := 1 修改为 # CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1
接着python的文件路径需要我们修改
# 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 修改为 PYTHON_INCLUDE := /usr/include/python2.7 \ /usr/local/lib/python2.7/dist-packages/numpy/core/include
hdf5库的路径也需要添加
# Whatever else you find you need goes here. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 修改为 # 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/hdf5
同时Makefile中的关于hdf5的内容也需要修改
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 修改为 LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
不然可能会出现以下错误
/usr/bin/ld: cannot find -lhdf5_hl /usr/bin/ld: cannot find -lhdf5 collect2: error: ld returned 1 exit status
接着开始编译过程
make pycaffe make all make test make runtest
可在其后加上-j"$(nproc)"以加快速度
可能在进行make runtest时出现一些错误,但若前三个命令没有错误出现,就不妨碍使用(暂时没发现什么妨碍的地方)
4.配置环境变量
在.bashrc中配置环境变量
sudo gedit ~/.bashrc
在后面加上
export PYTHONPATH=/your path/caffe/python:$PYTHONPATH
接着
source ~/.bashrc
5.测试
Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import caffe >>> dir(caffe) [\'AdaDeltaSolver\', \'AdaGradSolver\', \'AdamSolver\', \'Classifier\', \'Detector\', \'Layer\', \'NCCL\', \'NesterovSolver\', \'Net\', \'NetSpec\', \'RMSPropSolver\', \'SGDSolver\', \'TEST\', \'TRAIN\', \'Timer\', \'__builtins__\', \'__doc__\', \'__file__\', \'__name__\', \'__package__\', \'__path__\', \'__version__\', \'_caffe\', \'classifier\', \'detector\', \'get_solver\', \'has_nccl\', \'init_log\', \'io\', \'layer_type_list\', \'layers\', \'log\', \'net_spec\', \'params\', \'proto\', \'pycaffe\', \'set_device\', \'set_mode_cpu\', \'set_mode_gpu\', \'set_multiprocess\', \'set_random_seed\', \'set_solver_count\', \'set_solver_rank\', \'solver_count\', \'solver_rank\', \'to_proto\'] >>>
没问题,OK~
官方github:https://github.com/BVLC/caffe
官方安装文档:http://caffe.berkeleyvision.org/installation.html
参考:https://blog.csdn.net/muzilinxi90/article/details/53673184