Ubuntu-Caffe安装记录

1. 安装环境

  • Ubuntu18.04.6 LTS
  • Python 3.6.13
  • Nvidia Geforce 1070
  • CUDA 10.1
  • CuDNN 8.1.1
  • torch==1.8.2

2. 安装步骤

2.1 安装部分依赖文件

# python3 modules (numpy, protobuf, skimage)
sudo pip3 install numpy
sudo apt-get install python3-skimage
sudo apt-get install python3-protobuf

# build essential
sudo apt-get install build-essential cmake git pkg-config

# gflags, glog, lmdb
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

# boost
sudo apt-get install libboost-all-dev

# hdf5
sudo apt-get install libhdf5-dev

# protobuf
sudo apt-get install protobuf-compiler libprotobuf-dev

# blas
sudo apt-get install libblas-dev libcblas-dev libatlas-base-dev libopenblas-dev

# leveldb
sudo apt-get install libleveldb-dev

# snappy
sudo apt-get install libsnappy-dev

2.2 安装OpenCV

这里以OpenCV3.3.1为例,更高版本也能正常运行

2.2.1 安装部分依赖

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

2.2.2 下载源码

下载地址:https://github.com/opencv/opencv/archive/refs/tags/3.3.1.tar.gz
下载后解压到安装目录下,解压后的文件名为opencv-3.3.1。

2.2.3 开始安装

2.2.3.1 预备阶段

cd "Your Path"/opencv-3.3.1
mkdir build  # 创建工程编译所需文件夹
cd build

2.2.3.2 配置文件

CMAKE_BUILD_TYPE =RELEASE指明编译的版本是Release版,CMAKE_INSTALL_PREFIX=/usr/local指明编译后的可执行程序的存放目录。

注意,后面的两个点千万不能省,代表了上级目录

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

2.2.3.3 编译和安装

-j8指8线程同时编译,根据自己CPU的特点调整该参数,如果不了解自己CPU,直接执行make即可。

make -j8     # 多线程执行make任务
sudo make install

2.2.3.4 查看安装信息

pkg-config --modversion opencv  #查看安装版本

2.2.3.5 验证程序

创建main.cpp文件,加入如下代码:

#include 
#include 
 
using namespace std;
using namespace cv;
 
int main()
{
    Mat srcImage = imread("lena.jpg");
    imshow("Image",srcImage);
 
    waitKey(0);
 
    return 0;
}

2.3 安装Caffe

2.3.1 下载源码

cd ~/"Your Caffe setup route" # 开发环境目录
git clone https://github.com/BVLC/caffe
cd caffe
cp Makefile.config.example Makefile.config

2.3.2 修改Makefile.config文件

  • 取消对CPU_ONLY := 1的注释(第8行)
# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1
  • 取消对OPENCV_VERSION := 3的注释(第22行)
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
  • 取消对PYTHON_INCLUDE的注释(第80行),并对之前的python2对应的PYTHON_INCLUDE进行注释
# 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)/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-py36
PYTHON_INCLUDE := /usr/include/python3.6m \
                 /usr/lib/python3.6/dist-packages/numpy/core/include
  • 配置引用文件路径(第96行)(主要是HDF5的路径问题)
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /opt/Opencv2/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /opt/Opencv2/lib
  • 编译并测试
sudo make all
sudo make test
sudo make runtest

2.4 安装Caffe-Python接口

2.4.1 安装Python接口依赖库

在caffe根目录下,有个python文件夹,文件夹里面有个requirements.txt,里面有需要的依赖库和版本信息,按照其安装即可,在安装前,需要先安装fortran编辑器(gfrotran),因为安装scipy库时需要它,命令如下:

sudo apt-get install gfortran
cd "Your route"/caffe/python
for req in $(cat requirements.txt); do pip install $req; done

2.4.2 将caffe根目录下的python文件夹加入到环境变量

在caffe根目录下,有个python文件夹,文件夹里面有个requirements.txt,里面有需要的依赖库和版本信息,按照其安装即可,在安装前,需要先安装fortran编辑器(gfrotran),因为安装scipy库时需要它,命令如下:

sudo apt-get install gfortran
cd "Your route"/caffe/python
for req in $(cat requirements.txt); do pip install $req; done

2.4.2 将caffe根目录下的python文件夹加入到环境变量

2.4.2.1 先打开配置文件bashrc

sudo gedit ~/.bashrc

2.4.2.2在文件的最后面添加

export PYTHONPATH="Your Own Path"/caffe/python:$PYTHONPATH

注意,这里路径根据每个人电脑则不一样,可以自行进入自己的caffe文件夹里的python目录下,输入pwd查看路径。

2.4.2.3 然后执行更新配置(建议重启电脑)

sudo ldconfig

2.4.2.4 编译python接口

cd "Your Own Path"/caffe/
make pycaffe

2.4.2.5 验证python接口

Python 3.6.13 |Anaconda, Inc.| (default, Jun  4 2021, 14:25:59) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
>>>

到此Caffe全部安装完成

3. 可能遇到的问题

问题1

编译Caffe的时候出现问题"ld cannot find lboost_python3"

这个时候应该创建一个libboost_python-py36.so的符号链接。

具体做法为"cannot find -lboost_python3" when using Python3 Ubuntu16.04:

cd /usr/lib/x86_64-linux-gnu
sudo ln -s libboost_python-py36.so libboost_python3.so

问题2

在编译make test的时候显示:"libopencv_core.so.3.3: cannot open shared object: …"

修改在/etc/ld.so.conf为:

include /etc/ld.so.conf.d/*.conf
include /usr/local/lib

问题3

安装caffe遇到 Makefile:635: recipe for target '.build_release/tools/compute_image_mean.bin' failed

将Makefile.config修改为(108行):

# 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 :=
LIBRARIES += glog gflags protobuf leveldb snappy \
       lmdb boost_system hdf5_hl hdf5 m \
       opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

问题4

安装Caffe遇到 undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE

原因是boost_python的版本不匹配

先在系统的/usr/lib/x86_64-linux-gnu/路径下查找boost_python文件的版本是否与python版本匹配,我的电脑中文件名为:libboost_python3-py36.so.1.65.1 说明版本为python3.6,匹配。若不匹配则安装对应的boost_python版本。

然后修改Makefile.config(80行)

# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python-py36

接着重新编译文件!

你可能感兴趣的:(Ubuntu-Caffe安装记录)