caffe(GPU)版编译安装

ubuntu22.04,python3.7,cuda11.1,cudnn8.1.1,opencv4.4.0

1.opencv4.4.0安装:

https://blog.csdn.net/qq_43552048/article/details/132069703

2.安装依赖

sudo apt-get install cmake git unzip -y
sudo apt-get install libprotobuf-dev libleveldb-dev liblmdb-dev -y
sudo apt-get install libsnappy-dev libhdf5-serial-dev protobuf-compiler -y
sudo apt-get install --no-install-recommends libboost-all-dev -y
sudo apt-get install libatlas-base-dev libopenblas-dev -y
sudo apt-get install the python3-dev python3-skimage -y
sudo pip3 install pydot -y
sudo apt-get install graphviz -y

3.下载caffe

wget -O caffe.zip https://github.com/Qengineering/caffe/archive/ssd.zip

注意这个是别人改版的,caffe官方github已经停止更新,这是官方的:https://github.com/BVLC/caffe

4.构建

解压后进入工程目录:

cd caffe-ssd

修改Makefile.config.example文件名为Makefille.config,还需修改相应的变量,这是我的仅供参考

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

# snapshot feature maps inputted to conv and fc layers
# USE_SNAPSHOT_FEATURE := 1

# display profiling results
# USE_PROFILE_DISPLAY := 1

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

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

# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 1
# 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
OPENCV_VERSION := 4

# 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 lines for compatibility.
# 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_50,code=compute_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_70,code=sm_70 \
             -gencode arch=compute_80,code=sm_80


# 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/local/include/python3.7m \
		/usr/local/lib/python3.7/site-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)
PYTHON_LIBRARIES := boost_python python3.7m
# 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/local/lib
# 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)
# WITH_PYTHON_LAYER := 1

# 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/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

# 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 ?= @

注意一定要使用非base环境,即退出anaconda,这里需要特别注意的是CUDA_ARCHPYTHON_INCLUDEPYTHON_LIBRARIES
我的是RTX30系列显卡,CUDA_ARCH注释了30和35那两行,不然后面编译会报错nvcc fatal : Unsupported gpu architecture 'compute_30',PYTHON_INCLUDE和PYTHON_LIBRARIES改成你非anaconda里的python,PYTHON_INCLUDE需要填找到你python3版本对应安装的numpy。PYTHON_LIBRARIES那个boost_python3应该是说在你的usr/local/lib下有libboost_python3.so这个文件,这一般是个软连接文件链接到你安装的那个python版本的boost库文件,比如我的是python3.7就是链接到libboost_python37.so。上面安装依赖sudo apt install libboost-all-dev可能下载的是你系统里其他python3版本的boost.so库,你的boost如果没有对应上你的python3.x版本就需要去官网下载下来源码编译

boost官方:https://www.boost.org/users/history/version_1_68_0.html
下载boost_1_68_0.tar.gz或可能其它版本

编译boost:

tar xzvf boost_1_68_0.tar.gz
cd boost_1_68_0/
sudo apt-get update  
sudo apt-get install build-essential autotools-dev libicu-dev ibbz2-dev
#卸载旧版本
# uninstall dpkg
sudo apt --purge remove libboost-dev
sudo apt --purge remove libboost-all-dev
sudo apt --purge autoremove libboost-all-dev
 
# to uninstall the version which we installed from source
sudo rm -rf /usr/lib/libboost_*
sudo rm -rf /usr/include/boost
 
./bootstrap.sh
./b2 --with-python include="/usr/local/include/python3.7m/" 
sudo ./b2 install -j4 
 
# Add the Boost libraries path to the default Ubuntu library search path
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/boost.conf'
 
sudo  ldconfig
 
#查看 boost 版本信息
cat /usr/local/include/boost/version.hpp | grep "BOOST_LIB_VERSION"
#输出如:
#  //  BOOST_LIB_VERSION must be defined to be the same as BOOST_VERSION
#  define BOOST_LIB_VERSION "1_68"
# 编译安装成功后,在 /usr/local/lib 中会生成有:libboost_python37.so 和 libboost_python37.a 等文件.
cd /usr/local/lib  
sudo ln -s libboost_python-py37.so libboost_python3.so  
sudo ln -s libboost_python-py37.a libboost_python3.a  
 
# 建立到 /usr/lib/x86_64-linux-gnu 路径的软连接
sudo cp /usr/local/lib/libboost_python37.a  /usr/lib/x86_64-linux-gnu/libboost_python-py37.a
sudo cp /usr/local/lib/libboost_python37.so.1.68.0  /usr/lib/x86_64-linux-gnu/libboost-py37.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost-py37.so libboost_python.so

参考:https://blog.csdn.net/jiangbohan789/article/details/105115998/

之后开始构建:

make clean
make all -j$(nproc)

这里可能会报感应不到opencv的问题:
1.src/caffe/util/io.cpp:6:10: fatal error: opencv2/core/core.hpp: No such file or directory
在/usr/local/include文件夹发现是opencv4,在它子文件夹是opencv2,把opencv2复制出来和opencv4同一个文件夹就可以解决。

2.Package opencv was not found in the pkg-config search path.

cd /usr/local/lib
sudo mkdir pkgconfig
cd pkgconfig
sudo touch opencv.pc

在opencv.pc中添加以下信息,注意这些信息需要与自己安装opencv时的库路径对应:

prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version:4.0.1
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann  -lopencv_core
~                                               

参考:
https://blog.csdn.net/l2181265/article/details/107080398/
https://blog.csdn.net/PecoHe/article/details/97476135

3./usr/bin/ld: cannot find -lboost_python3: No such file or directory
参考上面编译安装boost
4. /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libboost_python.so: undefined reference to PyCMethod_New
这个是因为我之前将libboost_python.so链接到python3.10版本的libboost导致的,必须链接到libboost-py37.so

构建顺利的话就显示:
在这里插入图片描述若很多错就输入make all -j$(nproc) | grep error查错就行了
最后使用python构建:

make pytest

成功最后显示:
在这里插入图片描述

5.添加环境变量

cd ~
sudo gedit ~/.bashrc

添加:

export PYTHONPATH="${PYTHONPATH}:$HOME/caffe/caffe-ssd/python"

6.测试

在这里插入图片描述
这里我好像还少了skimage和protobuf等一些包,记得之前我是sudo apt install安装了依赖的,发现是因为这些包安装在/usr/lib/python3/dist-packages里而系统搜索路径好像并没有包含这个路经:
在这里插入图片描述这里我至显示/usr/loca/…下的一些路径,解决办法:

which pip
which pip3

在这里插入图片描述
因此使用/usr/local/bin下的pip3 install XXX安装。

总体非常麻烦,整了可能快两天,若能帮到你的话麻烦点个赞求安慰一下… 最后感谢:
https://blog.csdn.net/weixin_39161727/article/details/120136500(cpu版)
https://qengineering.eu/install-caffe-on-ubuntu-20.04-with-opencv-4.4.html
https://www.bilibili.com/video/BV1oA411j7Cf/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在安装 Apt Caffe GPU 本时,其安装目录会根据不同的操作系统和安装方式有所差异。 在 Linux 系统上,通常会将 Apt Caffe GPU 安装在默认的 /usr/local 目录下。具体安装目录为 /usr/local/opt/caffe-gpu。 在 macOS 系统上,Apt Caffe GPU 本的安装目录也常为 /usr/local 目录下。具体安装目录为 /usr/local/opt/caffe-gpu。 需要注意的是,以上的目录仅为参考,实际的安装目录可能会因为个人设置、操作系统本等因素而有所不同。可以通过查看安装过程中的输出信息或者参考相应的文档来确定确切的安装目录。 另外,在安装完 Apt Caffe GPU 本后,我们也可以通过配置相关环境变量来使用相应的库和工具。比如,可以设置 LD_LIBRARY_PATH 环境变量指向 Apt Caffe GPU安装目录,以便系统能够正确查找和加载相关的动态库文件。 总之,Apt Caffe GPU 本的安装目录可能会根据不同的系统和安装方式有所变化,但一般来说会位于 /usr/local 目录下。可以通过查看安装过程中的输出信息或者相关文档来确定确切的安装目录。 ### 回答2: apt caffeGPU本默认安装目录是在/usr/local/下。apt-get命令在安装软件时会将其默认安装到该目录下,因此apt caffeGPU本也不例外。 在安装完成之后,相关的文件和目录会被安装到/usr/local/caffe/目录下。其中包括caffe的源代码、编译后的可执行文件、配置文件、模型文件等。而GPU相关的文件和目录则会在/usr/local/cuda/下。 在这些目录中,我们可以找到caffe编译后的可执行文件caffe,以及其他与caffe相关的工具。此外,也可以在/usr/local/cuda/目录下找到与GPU相关的库文件和头文件。 值得一提的是,以上路径仅仅是默认路径,用户在安装时也可以选择其他路径进行安装。在进行安装之前,可以通过apt-get命令的参数来指定安装目录,以达到自定义安装路径的目的。但需要注意的是,如果选择了非默认目录,在使用时需要注意相关文件和目录的路径。 总结起来,apt caffe GPU本的默认安装目录是/usr/local/caffe/,与之相关的GPU文件和目录则位于/usr/local/cuda/下。用户也可以选择其他自定义路径进行安装,只需在安装时进行相应的设置即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值