【caffe】caffe安装 ubuntu16.04 版

    xiaoxiao2021-03-25  80

    参考自 ubuntu16.04安装caffe以及各种问题汇总

    1、安装各种更新包

    sudo apt-get update sudo apt-get upgrade sudo apt-get install -y build-essential cmake git pkg-config sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install -y libatlas-base-dev sudo apt-get install -y --no-install-recommends libboost-all-dev sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev sudo apt-get install -y python-pip sudo apt-get install -y python-dev sudo apt-get install -y python-numpy python-scipy sudo apt-get install -y libopencv-dev

    2、下载caffe

    cd ~ mkdir git //在home下新建一个git文件夹,用来存放那些从github上git下来的文件 git clone https://github.com/BVLC/caffe.git //从github上git caffe

    3、开始安装

    cd caffe //打开到刚刚git下来的caffe cp Makefile.config.example Makefile.config //将Makefile.config.example的内容复制到Makefile.config //因为make指令只能make Makefile.config文件,而Makefile.config.example是caffe给出的makefile例子 vim Makefile.config //打开Makefile.config文件

    修改Makefile.config的配置

    //如果你不使用GPU的话,就将 # CPU_ONLY := 1 修改成: CPU_ONLY := 1 //若使用cudnn,则将 # USE_CUDNN := 1 修改成: USE_CUDNN := 1 //若使用的opencv版本是3的,则将 # OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3 //若要使用python来编写layer,则需要将 # WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1 //重要的一项# 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/local/include /usr/include/hdf5/serial LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial //这是因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径

    注意,如果编译报错hdf5相关,就是上面提到的hdf5路径未修改正确。

    4、执行python文件夹下的指令

    现在将目录cd到你之前git的caffe文件夹下:

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

    如果发现执行上述代码后,终端中有很多红字,一堆的错误之类的,那不管是什么错误都执行下面一句话:

    for req in $(cat requirements.txt); do sudo -H pip install $req --upgrade; done

    执行完上面这句话后应该就不会有很多红字错误了

    5、make 各种文件

    cd .. \\此时位置应该处于caffe文件夹下 make all -j8 //j8代表计算机cpu有8个核,因此可以多线程一起make,这样make的速度会快很多。一般常用的还有j4 make test -j8 make runtest -j8 make pycaffe //如果以后用python来开发的话必须执行这一句,一般不管你是否用python,都会执行这一句 make distribute

    至此,安装caffe差不都就已经完成了。但是笔者在安装的过程中,遇到了各种各样问题,有时候解决了这个问题,那个问题又出现了,几近崩溃的边缘,究其原因还是对ubuntu这个系统熟悉。笔者在遇到问题google的时候都将界面留存了下来,下面罗列一下遇到问题以及解决方式。

    6、caffe安装错误集锦

    (1) 在make all 的时候出现如下错误

    Error 'fatal error: pyconfig.h: No such file or directory' when executing 'make pycaffe' on dev-branch #410

    解决方案:

    make clean export CPLUS_INCLUDE_PATH=/usr/include/python2.7 make all -j8

    (2) string.h ‘memcy’ was not declared in this scope

    解决方案:打开makefile搜索并替换

    NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

    NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

    保存退出

    重新make

    (3) 类似以下情况的错误:

    .build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)' .build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'

    这种问题很有可能是你的系统中使用的是opencv3,但是你的Makefile.config中没有将OPENCV_VERSION := 3这一句取消注释。

    (4) 出现各种undefined referenceserrors

    比如:

    undefined reference togoogle::base::CheckOpMessageBuilder::NewString()'

    这类问题往往是因为gcc和g++编译器版本的问题,在ubuntu16.04中,其默认的gcc编译器版本是gcc 5,但是cuda目前又不支持高于4.9的编译器,因此会出现不兼容的情况。

    解决方法: 这里有一种比较暴力的解决方法,不确定是否安全。编辑/usr/local/cuda/include/host_config.h,将其中的第115行注释掉:

    #error -- unsupported GNU version! gcc versions later than 4.9 are not supported!

    改为

    //#error -- unsupported GNU version! gcc versions later than 4.9 are not supported!

    注意:查看现在的gcc编译器版本,可能有的人在之前安装cuda的时候会将默认的gcc、g++版本改成4.8之类比较老的版本,在make caffe的时候最好将gcc和g++的版本改到最新的,这样在make的时候才不会出现类似undefined references之类的错误。具体的修改方法可以google之。

    转载请注明原文地址: https://ju.6miu.com/read-16169.html

    最新回复(0)