Ubuntu16.04.3服务器上opencv和caffe的配置


需要先配置好cudnn(驱动,cuda)
配置细节可以参考我之前的博客http://blog.csdn.net/bryant_meng/article/details/78559907

1 主要参考

(Ubuntu16.04环境下安装caffe及运行faster-rcnn)
http://blog.csdn.net/zoro_lov3/article/details/60581174

2 OpenCV的配置

2.1 基本依赖项

#[compiler]
apt-get install build-essential
#[required]
apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
#[optional]
apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

【error】:
E: Unable to locate package ibtbb
解决办法:apt-get updata

还继续报错就参考这个
http://blog.csdn.net/u012999651/article/details/51781691

【error】:

libtbb-dev : Depends: libtbb2 (= 4.2~20130725-1.1ubuntu1) but
4.4~20151115-0ubuntu3 is to be installed

libtbb-dev依赖项失败,libtbb-dev的安装依赖于libtbb2,然后apt-get install ibtbb2 安装的版本过高

【解决方案】下载相应的ibtbb2 版本
提供两种下载方式
(Unbuntupackages)https://packages.ubuntu.com/trusty/amd64/libtbb2/download
百度云链接http://pan.baidu.com/s/1jIsypgU 密码:p5tq
手动安装(不用apt-get install)

dpkg -i libtbb2_4.2-20130725-1.1ubuntu1_amd64.deb

然后再apt-get install libtbb-dev

安装好Anaconda的情况下,用如下方式可以一步到位的安装

conda install opencv

这里是 Anaconda安装的方法

安装成功后 import cv2 不会报错!!!
这里写图片描述

如果没有安装anaconda,请继续阅读

2.2 下载opencv

可以下载opencv的包解压,也可以用最新代码:

git clone https://github.com/Itseez/opencv.git

2.3 配置

在终端中cd到你安装的opencv文件夹下

cd /root/username/opencv

2.3.1 开始配置

mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

成功后显示如下
这里写图片描述

2.3.2 继续

make -j7

make -j 后面的数字是并行个数,cpu厉害就设大点。一般是用4
查看电脑cpu性能的方法如下:

lscpu

对应信息如下

Architecture:          i686            #cpu架构
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian   #小尾序
CPU(s):                4               #总共有4核
On-line CPU(s) list:   0-3
Thread(s) per core:    1               #每个cpu核,只能支持一个线程,即不支持超线程
Core(s) per socket:    4               #每个cpu,有4个核
Socket(s):             1               #总共有1一个cpu
Vendor ID:             GenuineIntel    #cpu产商 intel
CPU family:            6
Model:                 42
Stepping:              7
CPU MHz:               1600.000
BogoMIPS:              5986.12
Virtualization:        VT-x            #支持cpu虚拟化技术
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              6144K

到100%没提示错误的话就OK了
这里写图片描述

2.3.3 最后

make install

此时

import cv2

会报错

就可以了,更多解决import cv2报错的方法参考下面的网站

https://stackoverflow.com/questions/25215102/installing-opencv-for-python-on-ubuntu-getting-importerror-no-module-named-cv2

这里写图片描述


import cv2如果出现以下错误
这里写图片描述

ImportError: /home/ym/anaconda3/lib/python3.6/site-packages/../../libstdc++.so.6: version `GLIBCXX_3.4.22' not found (required by /home/ym/anaconda3/lib/python3.6/site-packages/../../libopencv_objdetect.so.3.3)

请参考下面这篇博客
解决libstdc++.so.6: version `GLIBCXX_3.4.22’ not found

3 caffe的配置

3.1 依赖项的安装

#基本依赖项
apt-get install libprotobuf-dev libleveldb-dev  libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler 
apt-get install --no-install-recommends libboost-all-dev 
apt-get install libatlas-base-dev

#其它依赖项
apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

3.2 配Opencv

参照前面的opencv的安装

3.3 配CuDNN

参照我之前的博客
http://blog.csdn.net/bryant_meng/article/details/78559907

3.4 下载caffe

git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config

将Makefile.config.example的内容复制到Makefile.config
因为make指令只能make Makefile.config文件,Makefile.config.example是caffe给出的makefile例子

3.5 配置caffe

vim Makefile.config

若使用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 

特别注意:修改下面两项
first:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include 

修改为:

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial 

second:

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 

修改为:

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的位置,所以需要更改这一路径,否则在make all –j8 的时候,会出现如下情况
这里写图片描述
退出并保存,然后make

make all -j8 
make test
make runtest

mak test成功后显示如下
这里写图片描述

可能出现如下问题:

/anaconda3/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用

正确解决方法

在 Makefile.config 中,加入

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

$(HOME)是你的根目录
这里写图片描述

3.6 验证是否成功

import caffe  
Traceback (most recent call last):  
File "<stdin>", line 1, in <module>  
ImportError: No module named caffe  

import caffe时候还会报错,因为没有把环境变量添加进去

【解决思路】
基本思路是把caffe中的python导入到解释器中

【解决方法】
放到配置文件中,可以永久有效果,命令操作如下:
把环境变量路径放到 ~/.bashrc文件中,打开文件

vim ~/.bashrc

在文件下方写入

export PYTHONPATH=~/caffe/python

这里写图片描述

~:是你的用户主目录,它等于 :home/<你的用户名>/

退出并保存,在终端写入下面语句,使环境变量生效

source ~/.bashrc

成功了
这里写图片描述

附录

mmcv

mmcv

https://mmcv.readthedocs.io/zh_CN/latest/get_started/installation.html

使用 mim 安装(推荐)

mim 是 OpenMMLab 项目的包管理工具,使用它可以很方便地安装 mmcv-full

pip install -U openmim
mim install mmcv-full


caffe 学习笔记

https://blog.csdn.net/kkk584520/article/list/1

https://blog.csdn.net/kkk584520/article/details/52748821

OpenCV的构建信息

OpenCV 的 getBuildInformation() 函数来获取详细的 OpenCV 构建设置和信息,包括版本控制、额外模块、平台、CPU/HW 特性、编译器信息、OpenCV 模块、GUI、Media I/O 等。这对于理解和调试 OpenCV 的配置非常有帮助。

import cv2
print(cv2.getBuildInformation())

"""
General configuration for OpenCV 4.10.0 =====================================
  Version control:               4.10.0-dirty

  Extra modules:
    Location (extra):            /io/opencv_contrib/modules
    Version control (extra):     4.10.0

  Platform:
    Timestamp:                   2024-06-17T17:56:43Z
    Host:                        Linux 5.15.0-1064-azure x86_64
    CMake:                       3.29.5
    CMake generator:             Unix Makefiles
    CMake build tool:            /bin/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (16 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (8 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (36 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (5 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                /opt/rh/devtoolset-10/root/usr/bin/c++  (ver 10.2.1)
    C++ flags (Release):         -Wl,-strip-all   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -Wl,-strip-all   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /opt/rh/devtoolset-10/root/usr/bin/cc
    C flags (Release):           -Wl,-strip-all   -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -Wl,-strip-all   -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/ffmpeg_build/lib  -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
    Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -L/ffmpeg_build/lib  -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
    ccache:                      YES
    Precompiled headers:         NO
    Extra dependencies:          /lib64/libopenblas.so Qt5::Test Qt5::Concurrent /usr/local/lib/libpng.so /lib64/libz.so Qt5::Core Qt5::Gui Qt5::Widgets Iconv::Iconv dl m pthread rt
    3rdparty dependencies:       libprotobuf ade ittnotify libjpeg-turbo libwebp libtiff libopenjp2 IlmImf ippiw ippicv

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core cvv datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python3 quality rapid reg rgbd saliency shape signal stereo stitching structured_light superres surface_matching text tracking video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 alphamat cannops cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev freetype hdf java julia matlab ovis python2 sfm ts viz
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:                           QT5
    QT:                          YES (ver 5.15.13 )
      QT OpenGL support:         NO
    GTK+:                        NO
    VTK support:                 NO

  Media I/O: 
    ZLib:                        /lib64/libz.so (ver 1.2.7)
    JPEG:                        build-libjpeg-turbo (ver 3.0.3-70)
      SIMD Support Request:      YES
      SIMD Support:              YES
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         /usr/local/lib/libpng.so (ver 1.6.43)
    TIFF:                        build (ver 42 - 4.6.0)
    JPEG 2000:                   build (ver 2.5.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (59.37.100)
      avformat:                  YES (59.27.100)
      avutil:                    YES (57.28.100)
      swscale:                   YES (6.7.100)
      avresample:                NO
    GStreamer:                   NO
    v4l/v4l2:                    YES (linux/videodev2.h)

  Parallel framework:            pthreads

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2021.11.0 [2021.11.0]
           at:                   /io/_skbuild/linux-x86_64-3.9/cmake-build/3rdparty/ippicv/ippicv_lnx/icv
    Intel IPP IW:                sources (2021.11.0)
              at:                /io/_skbuild/linux-x86_64-3.9/cmake-build/3rdparty/ippicv/ippicv_lnx/iw
    VA:                          NO
    Lapack:                      YES (/lib64/libopenblas.so)
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  OpenCL:                        YES (no extra features)
    Include path:                /io/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 /opt/python/cp39-cp39/bin/python3.9 (ver 3.9.19)
    Libraries:                   libpython3.9m.a (ver 3.9.19)
    Limited API:                 YES (ver 0x03060000)
    numpy:                       /home/ci/.local/lib/python3.9/site-packages/numpy/_core/include (ver 2.0.0)
    install path:                python/cv2/python-3

  Python (for build):            /opt/python/cp39-cp39/bin/python3.9

  Java:                          
    ant:                         NO
    Java:                        NO
    JNI:                         NO
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /io/_skbuild/linux-x86_64-3.9/cmake-install
-----------------------------------------------------------------
"""

OpenCV番外篇之获得OpenCV的构建信息

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值