Ubuntu 16.04安装Caffe的记录及FCN官方代码的配置

时间:2021-08-04 01:43:17

相关内容搜集自官方文档与网络,既无创新性,也不求甚解,我也不了解Caffe,仅仅搭上之后做个记录,方便以后重装

 

安装依赖项
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 libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

下载Caffe
sudo apt-get install git
git clone https://github.com/BVLC/caffe.git


cp Makefile.config.example Makefile.config

修改Makefile.config
找到
# CPU_ONLY := 1
取消注释,设置为CPU模式,不用CUDA

找到
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
修改为如下形式,以加入hdf5库的目录
# 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

进行编译
make all
make test
make runtest


安装Caffe的Python接口
sudo apt-get install gfortran安装scipy库的时候需要fortran编译器
进入Caffe根目录下的python目录,目录下的requirements.txt文件中有依赖项
sudo pip install -r requirements.txt

打开~/.bashrc文件,在最后面加入如下代码,以将caffe的python目录加入环境变量中
export PYTHONPATH=/home/xxx/caffe/python:$PYTHONPATH
保存退出,更新配置文件
sudo ldconfig

修改配置文件Makefile.config
找到
# Note: this is required only if you will compile the python interface.
按个人需要修改后面的内容的注释
找到
# Uncomment to support layers written in Python (will link against Python libs)
# WITH_PYTHON_LAYER := 1
取消注释

编译
make pycaffe

其间会报错
fatal error: numpy/arrayobject.h
需要安装python-numpy
sudo apt-get install python-numpy

然后再编译就可以了

进入python,import caffe

下载FCN的官方源码
git clone https://github.com/shelhamer/fcn.berkeleyvision.org.git
进入voc-fcn8s目录,打开caffemodel-url文件中的地址,下载已经训练好的caffemodel,保存在当前目录下
修改FCN根目录下的infer.py,根据需要修改载入图片的地址,和用到的模型的地址
在原有代码中加入如下代码,用于显示与保存图片
import matplotlib.pyplot as plt
plt.imshow(out, cmap='gray');plt.axis('off')
plt.savefig('test.png')
plt.show()


数据集
VOC2007与VOC2012,用于图像语义分割、目标检测与图像分类
http://pjreddie.com/projects/pascal-voc-dataset-mirror/

MNIST,用于数字的手写识别
http://yann.lecun.com/exdb/mnist/

CIFAR-10,用于10类图像分类
CIFAR-100,用于100类图像分类
https://www.cs.toronto.edu/~kriz/cifar.html