Ubuntu 20.04.1安装Caffe深度学习框架指南
【代码】 Ubuntu 20.04.1 安装pycaff。
·
查看环境
系统:Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-42-generic x86_64)
caffe-master# python3 --version
Python 3.8.10
caffe-master#which python3
/usr/bin/python3
# 安装依赖库
# 更新系统软件包列表
sudo apt-get update
# 安装编译工具和基础库
sudo apt-get install -y build-essential cmake git pkg-config
# 安装 Protobuf 相关库
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev protobuf-compiler
# 安装 OpenCV 库用于图像处理
sudo apt-get install -y libopencv-dev
# 安装 HDF5 库用于数据存储
sudo apt-get install -y libhdf5-serial-dev
# 安装 ATLAS 库用于线性代数计算
sudo apt-get install -y libatlas-base-dev
# Boost 库(避免推荐包)
sudo apt-get install -y --no-install-recommends libboost-all-dev
# 安装 gflags 和 glog 用于日志和参数管理
sudo apt-get install -y libgflags-dev libgoogle-glog-dev
# 安装 LMDB 库用于数据库管理
sudo apt-get install -y liblmdb-dev
# 安装 Python 开发环境
sudo apt-get install -y python3-dev python3-pip
pip3 install numpy scipy matplotlib scikit-image protobuf==3.20.3 # 固定兼容版本
方式二
# 下载 Protocol Buffers 3.20.3 源码
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.20.3/protobuf-all-3.20.3.tar.gz
# 解压下载的文件
tar -xzvf protobuf-all-3.20.3.tar.gz
# 进入解压后的目录
cd protobuf-3.20.3
# 配置编译选项
./configure
# 编译源码
make -j$(nproc)
# 安装编译好的程序
sudo make install
# 更新动态链接库缓存
sudo ldconfig
查看依赖库
# protoc --version
libprotoc 3.20.3
caffe-master# ls /usr/lib/x86_64-linux-gnu/libboost_python38*
/usr/lib/x86_64-linux-gnu/libboost_python38.a /usr/lib/x86_64-linux-gnu/libboost_python38.so.1.71.0
/usr/lib/x86_64-linux-gnu/libboost_python38.so
caffe-master#ls /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so
/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so
/caffe-master# ls /usr/local/protobuf-3.20/lib/libprotobuf*
caffe-master# find / -name "Python.h" 2>/dev/null
# 克隆 Caffe 源码仓库
git clone https://github.com/BVLC/caffe.git
cd caffe
方式二:手动下载压缩包
GitHub 上 caffe 项目的页面:https://github.com/BVLC/caffe
在页面右侧,找到绿色的 “Code” 按钮,点击它。
在弹出的菜单中,选择 “Download ZIP” 选项。
浏览器会下载一个名为 caffe-master.zip 的压缩包到你的默认下载目录。
下载完成后,你需要解压这个压缩包。
在终端中,可以使用以下命令(假设你在 Linux 或 macOS 系统上,且 unzip 命令已安装):
# 进入压缩包所在目录
cd path_to_your_download_directory
# 解压压缩包
unzip caffe-master.zip
查看文件是否齐全
/caffe-master$ ls
build CMakeLists.txt distribute include Makefile.config models src
build.log CONTRIBUTING.md docker INSTALL.md Makefile.config.example python tools
caffe.cloc CONTRIBUTORS.md docs LICENSE Makefile.config.txt README.md
cmake data examples Makefile matlab scripts
# 配置 Makefile.config 文件 (备份或者导出>txt在本地细看配置相关内容)
cp Makefile.config.example Makefile.config
nano Makefile.config
## 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
# 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版本调整CUDA_ARCH设置
# 这里以CUDA 11.0为例,保留合适的计算能力设置
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
# 若CUDA版本 < 6.0,注释掉compute_50及以上的设置
# 若CUDA版本 < 8.0,注释掉compute_60和compute_61的设置
# 若CUDA版本 >= 9.0,注释掉compute_20和compute_21的设置
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# 强制链接顺序
LINKFLAGS += -Wl,-rpath=/usr/local/protobuf-3.20/lib \
-lprotobuf \
-lboost_python38 \
-lboost_system \
-lboost_filesystem \
-lpython3.8 \
-lhdf5_serial \
-lopencv_core -lopencv_highgui -lopencv_imgproc
# 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.
# Python配置(系统Python 3.8)
PYTHON_INCLUDE := /usr/include/python3.8 \
$(shell python3 -c "import numpy; print(numpy.get_include())")
PYTHON_LIB := /usr/lib/python3.8/config-3.8-x86_64-linux-gnu
PYTHON_LIBRARIES := boost_python38 python3.8
# 库路径配置(优先级从高到低)
LIBRARY_DIRS := /usr/local/protobuf-3.20/lib \
/usr/lib/x86_64-linux-gnu \
/usr/lib/python3.8/config-3.8-x86_64-linux-gnu \
/usr/lib/x86_64-linux-gnu/hdf5/serial
# 清理重复的路径定义,保持一致性
# 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
# 清理重复的设置和注释
# 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
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include /usr/include/hdf5/serial /usr/local/protobuf-3.20/include /usr/include/opencv4 /usr/include/opencv4/opencv2
# 清理重复的LIBRARY_DIRS定义
# 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/protobuf-3.20/lib /usr/lib/x86_64-linux-gnu/opencv4/3rdparty
# Protobuf specific paths
PROTOBUF_INCLUDE := /usr/local/protobuf-3.20/include
PROTOBUF_LIB := /usr/local/protobuf-3.20/lib
# OpenCV configuration
USE_OPENCV := 1
OPENCV_VERSION := 4
# 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 := 0
# 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 ?= @
# 设置环境变量
echo 'export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH' >> ~/.bashrc
source ~/.bashrc
开始编译
caffe-master# make clean
再次确定环境配置好
caffe-master# export PYTHONPATH=/home/xxx/Desktop/caffe-master/python:$PYTHONPATH
caffe-master# make all -j$(nproc)
# 编译 PyCaffe
make pycaffe
# 验证安装
python3 -c "import caffe; print(caffe.__version__)"
报错缺什么包就装什么包
sudo apt-get install python3-skimage
/caffe-master# pip3 install protobuf==3.20.3
/caffe-master# python -c "import google.protobuf; print(google.protobuf.__version__)"
>>> import caffe
>>> print(caffe.__version__)
1.0.0
>>> exit()
注意:
在安装过程中遇到问题报错,请根据实际报错的提示,调整caffe配置文件的路径等选项。
后续配置python接口,根据具体的错误信息进行排查和解决即可。
参考脚本
#!/bin/bash
# Caffe 自动安装脚本
# 适用于 Ubuntu 20.04 + Python 3.8 环境
# 使用方法: chmod +x install_caffe.sh && ./install_caffe.sh
set -e # 遇到错误立即退出
echo "===== Caffe 自动安装脚本 ====="
echo "=== 正在检查系统环境 ==="
# 1. 系统环境检查
if [ $(lsb_release -rs) != "20.04" ]; then
echo "[警告] 本脚本专为Ubuntu 20.04设计,当前系统版本为: $(lsb_release -ds)"
read -p "是否继续? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
if ! command -v python3 &> /dev/null; then
echo "[错误] Python3 未安装!"
exit 1
else
PYTHON_VERSION=$(python3 --version | cut -d' ' -f2)
echo "检测到 Python 版本: $PYTHON_VERSION"
if [[ ! $PYTHON_VERSION =~ ^3.8 ]]; then
echo "[警告] 推荐使用 Python 3.8.x,当前版本为 $PYTHON_VERSION"
read -p "是否继续? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
fi
# 2. 安装系统依赖
echo -e "\n=== 正在安装系统依赖 ==="
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y --no-install-recommends \
build-essential \
cmake \
git \
pkg-config \
libprotobuf-dev \
libleveldb-dev \
libsnappy-dev \
libopencv-dev \
libhdf5-serial-dev \
protobuf-compiler \
libatlas-base-dev \
libgflags-dev \
libgoogle-glog-dev \
liblmdb-dev \
python3-dev \
python3-pip \
libboost-all-dev
# 3. 安装Python依赖
echo -e "\n=== 正在安装Python依赖 ==="
pip3 install --upgrade pip
pip3 install numpy scipy matplotlib scikit-image protobuf==3.20.3
# 4. 克隆Caffe源码
echo -e "\n=== 正在获取Caffe源码 ==="
if [ -d "caffe" ]; then
echo "检测到已存在的caffe目录,跳过克隆"
else
git clone https://github.com/BVLC/caffe.git
fi
cd caffe
# 5. 配置Makefile.config
echo -e "\n=== 正在配置Makefile.config ==="
cp Makefile.config.example Makefile.config
# 自动生成配置
cat << 'EOF' > Makefile.config
## 自动生成的Caffe配置
# CPU模式 (取消注释启用CPU_ONLY)
# CPU_ONLY := 1
# CUDA配置 (根据需要修改)
CUDA_DIR := /usr/local/cuda
CUDA_ARCH := -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配置
BLAS := atlas
# Python配置
PYTHON_INCLUDE := /usr/include/python3.8 \
$(shell python3 -c "import numpy; print(numpy.get_include())")
PYTHON_LIB := /usr/lib/python3.8/config-3.8-x86_64-linux-gnu
PYTHON_LIBRARIES := boost_python38 python3.8
# 库路径配置
LIBRARY_DIRS := /usr/local/lib \
/usr/lib/x86_64-linux-gnu \
/usr/lib/python3.8/config-3.8-x86_64-linux-gnu \
/usr/lib/x86_64-linux-gnu/hdf5/serial
# 包含路径
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include \
/usr/include/hdf5/serial /usr/include/opencv4
# 启用Python层支持
WITH_PYTHON_LAYER := 1
# OpenCV配置
USE_OPENCV := 1
OPENCV_VERSION := 4
EOF
# 6. 编译安装
echo -e "\n=== 开始编译Caffe (这可能需要一些时间...) ==="
make clean
export PYTHONPATH=$(pwd)/python:$PYTHONPATH
# 使用所有CPU核心编译
CPU_CORES=$(nproc)
echo "使用 $CPU_CORES 个CPU核心进行编译"
echo -e "\n=== 编译主程序 ==="
make all -j$CPU_CORES
echo -e "\n=== 编译Python接口 ==="
make pycaffe
echo -e "\n=== 编译测试工具 ==="
make test -j$CPU_CORES
# 7. 验证安装
echo -e "\n=== 验证安装 ==="
if python3 -c "import caffe; print('Caffe安装成功! 版本:', caffe.__version__)"; then
# 添加到环境变量
echo "export PYTHONPATH=$(pwd)/python:\$PYTHONPATH" >> ~/.bashrc
echo -e "\n=== Caffe 安装完成 ==="
echo "Python路径已添加到 ~/.bashrc"
echo "请执行以下命令使更改生效:"
echo "source ~/.bashrc"
else
echo -e "\n[错误] Caffe 安装验证失败!"
echo "可能的解决方法:"
echo "1. 检查依赖是否完整安装"
echo "2. 查看编译日志寻找错误信息"
echo "3. 尝试手动编译: make clean && make all -j$CPU_CORES"
exit 1
fi
更多推荐
所有评论(0)