您的位置:首页 > Web前端

分享3:ubuntu安装Caffe,及相关安装错误的解决

2019-08-14 21:01 1101 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/public669/article/details/99606216

现在很多同学都用着python一类的框架,例如Google的Tensorflow、Facebook的Pytorch、Amazon的MXNet等,而不愿意学习caffe,其实在做图像这一块,还是caffe比较的友好,知道大家都嫌caffe编译起来麻烦,其实caffe的编译安装真的很简单,接下来我就来分享一下安装caffe的过程

环境介绍:

笔者的环境是:
ubuntu16.04
cuda8.0
caffe安装:

  1. 依赖环境的安装:
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 libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install git cmake build-essential
  1. 下载caffe
    在安装路径下clone,一般默认会安装在home文件加下,文件夹名称为caffe.
    笔者就是放在home路径下的
    clone地址如下:
git clone https://github.com/BVLC/caffe.git
  1. 修改 Makefile.config文件
cd caffe
sudo cp Makefile.config.example Makefile.config

复制一份的原因是编译 caffe 时需要的是 Makefile.config 文件,而Makefile.config.example 只是caffe 给出的配置文件例子,不能用来编译 caffe
然后修改 Makefile.config 文件,在 caffe 目录下打开该文件:

sudo gedit Makefile.config

修改 Makefile.config 文件内容:
使用 cudnn

将#USE_CUDNN := 1
修改成:
USE_CUDNN := 1

应用 opencv 且版本是3的

将#WITH_PYTHON_LAYER := 1
修改为
WITH_PYTHON_LAYER := 1

修改 python 路径:

#Whatever else you find you need goes here. 下面的
INCLUDE_DIRS:= $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS:= $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/include/hdf5/serial /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/lib /usr/lib

修改cuda,笔者为cuda8.0,从下面的介绍信息可以看到,cuda<8.0的需要将60和61删除,大家根据自己的cuda版本来删除对应的

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lin Makefile.config es for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH :=-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \-gencode arch=compute_61,code=compute_61

因为笔者的cuda版本为8.0所以,笔者这里删除了60和61,结果如下

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH :=-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_61,code=compute_61
  1. 修改Makefile文件
sudo gedit Makefile
将:
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
  1. 编译
make all

make test

make runtest


make pycaffe -j8


6. 测试
ctrl+alt+t 打开命令行

输入

python

再继续输入

import caffe

如果出现以下情况则表明安装成功:

  1. 容易出错的地方一:

    这一步错误的原因是,我们在编译之前修改 Makefile.config时,没有修改正确,应该根据提示进行修改:
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH :=-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \-gencode arch=compute_61,code=compute_61

例如:你的CUDA版本小于6.0删除如下两行:

-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_61,code=sm_61 \

如果你的CUDA版本小于8.0则删除如下两行:

-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \

如果你的CUDA版本小于9.0则删除如下两行:

-gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
  1. 容易出错的地方二:
    ./include/caffe/util/hdf5.hpp:7:18: fatal error: hdf5.h: No such file or directory
In file included from src/caffe/solvers/sgd_solver.cpp:5:0:
./include/caffe/util/hdf5.hpp:7:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:591: recipe for target '.build_release/src/caffe/solvers/sgd_solver.o' failed
make: *** [.build_release/src/caffe/solvers/sgd_solver.o] Error

发现是依赖出问题了,我已经安装了hdf5了
修改Makefile.config

sudo gedit Makefile.config

修改如下:

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/include/hdf5/serial /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/lib /usr/lib

总结:

到这里安装就结束了,看得出来,安装caffe的过程不是太复杂,知道大家跟着我的步骤来,基本上都能安装成功!!
有什么问题欢迎交流:1017190168

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐