之前用Fast_rcnn 的例子训练自己的数据集,但是由于Fast_rcnn 例子中用的是预先处理好的图片文件(就是吧图片先用selective-search 把图片预先处理成matlab的.mat文件)。所以再用Fast_rcnn 训练自己的数据图图片时,也要把自己的图片先预处理。终于把数据弄的一致了。但是网络又不收敛。所以改用Faster_rcnn。 相比Fast_rcnn 好用的多。
一 首先下载Faster_rcnn 项目
1)项目地址 https://github.com/rbgirshick/py-faster-rcnn 在你想放置的文件夹下面执行:
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
一定要有--recursive 否则不会下载caffe 部分
2)进入Faster_rcnn 根目录下
cd $FRCN_ROOT/lib
make
编译的过程有可能有gcc版本的错误。我在之前编译别的软件的时候将gcc版本降低了,编译不通过。把gcc和 g++ 版本调高后编译顺利通过。
3)编译Caffe and pyCaffe。 这里的caffe并不是正常的caffe,也不需要自己下载。系统需要安装caffe 的依赖环境 ,具体参考 caffe 官方文档,在编译之前需要修改Makefile文件。
cd $FRCN_ROOT/caffe-fast-rcnn
cp Makefile.config.example Makefile.config
修改如下两处
# In your Makefile.config, make sure to have this line uncommented
WITH_PYTHON_LAYER := 1
# Unrelatedly, it's also recommended that you use CUDNN
USE_CUDNN := 1
如果编译时提示找不到hdf5,则需要添加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 /usr/lib/x86_64-linux-gnu/hdf5/serial
配置好后开始编译:
make -j8 && make pycaffe
4)编译完成后下载预训练的模型
cd $FRCN_ROOT
./data/scripts/fetch_faster_rcnn_models.sh
有可能需要翻墙下载。总之把模型下好。跑一下demo
cd $FRCN_ROOT
./tools/demo.py
没错误的话应该看到几张图片的识别实例(要是ssh 登录服务器会提示没有显示设备)
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
一定要有--recursive 否则不会下载caffe 部分
2)进入Faster_rcnn 根目录下
cd $FRCN_ROOT/lib
make
编译的过程有可能有gcc版本的错误。我在之前编译别的软件的时候将gcc版本降低了,编译不通过。把gcc和 g++ 版本调高后编译顺利通过。
3)编译Caffe and pyCaffe。 这里的caffe并不是正常的caffe,也不需要自己下载。系统需要安装caffe 的依赖环境 ,具体参考 caffe 官方文档,在编译之前需要修改Makefile文件。
cd $FRCN_ROOT/caffe-fast-rcnn
cp Makefile.config.example Makefile.config
修改如下两处
# In your Makefile.config, make sure to have this line uncommented
WITH_PYTHON_LAYER := 1
# Unrelatedly, it's also recommended that you use CUDNN
USE_CUDNN := 1
如果编译时提示找不到hdf5,则需要添加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 /usr/lib/x86_64-linux-gnu/hdf5/serial
配置好后开始编译:
make -j8 && make pycaffe
4)编译完成后下载预训练的模型
cd $FRCN_ROOT
./data/scripts/fetch_faster_rcnn_models.sh
有可能需要翻墙下载。总之把模型下好。跑一下demo
cd $FRCN_ROOT
./tools/demo.py
没错误的话应该看到几张图片的识别实例(要是ssh 登录服务器会提示没有显示设备)
二 训练自己的数据
训练自己的图片数据需要将自己的图片变为VOC格式。可以按照github上的文档,下载pascalVOC数据集,然后将文件都删掉,用自己的数据替换。我这里没这样做,下载太慢了。 因为之前弄过Fast_rcnn,目录结构直接自己建立就行了。先说一下目录组织结构
$FRCN_ROOT--|
|-data-|
|-VOCdevkit2007-|
|-VOC2007-|
|-Annotations- (存放图片的.xml)
|-JPEGImages-(存放图片本身)
|-ImageSets-|
|-Main-(存放 train.txt test.txt trainval.txt val.txt)
|-data-|
|-VOCdevkit2007-|
|-VOC2007-|
|-Annotations- (存放图片的.xml)
|-JPEGImages-(存放图片本身)
|-ImageSets-|
|-Main-(存放 train.txt test.txt trainval.txt val.txt)
1) 将图片中的目标区域提取出来变为xml文件,每张图片一个xml文件。 我用的是Python-Based-Labeling-Tool-for-PASCAL-VOC-master 这个工具,也可以在github 上有个py-faster-rcnn-data-interface-generator 这个工具或者自己写个小脚本。至于工具怎么用自行百度。
2) Main 文件夹下的四个文件由python 脚本生成createdata.py
2) Main 文件夹下的四个文件由python 脚本生成createdata.py
#!/usr/bin/env python
import numpy as np
import os
import random
""" create_imageset.py
Create .txt files containing the names of the imageset for training, validation and testing for the pascal_voc benchmark.
"""
DATASET_PATH = '$Faster_rcnn/data/VOC2007'
test = []
train = []
val = []
trainval = []
#test_probality = 0.2
#train_probablity = 0.5
test_probability = 0.2
train_probability = 0.8
def save_imagesets(imageset_path):
with open(os.path.join(imageset_path, "test.txt"), "w") as test_file:
test_file.write('\n'.join(i for i in test))
with open(os.path.join(imageset_path, "train.txt"), "w") as train_file:
train_file.write('\n'.join(i for i in train))
with open(os.path.join(imageset_path, "val.txt"), "w") as val_file:
val_file.write('\n'.join(i for i in val))
with open(os.path.join(imageset_path, "trainval.txt"), "w") as trainval_f:
trainval_f.write('\n'.join(i for i in trainval))
f __name__ == '__main__':
# get all files that have an existing annotation
annotation_path = os.path.join(DATASET_PATH, 'Annotations')
imageset_path = os.path.join(DATASET_PATH, 'ImageSets', 'Main')
files = [f for f in os.listdir(annotation_path)]
files.sort()
for f in files:
# strip extenstion
short_name = os.path.splitext(f)[0]
# decide whether its testing (p=0.5) or trainval(p=0.5)
if random.random() < test_probability :
test.append(short_name)
else:
trainval.append(short_name)
# train (p=0.5) or val (p=0.5)
if random.random() < train_probability:
train.append(short_name)
else:
val.append(short_name)
print("ImageSets saved")
save_imagesets(imageset_path)
3) 将自己的图片和文件放到对应的文件夹下面
4) 下载ImageNet数据集下预训练得到的模型参数(用来初始化)
5) 修改网络参数,这里用的是ZF网络,修改/models/faster_rcnn_alt_opt配置文件
配置文件的修改可以参考下面这篇博客