Openpose python API installation on Ubuntu 18.04 总结

Openpose 是最近非常火的一款图像处理开源软件,但想要把它给安装在自己的Ubuntu 电脑上的话,难度还是颇高的。从 CSDN 上的哀鸿遍野可见一斑。确实也踩了不少坑。就在本篇文章中分享下心得。

硬件配置:cpu: i7 (openpose 主要吃 gpu) gpu: Rtx2060。

必要的软件:nvidia driver 450,cuda 10.0,cudnn 7.6.3,opencv 3.4.7 (WARNING:不要不把版本当回事,极力推荐走和笔者一样的软件配置)

Openpose 的最大难点,就在于软件之间的不兼容性。nvidia driver, cuda 和 cudnn 便是罪魁祸首。在这里点名 Diss 英伟达,为了让自己新出的显卡卖得好,和老母鸡下蛋一样一直在给自己的显卡推出新的 driver 和 cuda 工具包,却又不 care 和以前的版本的兼容性问题。直接导致了 openpose 和其他一众优秀的开源软件在一个版本环境下可以运行但一旦换一个版本环境就直接起飞的窘境。这里又想起了我可怜的 jetson nx xavier 开发板,系统默认运行 cuda 11。运行参数在说明书上吹的天花乱坠但实际上绝大多数要用到 cuda 的 project 都不鸟这个所谓的最新版本。那英伟达你推出 cuda 11 有个毛线用处啊! 所以小伙伴们一定要把这个事情记在心里,openpose 成功后千万不要信了英伟达那张嘴随随便便的去更新,一更就废。

吐槽了那么久现在赶紧谈正事。首先 cite 一下 CSDN 用户樱桃木的,基于 ubuntu 18.04 安装 openpose 的文章 link,受益匪浅。但这篇文章的美中不足之处,就在于它只包含了如何安装 openpose demo 程序的教程,并不讲述如何安装 openpose python API。想要在 openpose 的基础上做进一步开发的小伙伴还请关注我的总结。

第一步:安装 nvidia 显卡驱动

一般来说,英伟达的显卡都会自带驱动的。但这套驱动正好就是 450 版本的可能性微乎其微。所以别想偷懒,这步不可以省略。否则会产生一个非常难搞的 bug, 我最后还是靠排除法发现是显卡驱动版本的问题,到后面的流程我会点出来。(英伟达你做个人吧)

这一步请参考帖子 link。 但我当时并没有完全跟着他的步骤走,最后虽然成功了但可能有运气成分。简易化的步骤如下:

若以前的驱动是通过 ppa 下载的,使用下面的命令把旧驱动卸载掉。

$ sudo apt-get remove --purge nvidia*

同样的,我们也使用 ppa 去下载新驱动。准备工作如下:

# 添加驱动源
$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get update

安装 nvidia-driver-450

sudo apt-get install nvidia-driver-450

这里和分享的帖子中不太一样,原因是近来下载英伟达 driver 的命令变了。这里也提醒大家如果碰到了感觉很莫名其妙的问题请积极 Google,情况总是赶不上变化。

安装完毕后重启 (一定要重启,要不然参数不会变的,不要像我当时那样头铁),敲入以下命令查看显卡信息。

nvidia-settings

第二步:安装cuda10.0 和 cudnn 7.6.3

请参照樱桃木大神的帖子,本文就不赘述了。只想提两个关键点:

  1. 为了避免一学就废后又不知道怎么把错误的结果删除掉,强调大家一定要下载 runfile 来安装 cuda 10.0。原因是这样安装下来在路径 /usr/local/cuda-10.0/bin 中会有一个 uninstall_cuda_10.0.pl 文件,运行它就可以安全地把 cuda 10.0 从 ubuntu 中移除出去。

    sudo /usr/local/cuda-10.0/bin/uninstall_cuda_10.0.pl
    

    否则就要使用

    sudo apt-get purge cuda-10.0
    
  2. 请参照 link 来选择安装适合 cuda 10.0 的 cudnn 版本。该网页如下图所示:令人摸不到头脑的 archive
    由此可以发现,英伟达的更新是多么的没有头绪。为了支持一个 cuda 10.0,它可以给你划分出这么多版本。。。就不能一次到位整个大点的更新吗?我个人血的教训是,别看就差0.0.1,一样可以给你整出花来。所以大家一定要忍住好奇心,别总想着:哇 cudnn 的最新版本号都吊打 3.7.4了,用它肯定棒。。。

  3. 第二点中的第三点 (懒得改了),cudnn 的卸载问题。相比较其它的软件 cudnn 其实算得上是很好管理了。当你安装完 cudnn 之后,本质上它就值两个文件。/cudnn-10.0-linux-x64-v7.6.3.30/cuda/include/cudnn.h 还有 /cudnn-10.0-linux-x64-v7.6.3.30/cuda/lib64/libcudnn*。想要让 cuda 和该 cudnn 协同工作的关键,就是要把上述两个 file 给放进 cuda 的安装目录中。命令如下:

    // An highlighted block
    sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
    sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
    sudo chmod a+r /usr/local/cuda/include/cudnn.h
    sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
    

    注意:运行上述命令的时候确保你的 terminal 在 cudnn-10.0-linux-x64-v7.6.3.30 文件夹中,对 Ubuntu 不太熟悉的小伙伴要小心。/usr/local/cuda是什么?它是一个软连接。这个文件夹其实就是你当前系统正在使用着的 cuda 程序。用户可以在自己的电脑上安装各种不同版本的 cuda, 但是计算机只能使用一种。所以各种 cuda 相关的配置都要向 /usr/local/cuda 里面做。如果你发现 /usr/local/ 路径下塞满了各种版本的 cuda, 已经导致你的系统没有办法运行你的预期版本了,请参阅这篇帖子 link 来解决问题。它把版本混乱问题解释的非常清楚。谢谢!本质上来说,如果你更换了不同的 cudnn 版本,只需要把安装后得到的那两个文件给覆盖到 /usr/local/cuda 里面去就好了。

    用于检测 cuda 和 cudnn 版本的命令:

    // test cuda
    nvcc --version
    // test cudnn
    cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
    //or
    cat /usr/include/cudnn.h | grep CUDNN_MAJOR -A 2
    // make sure outputs of the above two commands are the same
    

第三步:安装 opencv 3.4.7

这一步请完全参照樱桃木大神的帖子,非常完备非常好。确保最后的测试 exampe 里面输出的 opencv 版本和预期的一致。

第四步:安装 caffe

现在进入了安装 openpose 最关键的一步。caffe 是所有软件中最傲娇,但也是最关键的。caffe 安装好了,这个项目直接就可以说安装成功了。失败了,后面的就不用继续了。这个过程非常细致,所以请允许我直接 copy 一些樱桃木博文中的内容,关键点会被 comment 出来。
第一步,安装相关依赖库。

sudo apt-get --assume-yes install build-essential

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

第二步,请下载该 link下的 caffe。

第三步,修改 Makefile.config 文件。
Makefile.config 文件中定义了安装过程中的一系列参数。必须根据计算机上的配置和用户需求来修改这个文件,否则 caffe 不会安装成功,或者是不会包含预期的功能。为了实现 openpose python API,caffe 也必须支持 python。

  1. 进入 caffe 文件目录,将 Makefile.config.example 文件复制一份并更名为 Makefile.config。
    sudo cp Makefile.config.example Makefile.config
    
  2. 重点:修改 Makefile.config 文件。首先使用下面的命令打开 Makefile.config 文件:
    sudo gedit Makefile.config
    
    然后再该文件中做如下修改:
    ...
    将
    #USE_CUDNN:=1
    修改为
    USE_CUDNN:=1
    ...
    ...
    将
    #OPENCV_VERSION:=3
    修改为
    OPENCV_VERSION:=3
    ...
    ...
    将
    #WITH_PYTHON_LAYER:=1
    修改为
    WITH_PYTHON_LAYER:=1
    ...
    ...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
    ...
    ...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_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
    ...
    ...PYTHON_INCLUDE := /usr/include/python2.7 \ 
    	/usr/lib/python2.7/dist-packages/numpy/core/include
    修改为
    #PYTHON_INCLUDE := /usr/include/python2.7 \ 
    	#/usr/lib/python2.7/dist-packages/numpy/core/include	
    ...
    ...
    将
    #PYTHON_LIBRARIES := boost_python3 python3.5m
    #PYTHON_INCLUDE := /usr/include/python3.5m \
    	#/usr/lib/python3.5/dist-packages/numpy/core/include
    修改为
    PYTHON_LIBRARIES := boost_python3 python3.6m
    PYTHON_INCLUDE := /usr/include/python3.6m \
    	/usr/lib/python3.6/dist-packages/numpy/core/include (我的 python 版本为 3.6.9...
    ...PYTHON_LIB := /usr/lib
    修改为
    PYTHON_LIB := /usr/lib /usr/lib/x84_64-linux-gnu/   
    
    在 Makefile.config 文件中一共要做 8 次修改,以确保 caffe 在支持 openpose 之余还可以被 python3 使用。
  3. 修改 caffe 目录下的 Makefile 文件:
    ...
    将:
    NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
    替换为:
    NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
    ...
    
    ...
    将:
    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
    ...
    
  4. 修改 /usr/local/cuda/include/host_config.h 文件:
    将
    #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!
    
  5. 最后回到 caffe 目录下编译程序,执行:
    sudo make all -j16
    sudo make runtest -j16 (安装并测试 caffe)
    
    sudo make pycaffe -j16
    sudo make pytest -j16  (安装并测试 python caffe)
    

若一切都成功编译,那么 caffe 就安装成功。

问题总结:

  1. 最玄学的问题:make 过程中出现 undefined reference to “__cudaPushCallConfiguration” &"__cudaPopCallConfiguration"
    非常非常不赞同现有的在 CSDA 上的某一个对这个问题的高阅览答复,结论是 caffe 不支持 cuda 10.0,建议使用 cuda 8.0。请不幸碰到这个问题的小伙伴不要灰心。没有那么恐怖,要不然网上那么多其他人怎么成功的。我个人的解决方法是重新安装 nvidia driver 450。这个问题的本质是 driver 和 cuda 在版本的问题上打起架来了。(详细的我也不是太懂,虚心求教)版本问题着实是核心问题。

  2. make 过程中出现 error while loading shared libraries: libcudart.so.8.0: cannot open shared object file
    这一些列的问题完美的在 link 这篇帖子里得到了解答。请参阅。问题出在 caffe 找不到 cuda 相关的文件。
    以上的两个问题都是与 python caffe 无关的,如果你不想安装 python API 就应该不会碰到下面两个问题:

  3. 如果在 make 过程中出现了和 python 相关的错误,说明 Makefile.config 文件中一定有哪里设置错误了。一定确保默认的 python 2.7 被 comment 掉,自己电脑对应的 python3 版本写入正确。

  4. make 与 make pycaffe 正确,但是 make pytest 错误。这可能是因为 pytest 默认的 python 版本是 python 2.7,如果你是把 caffe 安装到 python3 版本里的话,那这里的错误是跑不掉的。另外一种检测办法是:

    python3
    >>import caffe
    

    如果 import 成功就说明 python caffe 安装成功了。

  5. import caffe 不成功,但编译全都正确。错误报告为: ImportError: dynamic module does not define module export function (PyInit__caffe)
    这个错误的原因在于你并没有把安装好的 caffe 给添入到 python3 库路径里面。请在 ~/.profile 的最后加入:

    export PYTHONPATH=~/caffe_V2_with_Python/python/:$PYTHONPATH
    

    上述命令中的 caffe 路径请按照你自己的电脑配置来修改。然后:

    source ~/.profile
    

    让新写入的配置生效。这个时候应该就可以成功 import caffe 了

At the end, let me attach my Makefile.config file to this blog:

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
# Edit 2
USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#	You should not set this flag if you will be reading LMDBs with any
#	possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
# Edit 1
OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr

# 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.
# Edit 5 cuda arch
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

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas

# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# Edit 8
#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)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		# $(ANACONDA_HOME)/include/python2.7 \
		# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

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

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

# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)
# Edit 3
WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
# Edit 4
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

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1

# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

第五步:安装 openpose & python API

今天又有一点晚了,就先写的不那么详细。说几个重点:

  1. 安装 openpose 其实就是水道渠成的事情。只要你 caffe 安装的对 openpose 安装也不会有太大问题。这个项目的安装难度大看来也不能全埋怨英伟达,毕竟 caffe 的安装还要下手改 Makefile.conf 文件,我也是第一次碰到。
  2. 安装 openpose 唯一需要改的一点(相对于樱桃木的文章),只是要在 cmake-gui 中勾选 BUILD PYTHON。其他的全部照旧就可以了。
  3. 坑的是运行 openpose python API。第一是可能无法 import pyopenpose。请不要灰心这只是系统内部的配置问题,并不是 make 过程中出了差错。第二是碰到运行 example python 程序时出现了a bytes-like object is required, not ‘NoneType’ 这样一个令人头痛不已的错误。这个其实是 example python 程序中的代码有问题,需要根据测试的图片在用户电脑中的实际储存位置来修改代码中的路径。这个错误的本质其实就是 python 程序找不到要用来做测试的图片。这个问题确实很难发现,十分感谢这篇文章 link 的作者的指点迷津。明天我会将详细的文章发出来。谢谢阅读!
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值