Ubuntu 16.04 Anaconda3+opencv3.4.6+Caffe+多GPU+CUDA10.1

Ubuntu 16.04 Anaconda3+opencv3.4.6+Caffe+多GPU+CUDA10.1

  • Anaconda安装
  • Caffe下载
  • 相关依赖库安装
  • opencv安装
  • caffe文件修改
    • Makefile.config文件修改
    • Makefile文件修改
    • CMakeLists.txt文件修改
    • make

默认已经安装NVIDIA显卡驱动以及CUDA、cudnn
博主的安装环境是是Ubuntu16.04 、NVIDIA 2080TI最新显卡、CUDA10.1、CUDNN10.1
如果没有安装请自行解决相关安装

Anaconda安装

  wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-4.2.0-Linux-x86_64.sh #如果没有,则需要安装wget
  bash Anaconda3-4.2.0-Linux-x86_64.sh #安装anaconda,一路yes


其他问题请查阅相关资料,建议添加中科大镜像,速度要快点、

Caffe下载

sudo apt install git
git clone https://github.com/BVLC/caffe.git #下载linux版的caffe

这里下载速度真的慢,可以直接去网站上下载,然后进行解压

相关依赖库安装

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

这下面的操作能执行就执行,其实也只是为了规避一些报错,虽然下面还会报很多很多错误。。。。。。。。。

  conda install libgcc
  conda install protobuf
  conda install -c menpo opencv3 #安装opencv3

opencv安装

这里根据CUDA版本问题,楼主的版本是CUDA10.1。
opencv3.2一直无法安装,所以建议安装opencv-3.4.6.在这里折腾了很久很久,亲测,安装3.4.6版本没有问题

  • 进入官网 : 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 ..
#  cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D FORCE_VTK=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_CUBLAS=ON -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES --expt-relaxed-constexpr" -D WITH_GDAL=ON -D WITH_XINE=ON -D BUILD_EXAMPLES=ON ..
make -j8  # CPU核数多就多设置一点
  • 然后编译,编译成功后安装:
sudo make install #安装
  • 安装完成后通过查看 opencv 版本验证是否安装成功:
    pkg-config --modversion opencv

caffe文件修改

在此之前,我们得加入以下环境变量

cd /home/user
gedit ~/.bashrc

在文件末尾添加以下两行(注意 usr为博主自己用户名,笔者这里为amax,请根据情况自行修改

export LD_LIBRARY_PATH=/home/amax/anaconda3/lib:$LD_LIBRARY_PATH
export PYTHONPATH=/home/amax/caffe/python:$PYTHONPATH

然后

source ~/.bashrc

然后进入caffe目录下

cd /home/user/caffe  #进入caffe目录
cp Makefile.config.example Makefile.config

官方推荐用kate修改make文件,不过 gedit也行
sudo apt-get install kate
博主vi也用不惯,就用low b gedit了
接着

Makefile.config文件修改

sudo gedit Makefile.config

1.查找
USE_CUDNN := 1
USE_OPENCV := 1
OPENCV_VERSION := 3
WITH_PYTHON_LAYER := 1 (这一句大概在94行 自行查找 ctrl+F)
全部取消注释
Ubuntu 16.04 Anaconda3+opencv3.4.6+Caffe+多GPU+CUDA10.1_第1张图片
39-45行修改,删除掉compute_20 21

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

Ubuntu 16.04 Anaconda3+opencv3.4.6+Caffe+多GPU+CUDA10.1_第2张图片
2.然后修改python目录,因为采用的是anaconda,所以
注释掉原先的python2.7引用目录,修改为anaconda
大概70-87行,这里的usr不修改

# PYTHON_INCLUDE := /usr/include/python2.7 \
		/usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		$(ANACONDA_HOME)/include/python3.7m \
		$(ANACONDA_HOME)/lib/python3.7m/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python-py35 python3.5m 
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib

Ubuntu 16.04 Anaconda3+opencv3.4.6+Caffe+多GPU+CUDA10.1_第3张图片*注意PYTHON_LIBRARIES修改boost_python-py35,在/usr/lib/x86_64-linux-gnu目录下查看是否有libboost_python-py35.so*
3. 大概97-100行
注释原先的INCLUDE_DIRSLIBRARY_DIRS
修改为,并添加LINKFLAGS := -Wl,-rpath,$(ANACONDA_HOME)/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 /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
LINKFLAGS := -Wl,-rpath,$(ANACONDA_HOME)/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/hdf5/serial/

在这里插入图片描述
ok! Makefile.config修改完成!保存关闭!
4. cuda10.1用户请注意
终端输入

find . -type f -exec sed -i -e 's^"hdf5.h"^"hdf5/serial/hdf5.h"^g' -e 's^"hdf5_hl.h"^"hdf5/serial/hdf5_hl.h"^g' '{}' \;
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libhdf5_serial.so.10.1.0 libhdf5.so
sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so 

5. 进入caffe/python环境目录下

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

Makefile文件修改

返回caffe目录
cd ..

sudo  gedit Makefile

修改429行
1. 注释NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
修改为
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

在这里插入图片描述2. LIBRARIES修改 大约181行左右
注释掉
# LIBRARIES += glog gflags protobuf boost_system boost_filesystem m

修改为
LIBRARIES += glog gflags protobuf leveldb snappy \ lmdb boost_system boost_filesystem hdf5_hl hdf5 m \ opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio
在这里插入图片描述
ok!保存!

CMakeLists.txt文件修改

69行# ---[ Includes修改,添加如下内容

set(${CMAKE_CXX_FLAGS} "-D_FORCE_INLINES ${CMAKE_CXX_FLAGS}")

在这里插入图片描述至此文件修改基本完成!!!

make

make all -j48

楼主两颗CPU,hahahahahaha 一般 -j4
在这里插入图片描述

make test -j48

在这里插入图片描述

make runtest -j48

这一步,很可能会报错
1.

[----------] Global test environment tear-down
[==========] 1096 tests from 150 test cases ran. (106016 ms total)
[  PASSED  ] 1095 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] BenchmarkTest/1.TestTimerMilliSeconds, where TypeParam = caffe::CPUDevice<double>
 1 FAILED TEST
Makefile:523: recipe for target 'runtest' failed
make: *** [runtest] Error 1

因为是多GPU所以
export MKL_CBWR=AUTO
2.

Warning! ***HDF5 library version mismatched error***
The HDF5 header files used to compile this application do not match
the version used by the HDF5 library to which this application is linked.
Data corruption or segmentation faults may occur if the application continues.
This can happen when an application was compiled by one version of HDF5 but
linked with a different version of static or shared HDF5 library.
You should recompile the application or check your shared library related
settings such as 'LD_LIBRARY_PATH'.
You can, at your own risk, disable this warning by setting the environment
variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.
Setting it to 2 or higher will suppress the warning messages totally.
Headers are 1.8.16, library is 1.8.18
	    SUMMARY OF THE HDF5 CONFIGURATION
	    =================================

General Information:
-------------------
		   HDF5 Version: 1.8.18
		  Configured on: Thu Nov 16 20:07:06 UTC 2017
		  Configured by: root@0dbf0ee2-5a1e-455f-760a-510cd936b9c5
		 Configure mode: production
		    Host system: x86_64-conda_cos6-linux-gnu
	      Uname information: Linux 0dbf0ee2-5a1e-455f-760a-510cd936b9c5 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
		       Byte sex: little-endian
		      Libraries: static, shared
	     Installation point: /home/amax/anaconda3

Compiling Options:
------------------
               Compilation Mode: production
                     C Compiler: /home/amax/anaconda3/bin/x86_64-conda_cos6-linux-gnu-cc
                         CFLAGS: -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -pipe -I/home/amax/anaconda3/include
                      H5_CFLAGS:   -std=c99 -pedantic -Wall -W -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -O -finline-functions
                      AM_CFLAGS: 
                       CPPFLAGS: -D_FORTIFY_SOURCE=2 -O2
                    H5_CPPFLAGS: -D_GNU_SOURCE -D_POSIX_C_SOURCE=200112L   -DNDEBUG -UH5_DEBUG_API
                    AM_CPPFLAGS:  -I/home/amax/anaconda3/include
               Shared C Library: yes
               Static C Library: yes
  Statically Linked Executables: no
                        LDFLAGS: -Wl,-O2 -Wl,--sort-common -Wl,--as-needed -Wl,-z,relro -Wl,-z,now -Wl,-rpath,/home/amax/anaconda3/lib -L/home/amax/anaconda3/lib
                     H5_LDFLAGS: 
                     AM_LDFLAGS:  -L/home/amax/anaconda3/lib
 	 	Extra libraries: -lrt -lpthread -lz -ldl -lm 
 		       Archiver: /home/amax/anaconda3/bin/x86_64-conda_cos6-linux-gnu-ar
 		 	 Ranlib: /home/amax/anaconda3/bin/x86_64-conda_cos6-linux-gnu-ranlib
 	      Debugged Packages: 
		    API Tracing: no

Languages:
----------
                        Fortran: yes
               Fortran Compiler: /home/amax/anaconda3/bin/x86_64-conda_cos6-linux-gnu-gfortran
          Fortran 2003 Compiler: yes
                  Fortran Flags: 
               H5 Fortran Flags:  
               AM Fortran Flags: 
         Shared Fortran Library: yes
         Static Fortran Library: yes

                            C++: yes
                   C++ Compiler: /home/amax/anaconda3/bin/x86_64-conda_cos6-linux-gnu-c++
                      C++ Flags: -fvisibility-inlines-hidden -std=c++17 -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -pipe -I/home/amax/anaconda3/include
                   H5 C++ Flags:  
                   AM C++ Flags: 
             Shared C++ Library: yes
             Static C++ Library: yes

Features:
---------
                  Parallel HDF5: no
             High Level library: yes
                   Threadsafety: yes
            Default API Mapping: v18
 With Deprecated Public Symbols: yes
         I/O filters (external): deflate(zlib)
                            MPE: no
                     Direct VFD: no
                        dmalloc: no
Clear file buffers before write: yes
           Using memory checker: no
         Function Stack Tracing: no
      Strict File Format Checks: no
   Optimization Instrumentation: no
Bye...
*** Aborted at 1563351176 (unix time) try "date -d @1563351176" if you are using GNU date ***
PC: @     0x7f9e5c2c2428 gsignal
*** SIGABRT (@0x3e8000037bd) received by PID 14269 (TID 0x7f9e65dd3440) from PID 14269; stack trace: ***
    @     0x7f9e5c668390 (unknown)
    @     0x7f9e5c2c2428 gsignal
    @     0x7f9e5c2c402a abort
    @     0x7f9e60caaf7c H5check_version
    @           0x4a9dc9 caffe::HDF5OutputLayerTest_TestForward_Test<>::TestBody()
    @           0x95b523 testing::internal::HandleExceptionsInMethodIfSupported<>()
    @           0x954b3a testing::Test::Run()
    @           0x954c88 testing::TestInfo::Run()
    @           0x954d65 testing::TestCase::Run()
    @           0x95603f testing::internal::UnitTestImpl::RunAllTests()
    @           0x956363 testing::UnitTest::Run()
    @           0x47129d main
    @     0x7f9e5c2ad830 __libc_start_main
    @           0x479249 _start
    @                0x0 (unknown)
Makefile:548: recipe for target 'runtest' failed
make: *** [runtest] 已放弃 (core dumped)

这个问题很麻烦
Headers are 1.8.16, library is 1.8.18
HDF5 library and header mismatch error 头文件与库文件匹配的错误
conda list
在这里插入图片描述
有两个方法

  1. conda remove hdf5
    再重新sudo make runtest -j
    但是博主测试了发现报错
error while loading shared libraries: libhdf5_hl.so.100: cannot open shared object file:

参考https://blog.csdn.net/qq_33144323/article/details/81275540

将Anaconda lib的路径添加到环境变量LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:{anaconda_dir}/lib
source ~/.bashrc
可是已经添加过,所以重新创建软链接发现最终还是HDF5 library and header 匹配问题。
2. 所以第二种,安装一样的版本。
还是先卸载hdf5conda remove hdf5
conda search hdf5
Ubuntu 16.04 Anaconda3+opencv3.4.6+Caffe+多GPU+CUDA10.1_第4张图片
很可惜 ,没有1.8.16版本.怎么办???
所以执行conda install hdf5=1.8.16失败,
查阅官网https://anaconda.org/biobuilds/hdf5

conda install -c biobuilds hdf5 

安装1.8.16成功!!!

make pycaffe
make distribute

python #python环境测试

python
>>> import caffe as cf
>>> print(cf.__version__)
1.0.0

Ubuntu 16.04 Anaconda3+opencv3.4.6+Caffe+多GPU+CUDA10.1_第5张图片
完成!!
—————————————————————————————————————————————————————
[1]:https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide#the-gpu-support-prerequisites
[2]: https://blog.csdn.net/lilin020401/article/details/94674866#第7步 安装 opencv3.4.6
[3]: https://blog.csdn.net/baidu_28342107/article/details/82022342
[4]: https://www.cnblogs.com/zjutzz/p/5716453.html?utm_source=itdadao&utm_medium=referral
[5]:https://www.cnblogs.com/bitterain/p/10529030.html

你可能感兴趣的:(Ubuntu)