写在最前面,一定要使用从github上下载的原生工程。从其他可行地方移植过来的已经编译过了,不能在自己的电脑环境中成功运行,会带来很多麻烦!!!!!!!!!!!!!!
实验室要做一个人体姿态检测的demo,我就在网上发现了开源的openpose。网上的资料比较多,但是具体到自己的电脑,问题还是非常多的,我经历过千辛万苦,重装了好几次系统,不过最终庆幸在google的强大支持下搞定了。为了让更多的朋友能够少走弯路,也为了我这些天的辛苦能有所价值,现在记录一下整个安装过程,其中遇到的错误就没有记录了。特别的,再此感谢网友们的智慧!
笔记本: cpu:i79750H gpu:gtx 1660ti,这个显卡比较新,推荐的驱动也比较新,算力竟然是75的
各个软件工具箱版本:nvidia驱动430,cuda 10.0 cudnn7.4, opencv3.4.7(3.4.1需要修改)
整个安装过程中遇到的问题主要是显卡驱动、cuda版本、OpenCV caffe openpose兼容性和编译的问题。
我装过ubuntu16.04,但是显卡驱动老是装不成功,装上了电脑就没法正常启动了。不过同样的安装方式在ubuntu18.04上就非常好使。
第一步:安装nvidia显卡驱动
最简单的办法,添加驱动源并更新,然后在软件和更新的附加驱动里面就可以找到驱动了!我的推荐的是430版本,非常新,一下图只做演示,不一定代表我的实际情况,因为过程中没有截图。
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
没错,就是那个nvidia-drivers-410,点上它后点击apply changes,等待五分钟左右驱动安装结束,然后重启电脑,你会发现界面都变得正常了。
重启后在指令行敲下面的命令,就可以看到显卡信息了,或者nvcc -V,同时也能看到推荐的cuda版本。
nvidia-settings
第二步:安装cuda10.0
先给出下载地址:
cuda历史版本下载地址(https://developer.nvidia.com/cuda-toolkit-archive)
cudnn下载地址(https://developer.nvidia.com/rdp/cudnn-archive)
先下载cuda10.0 .run文件
再安装好依赖
sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
然后进入命令行,输入
sudo sh cuda_10.0.130_410.48_linux.run
安装时,会让你先读文章,你就直接按空格键就ok
Do you accept the previously read EULA?
accept/decline/quit: accept
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48?
(y)es/(n)o/(q)uit: n
Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y
Enter Toolkit Location
[ default is /usr/local/cuda-9.0 ]:
Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y
Install the CUDA 9.0 Samples?
(y)es/(n)o/(q)uit: y
若无警告和报错,出现下列显示,即安装暂时成功。
Installing the CUDA Toolkit in /usr/local/cuda-10.0 …
Installing the CUDA Samples in /home/textminer …
Copying samples to /home/textminer/NVIDIA_CUDA-10.0_Samples now…
Finished copying samples.
===========
= Summary =
===========
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-10.0
Samples: Installed in /home/textminer
Please make sure that
– PATH includes /usr/local/cuda-10.0/bin
– LD_LIBRARY_PATH includes /usr/local/cuda-10.0/lib64, or, add /usr/local/cuda-10.0/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-10.0/bin
Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-10.0/doc/pdf for detailed information on setting up CUDA.
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 10.0 functionality to work.
To install the driver using this installer, run the following command, replacing with the name of this run file:
sudo .run -silent -driver
Logfile is /tmp/cuda_install_6583.log
最后,配置环境变量
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
然后安装cudnn(注意要和cuda版本相符合)
下载cudnn-10.0-linux-x64-v7.6.3.30.tgz并解压到任意地方
执行命令
# 也就是把cudnn解压后把cudnn.h和libcudnn*放到cuda安装目录里面去
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*
第三步 安装opencv3.4.7
在官网下载,下载链接http://opencv.org/releases.html,选择sources版本
解压,并进入解压后的目录
编译的过程有点漫长,要有耐心,我这两天一天编译好几次,也是心酸。。。
依次执行命令
cd /home/user_name/OpenCv
mkdir Release
cd Release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..
make -j12 (12表示cpu核数)
sudo make install
sudo make install 执行完毕后OpenCV编译过程就结束了,接下来就需要配置一些OpenCV的编译环境首先将OpenCV的库添加到路径,从而可以让系统找到
sudo gedit /etc/ld.so.conf.d/opencv.conf
执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加
/usr/local/lib
执行如下命令使得刚才的配置路径生效
sudo ldconfig
配置bash
sudo gedit /etc/bash.bashrc
在最末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
保存,执行如下命令使得配置生效
source /etc/bash.bashrc
更新
sudo updatedb
至此所有的配置都已经完成
下面用一个小程序测试一下
找到
cd到opencv-3.4.7/samples/cpp/example_cmake目录下
我们可以看到这个目录里官方已经给出了一个cmake的example我们可以拿来测试下
按顺序执行
cmake .
make
./opencv_example
即可看到打开了摄像头,在左上角有一个hello opencv
即表示配置成功
第四步 Caff搭建
安装相关依赖库
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
下载caffe,这里一定要下载下面这个链接里面的,不然会有很难解决的问题
https://github.com/BVLC/caffe/tree/f019d0dfe86f49d1140961f8c7dec22130c83154
修改Makefile.config
1、进入 caffe ,将 Makefile.config.example 文件复制一份并更名为 Makefile.config ,也可以在 caffe 目录下直接调用以下命令完成复制操作 :
sudo cp Makefile.config.example Makefile.config
复制一份的原因是编译 caffe 时需要的是 Makefile.config 文件,而Makefile.config.example 只是caffe 给出的配置文件例子,不能用来编译 caffe。
2、修改 Makefile.config 文件,在 caffe 目录下打开该文件:
sudo gedit Makefile.config
在文件中替换一下几个地方:
...
将
#USE_CUDNN := 1
修改成:
USE_CUDNN := 1
...
...
#如果此处是OpenCV2,则不用修改
将
#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
...
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!
第四步 编译
在caffe目录下执行
#jn 就是cpu的核心数,j8也就是八核
sudo make all -j8
测试,上面成功了也可以不用测试,测试挺费时间的。
sudo make runtest -j8
第五步 Openpose的搭建
1、下载openpose
下载openpose
git clone https://github.com/CMU-Perceptual-Computing-Lab/openpose.git
2、安装Cmake Gui
sudo apt-get install cmake-gui
3、 利用Cmake Gui 生成build文件
命令行download模型
cd openpose
cd models
./getModels.sh
cd ..
打开cmke软件,填写openpose源码目录以及build
点击Configure按钮, 选择Unix Makefile和use default native compling,点击finish按钮
点击Generate按钮
过程中无报错,且出现configuring done,中间会出现一些红色的可配置项。
接着配置caffe编译路径
如上图所示caffe编译后的目录项填写完成
最后点击Generate按钮
4、build
编译项目
cd build/
sudo make
编译过程中出现过一个错误 cannot find #include “caffe/proto/caffe.pb.h”
通过下图方法解决问题
5、测试
如果使用的时候出现错误
Starting OpenPose demo...
Configuring OpenPose...
Starting thread(s)...
Auto-detecting camera index... Detected and opened camera 0.
Auto-detecting all available GPUs... Detected 1 GPU(s), using 1 of them starting at GPU 0.
[libprotobuf ERROR google/protobuf/message_lite.cc:118] Can't parse message of type "caffe.NetParameter" because it is missing required fields: layer[0].clip_param.min, layer[0].clip_param.max
F0903 11:10:21.062268 31283 upgrade_proto.cpp:97] Check failed: ReadProtoFromBinaryFile(param_file, param) Failed to parse NetParameter file: models/pose/body_25/pose_iter_584000.caffemodel
*** Check failure stack trace: ***
@ 0x7f2f551e10cd google::LogMessage::Fail()
@ 0x7f2f551e2f33 google::LogMessage::SendToLog()
@ 0x7f2f551e0c28 google::LogMessage::Flush()
@ 0x7f2f551e3999 google::LogMessageFatal::~LogMessageFatal()
@ 0x7f2f546eb561 caffe::ReadNetParamsFromBinaryFileOrDie()
@ 0x7f2f5468cafa caffe::Net<>::CopyTrainedLayersFromBinaryProto()
@ 0x7f2f57b73677 op::NetCaffe::initializationOnThread()
@ 0x7f2f57b97c24 op::addCaffeNetOnThread()
@ 0x7f2f57b99116 op::PoseExtractorCaffe::netInitializationOnThread()
@ 0x7f2f57b9ec93 op::PoseExtractorNet::initializationOnThread()
@ 0x7f2f57b93971 op::PoseExtractor::initializationOnThread()
@ 0x7f2f57b8e9a1 op::WPoseExtractor<>::initializationOnThread()
@ 0x7f2f57bd3f21 op::Worker<>::initializationOnThreadNoException()
@ 0x7f2f57bd4050 op::SubThread<>::initializationOnThread()
@ 0x7f2f57bd63a8 op::Thread<>::initializationOnThread()
@ 0x7f2f57bd6577 op::Thread<>::threadFunction()
@ 0x7f2f560f766f (unknown)
@ 0x7f2f558196db start_thread
@ 0x7f2f55b5288f clone
已放弃 (核心已转储)
解决方案是:
在openpose里面也有caffe,我们就在这个caffe路径下新建build,然后编译。再用cmake_gui修改openpose里面包含的caffe路径就行了。
视频:
./build/examples/openpose/openpose.bin --video examples/media/video.avi
摄像头:
./build/examples/openpose/openpose.bin --face --hand
图片:
./build/examples/openpose/openpose.bin --image_dir examples/media/ --face --hand
大多数用户不需要OpenPose C++/Python而仅仅需要使用OpenPose的Demo:
# Ubuntu
./build/examples/openpose/openpose.bin --video examples/media/video.avi
:: Windows - Portable Demo
bin\OpenPoseDemo.exe --video examples\media\video.avi
输出
查看输出文件的格式、关键点数据结构等信息,看文档:doc/output.md。
增加运算速度以及基准测试
查看增加运行速度、减少内存需求的提示,查看文档:doc/speed_up_preserving_accuracy.md。
扩展:
DensePose:
论文地址:arxiv.org/abs/1802.00434
GitHub地址:github.com/facebookresearch/Densepose
数据集地址:github.com/facebookresearch/DensePose/blob/master/INSTALL.md#fetch-densepose-data
Realtime Multi-Person Pose Estimation:
论文地址:arxiv.org/abs/1611.08050
GitHub地址:github.com/ZheC/Realtime_Multi-Person_Pose_Estimation
OpenPose C++:github.com/CMU-Perceptual-Computing-Lab/openpose
TensorFlow:github.com/ZheC/Realtime_Multi-Person_Pose_Estimation
Keras(1):modelzoo.co/model/keras-realtime-multi-person-pose-estimation
Keras(2):github.com/michalfaber/keras_Realtime_Multi-Person_Pose_Estimation
PyTorch(1):github.com/tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation
PyTorch(2):github.com/DavexPro/pytorch-pose-estimation
PyTorch(3):github.com/MVIG-SJTU/AlphaPose/tree/pytorch
MXNet:github.com/dragonfly90/mxnet_Realtime_Multi-Person_Pose_Estimation
AlphaPose:
论文地址:arxiv.org/abs/1612.00137
GitHub地址:github.com/MVIG-SJTU/AlphaPose
ArtTracker论文:arxiv.org/abs/1612.01465
DeeperCut论文:arxiv.org/abs/1605.03170
网站地址:pose.mpi-inf.mpg.de/
GitHub地址:github.com/eldar/pose-tensorflow
DeepPose:
Chainer:github.com/mitmul/deeppose
TensorFlow:github.com/asanakoy/deeppose_tf
论文地址:static.googleusercontent.com/media/research.google.com/zh-CN//pubs/archive/42237.pdf