caffe安装踩坑实录【cuda9.0且已经装有tensorflow】

以前是用的tensorflow的框架,但是最近需要用一下caffe,安装过程bug无数,终于装成功了,记录分享一下。
因为我的tensorflow是用anaconda3装的,看了一些教程都说如果同时安装tensorflow和caffe而且后安装caffe可能会出现一些问题,怕把我的tensorflow也搞崩,就没有在anaconda环境下安装caffe。

已安装cuda和cudnn版本

查看CUDA和cudnn版本:

cat /usr/local/cuda/version.txt
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

在这里插入图片描述网上各种教程都非常全,可以自行安装

安装opencv

之前安装过opencv3.0、opencv3.1和opencv3.3,但是都存在各种bug没有编译成功,后来使用pip安装了最新的4.0版本的,但是!装caffe的时候麻烦就来了,opencv4.0各种问题,而且因为版本太新全网很少解决办法,就卸载重新装了opencv3.4。

卸载opencv
查看已经安装的opencv版本:

pkg-config --modversion opencv

卸载的时候先进入到当初安装opencv时的build目录,进入该目录执行卸载操作:

sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*

但是并没有卸载干净,通过以下命令还是可以显示opencv的一些信息

pkg-config opencv --libs
pkg-config opencv --modversion

于是进一步的卸载opencv的相关库:

sudo apt-get autoremove opencv-doc opencv-data libopencv-dev libopencv2.4-java libopencv2.4-jni python-opencv libopencv-core2.4 libopencv-gpu2.4 libopencv-ts2.4 libopencv-photo2.4 libopencv-contrib2.4 libopencv-imgproc2.4 libopencv-superres2.4 libopencv-stitching2.4 libopencv-ocl2.4 libopencv-legacy2.4 libopencv-ml2.4 libopencv-video2.4 libopencv-videostab2.4 libopencv-objdetect2.4 libopencv-calib3d2.4

这样就把原来装的opencv卸载干净了。

安装opencv3.4
准备依赖项:

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev`

进入官网 : http://opencv.org/releases.html , 选择 3.4.6 版本的 source , 下载 opencv-3.4.6.zip :
在这里插入图片描述
解压到要安装的位置,进入已解压的文件夹opencv-3.4.6目录下,执行:

mkdir build # 创建编译的文件目录
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
sudo make -j8 #编译

编译成功后安装:

sudo make install #安装

安装后查看opencv版本验证是否安装成功:

pkg-config --modversion opencv

安装caffe

1、可以直接从官网https://github.com/BVLC/caffe下载caffe-master.zip然后解压,也可以直接git clone https://github.com/BVLC/caffe.git
2、进入caffe的根目录,将将Makefile.config.example 文件复制一份并更名为 Makefile.config ,也可以在 caffe 目录下直接调用以下命令完成复制操作 :

sudo cp Makefile.config.example Makefile.config

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

sudo gedit Makefile.config

修改Makefile.config 文件内容:

应用 cudnn

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

应用 opencv 版本

将
#OPENCV_VERSION := 3 
修改为: 
OPENCV_VERSION := 3

使用 python 接口

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

修改 python 路径

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 

去掉compute_20

找到  
# 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 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_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

4、修改 caffe 目录下的 Makefile 文件:

将: 
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS) 
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m 
替换为: 
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

5、修改 /usr/local/cuda/include/crt/host_config.h 文件 :

将
#error -- unsupported GNU version! gcc versions later than 6 are not supported!
改为
//#error -- unsupported GNU version! gcc versions later than 6 are not supported!

6、然后开始编译,在 caffe 目录下执行 :

sudo make clean  #如果编译出错需要清除之前的编译再进行新的编译
make all -j8 #这一步我就遇到了各种bug,每台电脑可能遇到的问题都有不同,要耐心些一点点去解决
make test -j8 #有些教程是跳过这步的,但是我的没有这一步会出错
make runtest -j8 

编译中遇到的各种问题汇总及解决办法

一直出现类似错误:
.build_release/lib/libcaffe.so:对‘google::protobuf::internal::StringTypeHandlerBase::Delete(std::string*)’未定义的引用
.build_release/lib/libcaffe.so:对‘google::protobuf::MessageFactory::InternalRegisterGeneratedFile(char const*, void (*)(std::string const&))’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘cv::imencode(std::string const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)’未定义的引用
.build_release/lib/libcaffe.so:对‘google::protobuf::internal::StringTypeHandlerBase::New()’未定义的引用
.build_release/lib/libcaffe.so:对‘google::protobuf::internal::WireFormatLite::WriteBytesMaybeAliased(int, std::string const&, google::protobuf::io::CodedOutputStream*)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/lib/libcaffe.so:对‘google::protobuf::internal::WireFormatLite::WriteString(int, std::string const&, google::protobuf::io::CodedOutputStream*)’未定义的引用
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/convert_imageset.bin' failed
make: *** [.build_release/tools/convert_imageset.bin] Error 1

或者
/usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFOpen@LIBTIFF_4.0’未定义的引用

/usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFReadEncodedStrip@LIBTIFF_4.0’未定义的引用

/usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFSetField@LIBTIFF_4.0’未定义的引用

/usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFSetWarningHandler@LIBTIFF_4.0’未定义的引用

/usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFSetErrorHandler@LIBTIFF_4.0’未定义的引用

在Makefile.config 中,加入:

LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib

修改gcc版本,本人电脑同时有gcc5.0和gcc4.9的版本,可能存在冲突,要统一版本,在终端把4.9的升级一下。
查看版本:

cat /proc/driver/nvidia/version
nvcc -v
./bandwidthTest
gcc --version
gcc -v  #只能看到4.9版本

解决步骤:

ll /usr/bin/gcc*
which gcc
ll /usr/bin/gcc
sudo ln -s /usr/bin/gcc-5 /usr/bin/gcc -f
sudo ln -s /usr/bin/gcc++c-5 /usr/bin/gcc -f
sudo ln -s /usr/bin/g++-5 /usr/bin/g++ -f

gcc -v
ls /usr/bin/gcc
sudo ln -s /usr/bin/gcc-5* /usr/bin/gcc -f
gcc -v
sudo ln -s /usr/bin/g++-5* /usr/bin/g++ -f
g++ -v

这时候再去查看gcc和g++的版本就都是5.4版本的了,再进行编译就每什么问题了

这个是在编译后test时遇到的问题:
.build_release/tools/caffe
.build_release/tools/caffe: error while loading shared libraries: libcudart.so.9.0: cannot open shared object file: No such file or directory
Makefile:544: recipe for target 'runtest' failed
make: *** [runtest] Error 127

原因一般有两个, 一个是操作系统里确实没有包含该共享库(lib*.so.*文件)或者共享库版本不对, 遇到这种情况那就去网上下载并安装上即可。另外一个原因就是已经安装了该共享库, 但执行需要调用该共享库的程序的时候, 程序按照默认共享库路径找不到该共享库文件。我这里就是第二种原因,解决步骤如下:

sudo cp /usr/local/cuda/lib64/libcudart.so.9.0 /usr/local/lib/libcudart.so.9.0 && sudo ldconfig

用 mnist 手写数据集验证

1、进入caffe目录,首先下载mnist数据库:

sudo ./data/mnist/get_mnist.sh 

2、然后把将二进制数据库文件转换成lmdb数据库格式:

sudo ./examples/mnist/create_mnist.sh  

我这里就遇到了如下错误:
build/examples/mnist/convert_mnist_data.bin: error while loading shared libraries: libcublas.so.9.0: cannot open shared object file: No such file or directory
解决方案参考的这里,出现这个错误的原因应该是程序按照默认共享库路径找不到需要的共享库文件。默认的共享库路径是/lib和/usr/lib,但是当我们安装第三方软件时,库文件是在安装的路径下的。这里需要找到的就是CUDA安装路径下的库文件。按照下面命令进行操作,将新共享库目录加入到共享库配置文件/etc/ld.so.conf中

sudo su
cat /etc/ld.so.conf #首先查看一下文件下本身已有的内容
echo "/usr/local/cuda-9.0/lib64" >> /etc/ld.so.conf #将想要添加的路径写进文件,这里我安装的是CUDA9.0,可以根据自己的路径更改名称
ldconfig #ldconfig是一个动态链接库管理命令,其目的为了让动态链接库为系统所共享
cat /etc/ld.so.conf #再次查看进行确认

3、训练lenet网络:

sudo ./examples/mnist/train_lenet.sh  

结果如下:
在这里插入图片描述可以看到测试的识别准确率为 98.99%,整个caffe已经配置完毕。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值