R-FCN + ResNet + Python + 训练自己的模型

  • 安装MS Caffe
  • 测试Demo
  • 训练自己的模型的准备工作
  • 训练
  • 参考链接

安装MS Caffe

假设你已经装好了CUDA,cudnn,caffe等等。 这一步感觉和faster rcnn很像,都需要独特版本的caffe。这里的版本就是Microsoft的caffe。

pip install cython pip install easydict apt-get install
python-opencv
git clone https://github.com/Orpine/py-R-FCN.git
cd py-R-FCN

进入lib文件夹,然后:

make

接着进入caffe目录:(32核用起来果然很爽)

make -j32 && make pycaffe

这里的Makefile.config需要注意一下,每个人的都有一点点不一样,比如我就没有加入opencv。这里没写好的话就会有不少错误。

## 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

# 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.

# 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

# 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

# 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/include/python2.7 \
                /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:

# 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/lib/x86_64-linux-gnu/hdf5/serial/include
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 ?= @       

测试Demo

下载已经训练好的模型。
点击这里: https://1drv.ms/u/s!AoN7vygOjLIQqUWHpY67oaC7mopf

然后下载之后放进去:

$py-R-FCN/data/rfcn_models/resnet50_rfcn_final.caffemodel

$py-R-FCN/data/rfcn_models/resnet101_rfcn_final.caffemodel

运行:

./tools/demo_rfcn.py –net ResNet-50

效果就是:
R-FCN + ResNet + Python + 训练自己的模型_第1张图片

R-FCN + ResNet + Python + 训练自己的模型_第2张图片

训练自己的模型的准备工作

在data目录下,下载VOC的数据集。
1、下载

wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar

2、解压:

tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
tar xvf VOCtrainval_11-May-2012.tar

3、整理:
把下面的情况:

$VOCdevkit/                           # development kit
$VOCdevkit/VOCcode/                   # VOC utility code
$VOCdevkit/VOC2007                    # image sets, annotations, etc.
$VOCdevkit/VOC2012                    # image sets, annotations, etc.

弄成: 多一个0712

$VOCdevkit/                           # development kit
  $VOCdevkit/VOCcode/                   # VOC utility code
  $VOCdevkit/VOC2007                    # image sets, annotations, etc.
  $VOCdevkit/VOC2012                    # image sets, annotations, etc.
  $VOCdevkit/VOC0712                    # you just created this folder

PS:在这里需要注意把训练的train.sh里面的TRAIN_IMDB改成如下形式。

case $DATASET in  
  pascal_voc)  
    TRAIN_IMDB="voc_0712_trainval"  
    TEST_IMDB="voc_0712_test"  
    PT_DIR="pascal_voc"  
    ITERS=110000  

4、链接:

cd $RFCN_ROOT/data
ln -s $VOCdevkit VOCdevkit0712

5、下载事先训练好的imageNet的rest50,100的模型。

ImageNet-pre-trained ResNet-50 and ResNet-100 model 

下载地址:链接:http://pan.baidu.com/s/1slRHD0L 密码:r3ki

下载之后放在:/data/imagenet_models目录下。

训练

需要修改网络接口,我的输出是4类,那么在需要改不少东西。具体修改参考: http://blog.csdn.net/sinat_30071459/article/details/53202977
http://blog.csdn.net/xzzppp/article/details/52036794
这篇博客已经写得非常详细。

接着就使用 ./experiments/scripts/rfcn_end2end_ohem.sh 0 ResNet-50 pascal_voc 训练。

$RFCN_ROOT/experiments/scripts里还有一些其他的训练方法,也可以测试一下(经过上面的修改,无ohem的end2end训练也改好了,其他训练方法修改的过程差不多)。

将训练得到的模型

($RFCN_ROOT/output/rfcn_end2end_ohem/voc_0712_trainval

里最后的caffemodel)拷贝到

$RFCN_ROOT/data/rfcn_models

下,然后打开

$RFCN_ROOT/tools/demo_rfcn.py

,将CLASSES修改成你的标签,NETS修改成你的model,im_names修改成你的测试图片(放在data/demo下),最后:

./tools/demo_rfcn.py –net ResNet-50

看一下最后的结果:
R-FCN + ResNet + Python + 训练自己的模型_第3张图片

R-FCN + ResNet + Python + 训练自己的模型_第4张图片

参考链接

1、http://blog.csdn.net/sinat_30071459/article/details/53202977
2、https://github.com/Orpine/py-R-FCN

你可能感兴趣的:(CMU:,Computer,Vision,Computer,Vision)