Ubuntu16.04 安装caffe(cpu版本,python3.5)

目录

  • Step 1: 安装各种依赖库
  • Step 2: 复制Makefile.config.example并命名为Makefile.config
  • Step 3: 修改Makefile.config的值
  • Step 4: 修改Makefile
  • Step 5: 编译
  • Step 6: python接口的编译

参考链接: https://www.cnblogs.com/go-better/p/7161006.html

这里默认已经装好opencv3.x版本,并编译出opencv_world.so

Step 1: 安装各种依赖库

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

Step 2: 复制Makefile.config.example并命名为Makefile.config

sudo cp Makefile.config.example Makefile.config

Step 3: 修改Makefile.config的值

#CPU_ONLY := 1 去掉#
CPU_ONLY := 1


#OPENCV_VERSION := 3 去掉#
OPENCV_VERSION := 3


# Whatever else you find you need goes here
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

改为

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 /usr/lib/x86_64-linux-gnu/hdf5/serial




# 这里Python用3.5的版本,注释掉2.7版本的Python
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
                /usr/lib/python3.5/dist-packages/numpy/core/include

Step 4: 修改Makefile

# 原来的Makefile
LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system boost_filesystem hdf5_hl hdf5 m opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

# 如果编译的opencv是只含有opencv_world的,那Makefile可改为如下
LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system boost_filesystem hdf5_hl hdf5 m opencv_world

# 底下的几行改为
ifeq ($(USE_OPENCV), 1)
    LIBRARIES += opencv_world

    ifeq ($(OPENCV_VERSION), 3)
        LIBRARIES += opencv_world
    endif

endif

Step 5: 编译

make all -j8
make test -j8
make runtest -j8

Step 6: python接口的编译

# step1 切换到 caffe/python的路径底下,根据requirements.txt安装相应的库
for req in $(cat requirements.txt); do pip3 install $req; done

# step2 将caffe根目录下的python文件夹加入到环境变量
# 打开配置文件bashrc
sudo gedit ~/.bashrc
# 在末尾添加 export PYTHONPATH=/home/caffe/python:$PYTHONPATH

# step3 更新下环境配置
sudo ldconfig

# step4 切换到caffe路径,编译python接口
make pycaffe

ps:中间可能会出现如下问题:fatal error: numpy/arrayobject.h没有那个文件或目录
解决办法: sudo apt-get install python-numpy

# step5 测试接口是否编译成功
import caffe

# import caffe可能出现的问题:cannot find -lboost_python3
解决方法:切换到/usr/lib/x86_64-linux-gnu路径下,执行以下命令:
sudo ln -s libboost_python-py35.so libboost_python3.so 

到此结束。

你可能感兴趣的:(Ubuntu16.04 安装caffe(cpu版本,python3.5))