cuda11估计可行(可以试试^_^),实在折腾没办法了(降低了cudnn版本),装了cuda10.1+cudnn7.6.0

 

安装caffe的主要目的是用来装openpose的环境,但是实际上单独安装caffe环境并不能直接用到openpose上,需要下载openpose自带的caffe版本。这篇仅用来参考,其实差别不大。

被这个caffe环境折腾了好几天,要崩溃了。。。

下面是正文。。。


 

依赖:
Ubuntu20.04
cuda10.1
cudnn7.6.0
OpenCV4.5.1
Python 3.8+Numpy

1. 依赖 

 1. cuda10.1+cudnn7.6.0

卸载cuda11

cd /usr/local/cuda-11.0/bin/
sudo ./cuda-uninstaller
# 选中所有cuda相关选项
sudo rm -rf /usr/local/cuda-11.0

 删除cudnn

sudo rm -rf /usr/local/cuda/include/cudnn.h
sudo rm -rf /usr/local/cuda/lib64/libcudnn*

安装cuda10.1

参考

查看cudnn版本

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

#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 0
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

2. opencv4.5.1

参考

3. python3.8+numpy

使用系统Ubuntu自带python3.8,如果配置anaconda就要更改下面安装教程对应位置的路径,仅使用caffe是没问题的

openpose部分:由于刚开始配置的是anaconda但是出了各种小问题可能是路径没配置好,免于各种路径麻烦还是用系统的吧。。。

(base) lhw@lhw:~/lib/caffe/build$ python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

安装numpy,安装完numpy可以继续使用此安装命令查看numpy安装位置

(base) lhw@lhw:~/lib/caffe/build$ pip install numpy
Requirement already satisfied: numpy in /home/lhw/anaconda3/lib/python3.8/site-packages (1.19.2)

如果使用系统环境,则在conda的base环境中再使用conda deactivate退出conda环境,然后再pip install numpy
lhw@lhw:~/Gradute/collage/openpose$ pip install numpy
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (1.20.2)

 

4. 其他依赖

我也不晓得全不全,由于折腾许久也不知道自己到底装了哪些依赖了,后面如果有错误的话先百度一下看看对应的依赖有没有安装完

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
sudo apt install libatlas-base-dev
sudo apt install libgflags-dev libgoogle-glog-dev liblmdb-dev

依赖参考

 

 2. 安装(系统自带python3.8)

1.下载源码 

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

2.修改Makefile.config  

PYTHON_LIBRARIES := boost_python38 python3.8
# 这一行根据是可以在 /usr/lib/x86_64-linux-gnu 文件夹下找到libboost_python38.so
# 这个是在之前安装依赖的时候生成的,如果没有就要安装相关的依赖,Ubuntu20.04自带python3.8
# 注意下面的中文部分
ubuntu20.04+cuda10.1+cudnn7.6.0+opencv4.5.1+python3.8安装caffe

## 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).
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
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 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
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.
# 删除20和21两行,cuda11可能要删除20--50行
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.
#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/envs/py2_caffe_source
#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)
PYTHON_LIBRARIES := boost_python38 python3.8   # 取消注释并更改版本
# 取消注释并更改版本,系统自带python3.8
PYTHON_INCLUDE := /usr/include/python3.8 \
                /usr/local/lib/python3.8/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
# 更改一下python lib路径
PYTHON_LIB :=/usr/lib/python3.8/config-3.8-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)
# 取消注释,打开python接口
WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
# 添加include路径,添加你的OpenCV路径
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /usr/local/include/opencv4
#添加lib路径,最后那个是添加cudnn路径好像也没啥用。。
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/local/cuda/lib64/libcudnn.so

# 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

# enable pretty build (comment to see full commands)
Q ?= @

Makefile.config

3.修改Makefile、CMakeLists.txt、cmake/Dependencies.cmake

# Makefile
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
PYTHON_LIBRARIES ?= boost_python38 python3.8

# CmakeLists.txt 将python_version 改成"3"
set(python_version "3" CACHE STRING "Specify which Python version to use")

# cmake/Dependencies.cmake 将python部分中的两行都修改成下面格式与libboost_python38.so相匹配
find_package(Boost 1.46 COMPONENTS "python${boost_py_version}")

 

 4. cmake指令

由于不知为啥老是出现找不到cudnn错误,所以改成cmake编译

mkdir build && cd build

cmake -DCUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so \
    -DPYTHON_LIBRARY=/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so \
    -DPYTHON_INCLUDE_DIRS=/usr/include/python3.8 ..

# 完整的是下面部分,但是两个目录好像用不到,执行上面的就够了
cmake -DCUDNN_INCLUDE_DIR=/usr/local/cuda/include \
-DCUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so \ -DPYTHON_LIBRARY=/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so \-DPYTHON_INCLUDE_DIRS=/usr/include/python3.8 \ ​ -DPYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 ..

下面这是反馈信息

ubuntu20.04+cuda10.1+cudnn7.6.0+opencv4.5.1+python3.8安装caffe

(base) lhw@lhw:~/lib/caffe/build$ cmake -DCUDNN_LIBRARY=/usr/local/cuda/lib64/libcudnn.so \
>     -DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.8.so \
>     -DPYTHON_INCLUDE_DIRS=/usr/include/python3.8 ..
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) at cmake/Misc.cmake:32 (set):
  implicitly converting \'BOOLEAN\' to \'STRING\' type.
Call Stack (most recent call first):
  CMakeLists.txt:25 (include)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.54") found components: system thread filesystem 
-- Found Threads: TRUE  
-- Found GFlags: /usr/include  
-- Found gflags  (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libgflags.so)
-- Found Glog: /usr/include  
-- Found glog    (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libglog.so)
-- Found Protobuf: /usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread (found version "3.6.1") 
-- Found PROTOBUF Compiler: /usr/bin/protoc
/home/lhw/anaconda3/bin/h5cc: 1: eval: x86_64-conda_cos6-linux-gnu-cc: not found
-- HDF5 C compiler wrapper is unable to compile a minimal HDF5 program.
/home/lhw/anaconda3/bin/h5c++: 1: eval: x86_64-conda_cos6-linux-gnu-c++: not found
-- HDF5 CXX compiler wrapper is unable to compile a minimal HDF5 program.
-- Found HDF5: /usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5_cpp.so;/usr/lib/x86_64-linux-gnu/hdf5/serial/libhdf5.so (found version "1.10.4") found components: HL 
/home/lhw/anaconda3/bin/h5cc: 1: eval: x86_64-conda_cos6-linux-gnu-cc: not found
-- HDF5 C compiler wrapper is unable to compile a minimal HDF5 program.
/home/lhw/anaconda3/bin/h5c++: 1: eval: x86_64-conda_cos6-linux-gnu-c++: not found
-- HDF5 CXX compiler wrapper is unable to compile a minimal HDF5 program.
-- Found LMDB: /usr/include  
-- Found lmdb    (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/liblmdb.so)
-- Found LevelDB: /usr/include  
-- Found LevelDB (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libleveldb.so)
-- Found Snappy: /usr/include  
-- Found Snappy  (include: /usr/include, library: /usr/lib/x86_64-linux-gnu/libsnappy.so)
-- CUDA detected: 10.1
-- Found cuDNN: ver. 7.6.0 found (include: /usr/local/cuda-10.1/include, library: /usr/local/cuda/lib64/libcudnn.so)
-- Added CUDA NVCC flags for: sm_75
-- OpenCV found (/usr/local/lib/cmake/opencv4)
-- Found Atlas: /usr/include/x86_64-linux-gnu  
-- Found Atlas (include: /usr/include/x86_64-linux-gnu library: /usr/lib/x86_64-linux-gnu/libatlas.so lapack: /usr/lib/x86_64-linux-gnu/liblapack.so
-- Found PythonInterp: /home/lhw/anaconda3/bin/python3 (found suitable version "3.8.5", minimum required is "3.0") 
-- Found PythonLibs: /usr/lib/x86_64-linux-gnu/libpython3.8.so (found suitable version "3.8.5", minimum required is "3.0") 
-- Found NumPy: /home/lhw/anaconda3/lib/python3.8/site-packages/numpy/core/include (found suitable version "1.19.2", minimum required is "1.7.1") 
-- NumPy ver. 1.19.2 found (include: /home/lhw/anaconda3/lib/python3.8/site-packages/numpy/core/include)
-- Could NOT find Boost: missing: python385 (found /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.46"))
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.46") found components: python38 
-- Could NOT find Boost: missing: python3 (found /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.46"))
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.46") found components: python 
-- Found Doxygen: /usr/bin/doxygen (found version "1.8.17") found components: doxygen dot 
-- Detected Doxygen OUTPUT_DIRECTORY: ./doxygen/
-- Found Git: /usr/bin/git (found version "2.25.1") 
-- 
-- ******************* Caffe Configuration Summary *******************
-- General:
--   Version           :   1.0.0
--   Git               :   1.0-136-g9b891540-dirty
--   System            :   Linux
--   C++ compiler      :   /usr/bin/c++
--   Release CXX flags :   -O3 -DNDEBUG -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
--   Debug CXX flags   :   -g -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
--   Build type        :   Release
-- 
--   BUILD_SHARED_LIBS :   ON
--   BUILD_python      :   ON
--   BUILD_matlab      :   OFF
--   BUILD_docs        :   ON
--   CPU_ONLY          :   OFF
--   USE_OPENCV        :   ON
--   USE_LEVELDB       :   ON
--   USE_LMDB          :   ON
--   USE_NCCL          :   OFF
--   ALLOW_LMDB_NOLOCK :   OFF
--   USE_HDF5          :   ON
-- 
-- Dependencies:
--   BLAS              :   Yes (Atlas)
--   Boost             :   Yes (ver. 1.71)
--   glog              :   Yes
--   gflags            :   Yes
--   protobuf          :   Yes (ver. 3.6.1)
--   lmdb              :   Yes (ver. 0.9.24)
--   LevelDB           :   Yes (ver. 1.22)
--   Snappy            :   Yes (ver. 1.1.8)
--   OpenCV            :   Yes (ver. 4.5.1)
--   CUDA              :   Yes (ver. 10.1)
-- 
-- NVIDIA CUDA:
--   Target GPU(s)     :   Auto
--   GPU arch(s)       :   sm_75
--   cuDNN             :   Yes (ver. 7.6.0)
-- 
-- Python:
--   Interpreter       :   /home/lhw/anaconda3/bin/python3 (ver. 3.8.5)
--   Libraries         :   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.5)
--   NumPy             :   /home/lhw/anaconda3/lib/python3.8/site-packages/numpy/core/include (ver 1.19.2)
-- 
-- Documentaion:
--   Doxygen           :   /usr/bin/doxygen (1.8.17)
--   config_file       :   /home/lhw/lib/caffe/.Doxyfile
-- 
-- Install:
--   Install path      :   /home/lhw/lib/caffe/build/install
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lhw/lib/caffe/build

cmake

 

5. make all -j8

出现错误

 error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope

cd ..
sed -i \'s/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g\' src/caffe/layers/window_data_layer.cpp
sed -i \'s/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g\' src/caffe/util/io.cpp
sed -i \'s/CV_LOAD_IMAGE_GRAYSCALE/cv::ImreadModes::IMREAD_GRAYSCALE/g\' src/caffe/util/io.cpp

#下面部分忽略。。。上面三行就可以
sed -i \'s/CV_LOAD_IMAGE_COLOR/cv::IMREAD_COLOR/g\' src/caffe/test/test_io.cpp
sed -i \'s/CV_LOAD_IMAGE_GRAYSCALE/cv::ImreadModes::IMREAD_GRAYSCALE/g\' src/caffe/test/test_io.cpp

 

回到目录继续编译

cd build
make clean
make all -j8

 编译结果:

ubuntu20.04+cuda10.1+cudnn7.6.0+opencv4.5.1+python3.8安装caffe

Scanning dependencies of target pycaffe
[ 95%] Built target upgrade_net_proto_binary
[ 97%] Building CXX object python/CMakeFiles/pycaffe.dir/caffe/_caffe.cpp.o
[ 97%] Linking CXX executable extract_features
[ 98%] Linking CXX executable upgrade_net_proto_text
[ 98%] Linking CXX executable mnist/convert_mnist_data
[ 98%] Built target convert_mnist_data
[ 98%] Built target upgrade_net_proto_text
[ 98%] Built target extract_features
[100%] Linking CXX executable cifar10/convert_cifar_data
[100%] Built target convert_cifar_data
[100%] Linking CXX executable caffe
[100%] Built target caffe.bin
[100%] Linking CXX executable cpp_classification/classification
[100%] Built target classification
[100%] Linking CXX shared library ../lib/_caffe.so
Creating symlink /home/lhw/lib/caffe/python/caffe/_caffe.so -> /home/lhw/lib/caffe/build/lib/_caffe.so
[100%] Built target pycaffe

make all -j8

6. 将caffe添加到环境变量

$ gedit ~/.bashrc
# 将你的caffe路径下的python路径放到bashrc里面,保存后退出
export PYTHONPATH=$PYTHONPATH:/home/lhw/lib/caffe/python
$ source ~/.bashrc

7.生成caffe.pb.h

这个是openpose需要做的步骤,不知道其他用途要不要,,,,咱也没用过

不然openpose会生成错误:caffe/include/caffe/blob.hpp:9:10: fatal error: caffe/proto/caffe.pb.h: 没有那个文件或目录

# 在caffe根目录
protoc src/caffe/proto/caffe.proto --cpp_out=.
mkdir include/caffe/proto
mv src/caffe/proto/caffe.pb.h include/caffe/proto

 

8. python-caffe

没报错表明安装成功

(base) lhw@lhw:~/lib/caffe/build$ python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>> 

 

参考:

http://www.ai111.vip/thread-1062-1-1.html

https://qengineering.eu/install-caffe-on-ubuntu-20.04-with-opencv-4.4.html

https://blog.csdn.net/weixin_34208185/article/details/93522778

https://blog.csdn.net/qq_35468937/article/details/81514198