https://blog.csdn.net/qq_43552048/article/details/132069703
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
wget -O caffe.zip https://github.com/Qengineering/caffe/archive/ssd.zip
注意这个是别人改版的,caffe官方github已经停止更新,这是官方的:https://github.com/BVLC/caffe
解压后进入工程目录:
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_ARCH
,PYTHON_INCLUDE
,PYTHON_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或可能其它版本
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
cd ~
sudo gedit ~/.bashrc
添加:
export PYTHONPATH="${PYTHONPATH}:$HOME/caffe/caffe-ssd/python"
这里我好像还少了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/