caffe框架下目标检测——faster-rcnn实战篇操作
原有模型
1、下载fasrer-rcnn源代码并安装
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
1) 经常断的话,可以采取两步:
git clone https://github.com/rbgirshick/py-faster-rcnn.git
2) 到py-faster-rcnn中,继续下载caffe-faster-rcnn,采取后台跑:
git submodule update --init –recursive
2、编译cython模块
在py-faster-rcnn/lib目录下,运行以下命令:
Make
3、编译caffe和pycaffe
在py-faster-rcnn/caffe-faster-rcnn下,编译
make clean
make all –j16
make test
make runtest
注意:由于py-faster-rcnn使用python层,在Makefile.config中把WITH_PYTHON_LAYER:=1
我的环境会使用
4、下载模型:
Sh data/scripts/fetch_faster_rcnn_models.sh
5、运行基于python的demo
执行以下命令:
Python tools/demo.py
6、下载训练、测试数据集
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
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
7、为了PASCAL VOC创建symlinks,创建软连接
ln -s VOCdevkit VOCdevkit2007
遇到的问题见caffe框架下目标检测——faster-rcnn实战篇问题集锦
工程目录介绍
caffe-fast-rcnn —> caffe框架
data —> 存放数据,以及读取文件的cache
experiments —>存放配置文件以及运行的log文件,配置文件
lib —> python接口
models —> 三种模型, ZF(S)/VGG1024(M)/VGG16(L)
output —> 输出的model存放的位置,不训练此文件夹没有
tools —> 训练和测试的python文件
训练模型需要修改的部分:
data —> 存放数据,以及读取文件的cache
models —> 三种模型, ZF(S)/VGG1024(M)/VGG16(L)
lib —> python接口
开始训练部分:
experiments —>存放配置文件以及运行的log文件,配置文件
用模型测试/实验结果部分:
tools —> 训练和测试的python文件
数据集
参考VOC2007的数据集格式,主要包括三个部分:
JPEGImages
Annotations
ImageSets/Main
JPEGImages —> 存放你用来训练的原始图像
Annotations —> 存放原始图像中的Object的坐标信息,XML格式
ImageSets/Main —> 指定用来train,trainval,val和test的图片的编号
JPEGImages
把图片放入,但是有三点注意:
编号要以6为数字命名,例如000034.jpg
图片要是JPEG/JPG格式的,PNG之类的需要自己转换下
图片的长宽比(width/height)要在0.462-6.828之间,就是太过瘦长的图片不要
0.462-6.828,总之长宽比太大或者太小的,注意将其剔除,否则可能会出现下面实验错误:
Traceback (most recent call last):
File “/usr/lib/python2.7/multiprocessing/process.py”, line 258, in _bootstrap
self.run()
File “/usr/lib/python2.7/multiprocessing/process.py”, line 114, in run
self._target(*self._args, **self._kwargs)
File “./tools/train_faster_rcnn_alt_opt.py”, line 130, in train_rpn
max_iters=max_iters)
File “/home/work-station/zx/py-faster-rcnn/tools/../lib/fast_rcnn/train.py”, line 160, in train_net
model_paths = sw.train_model(max_iters)
File “/home/work-station/zx/py-faster-rcnn/tools/../lib/fast_rcnn/train.py”, line 101, in train_model
self.solver.step(1)
File “/home/work-station/zx/py-faster-rcnn/tools/../lib/rpn/anchor_target_layer.py”, line 137, in forward
gt_argmax_overlaps = overlaps.argmax(axis=0)
ValueError: attempt to get argmax of an empty sequence
Google给出的原因是 Because the ratio of images width and heights is too small or large,这个非常重要
Annotations
faster rcnn训练需要图像的bounding box信息作为监督(ground truth),所以你需要将你的所有可能的object使用框标注,并写上坐标,最终是一个XML格式的文件,一个训练图片对应Annotations下的一个同名的XML文件
参考官方VOC的Annotations的格式:
<annotation>
<folder>VOC2007</folder>#数据集文件夹
<filename>009963.jpg</filename>#图片的name
<source>#注释信息,无所谓有无
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
<flickrid>65163277</flickrid>
</source>
<owner>#注释信息,无所谓有无
<flickrid>Jez_P</flickrid>
<name>Jeremy Pick</name>
</owner>
<size>#图片大小
<width>374</width>
<height>500</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>#多少个框就有多少个object标签
<name>car</name>#bounding box中的object的class name
<pose>Frontal</pose>
<truncated>1</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>2</xmin>#框的坐标
<ymin>3</ymin>
<xmax>374</xmax>
<ymax>500</ymax>
</bndbox>
</object>
</annotation>
这里有一个非常好用的工具VOC框图工具,可以自动帮你生成需要的XML格式,实际中发现格式基本无误,只有小的地方需要改动下
https://github.com/tzutalin/labelImg
Imagesets/Main
因为VOC的数据集可以做很多的CV任务,比如Object detection, Semantic segementation, Edge detection等,所以Imageset下有几个子文件夹(Layout, Main, Segementation),修改下Main下的文件 (train.txt, trainval.txt, val.txt, test.txt),里面写上你想要进行任务的图片的编号
将上述你的数据集放在py-faster-rcnn/data/VOCdevkit2007/VOC2007下面,替换原始VOC2007的JPEGIMages,Imagesets,Annotations
代码修改
修改源文件
faster rcnn有两种各种训练方式:
Alternative training(alt-opt)
Approximate joint training(end-to-end)
推荐使用第二种,因为第二种使用的显存更小,而且训练会更快,同时准确率差不多,两种方式需要修改的代码是不一样的,同时faster rcnn提供了三种训练模型,小型的ZFmodel,中型的VGG_CNN_M_1024和大型的VGG16,论文中说VGG16效果比其他两个好,但是同时占用更大的GPU显存(~11GB)
我使用的是VGG16 model + alternative training,需要检测的类别四类,加上背景所以总共是五类
1 、py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_alt_opt/stage1_fast_rcnn_train.pt
layer {
name: 'data'
type: 'Python'
top: 'data'
top: 'rois'
top: 'labels'
top: 'bbox_targets'
top: 'bbox_inside_weights'
top: 'bbox_outside_weights'
python_param {
module: 'roi_data_layer.layer'
layer: 'RoIDataLayer'
param_str: "'num_classes': 5" #按训练集类别改,该值为类别数+1
}
}
layer {
name: "cls_score"
type: "InnerProduct"
bottom: "fc7"
top: "cls_score"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
inner_product_param {
num_output: 5 #按训练集类别改,该值为类别数+1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "bbox_pred"
type: "InnerProduct"
bottom: "fc7"
top: "bbox_pred"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
inner_product_param {
num_output: 20 #按训练集类别改,该值为(类别数+1)*4,四个顶点坐标
weight_filler {
type: "gaussian"
std: 0.001
}
bias_filler {
type: "constant"
value: 0
}
}
}
2 、py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_alt_opt/stage1_rpn_train.pt
layer {
name: 'input-data'
type: 'Python'
top: 'data'
top: 'im_info'
top: 'gt_boxes'
python_param {
module: 'roi_data_layer.layer'
layer: 'RoIDataLayer'
param_str: "'num_classes': 5" #按训练集类别改,该值为类别数+1
}
}
3、 py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_alt_opt/stage2_fast_rcnn_train.pt
layer {
name: 'data'
type: 'Python'
top: 'data'
top: 'rois'
top: 'labels'
top: 'bbox_targets'
top: 'bbox_inside_weights'
top: 'bbox_outside_weights'
python_param {
module: 'roi_data_layer.layer'
layer: 'RoIDataLayer'
param_str: "'num_classes': 5" #按训练集类别改,该值为类别数+1
}
}
layer {
name: "cls_score"
type: "InnerProduct"
bottom: "fc7"
top: "cls_score"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
inner_product_param {
num_output: 5 #按训练集类别改,该值为类别数+1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "bbox_pred"
type: "InnerProduct"
bottom: "fc7"
top: "bbox_pred"
param {
lr_mult: 1.0
}
param {
lr_mult: 2.0
}
inner_product_param {
num_output: 20 #按训练集类别改,该值为(类别数+1)*4,四个顶点坐标
weight_filler {
type: "gaussian"
std: 0.001
}
bias_filler {
type: "constant"
value: 0
}
}
}
4 、py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_alt_opt/stage2_rpn_train.pt
layer {
name: 'input-data'
type: 'Python'
top: 'data'
top: 'im_info'
top: 'gt_boxes'
python_param {
module: 'roi_data_layer.layer'
layer: 'RoIDataLayer'
param_str: "'num_classes': 5" #按训练集类别改,该值为类别数+1
}
}
5 、py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_alt_opt/faster_rcnn_test.pt
layer {
name: "cls_score"
type: "InnerProduct"
bottom: "fc7"
top: "cls_score"
inner_product_param {
num_output: 5#按训练集类别改,该值为类别数+1
}
}
layer {
name: "bbox_pred"
type: "InnerProduct"
bottom: "fc7"
top: "bbox_pred"
inner_product_param {
num_output: 20#按训练集类别改,该值为(类别数+1)*4
}
}
6、 py-faster-rcnn/lib/datasets/pascal_voc.py
class pascal_voc(imdb):
def __init__(self, image_set, year, devkit_path=None):
imdb.__init__(self, 'voc_' + year + '_' + image_set)
self._year = year
self._image_set = image_set
self._devkit_path = self._get_default_path() if devkit_path is None \
else devkit_path
self._data_path = os.path.join(self._devkit_path, 'VOC' + self._year)
self._classes = ('__background__', # always index 0
captcha' # 有几个类别此处就写几个,我是两个
)
line 212
cls = self._class_to_ind[obj.find('name').text.lower().strip()]
如果你的标签含有大写字母,可能会出现KeyError的错误,所以建议全部使用小写字母
到此代码修改就搞定了
训练
训练前还需要注意几个地方
1 cache问题
假如你之前训练了官方的VOC2007的数据集或其他的数据集,是会产生cache的问题的,建议在重新训练新的数据之前将其删除
(1) py-faster-rcnn/output
(2) py-faster-rcnn/data/cache
2 训练参数
py-faster-rcnn/models/pascal_voc/VGG16/faster_rcnn_alt_opt/stage_fast_rcnn_solver*.pt
base_lr: 0.001
lr_policy: 'step'
step_size: 30000
display: 20
....
迭代次数在文件py-faster-rcnn/tools/train_faster_rcnn_alt_opt.py中进行修改
line 80
max_iters = [80000, 40000, 80000, 40000]
分别对应rpn第1阶段,fast rcnn第1阶段,rpn第2阶段,fast rcnn第2阶段的迭代次数,自己修改即可,不过注意这里的值不要小于上面的solver里面的step_size的大小,大家自己修改吧
开始训练:
cd py-faster-rcnn
./experiments/scripts/faster_rcnn_alt_opt.sh 0 VGG16 pascal_voc
指明使用第一块GPU(0),模型是VGG16,训练数据是pascal_voc(voc2007),没问题的话应该可以迭代训练了
结果
训练完毕,得到我们的训练模型,我们就可以使用它来进行我们的object detection了,具体是:
1 将py-faster-rcnn/output/faster_rcnn_alt_opt/voc_2007_trainval/VGG16_faster_rcnn_final.caffemodel,拷贝到py-faster-rcnn/data/faster_rcnn_models下
2 将你需要进行test的images放在py-faster-rcnn/data/demo下
3 修改py-faster-rcnn/tools/demo.py文件
CLASSES = ('_background_', 'captcha') #参考你自己的类别写
NETS = {'vgg16': ('VGG16',
'VGG16_faster_rcnn_final.caffemodel'), #改成你训练得到的model的name
'zf': ('ZF',
'ZF_faster_rcnn_final.caffemodel')
}
im_names = ['1559.jpg','1564.jpg'] # 改成自己的test image的name
上几张我的检测结果吧
caffe框架下目标检测——faster-rcnn实战篇操作的更多相关文章
- caffe框架下目标检测——faster-rcnn实战篇问题集锦
1.问题 解决方案:没编译好,需要在lib下编译make 需要在caffe-fast-rcnn下编译make或者make all -j16 ,还需要make pycaffe 2.问题 解决方案:/p ...
- 目标检测-Faster R-CNN
[目标检测]Faster RCNN算法详解 Ren, Shaoqing, et al. “Faster R-CNN: Towards real-time object detection with r ...
- 目标检测faster rcnn error == cudaSuccess (2 vs. 0) out of memory
想尝试 更深更强的网络,或者自己写了一个费显存的层,发现1080 ti的11G显存不够用了,老师报显存不够怎么办? Check failed: error == cudaSuccess (2 vs. ...
- 目标检测--之RCNN
目标检测--之RCNN 前言,最近接触到的一个项目要用到目标检测,还有我的科研方向caption,都用到这个,最近电脑在windows下下载数据集,估计要一两天,也不能切换到ubuntu下撸代码~.所 ...
- 使用Caffe完成图像目标检测 和 caffe 全卷积网络
一.[用Python学习Caffe]2. 使用Caffe完成图像目标检测 标签: pythoncaffe深度学习目标检测ssd 2017-06-22 22:08 207人阅读 评论(0) 收藏 举报 ...
- Caffe框架下的图像回归测试
Caffe框架下的图像回归测试 参考资料: 1. http://stackoverflow.com/questions/33766689/caffe-hdf5-pre-processing 2. ht ...
- 目标检测系列 --- RCNN: Rich feature hierarchies for accurate object detection and semantic segmentation Tech report
目标检测系列 --- RCNN: Rich feature hierarchies for accurate object detection and semantic segmentation Te ...
- 基于候选区域的深度学习目标检测算法R-CNN,Fast R-CNN,Faster R-CNN
参考文献 [1]Rich feature hierarchies for accurate object detection and semantic segmentation [2]Fast R-C ...
- [目标检测] 从 R-CNN 到 Faster R-CNN
R-CNN 创新点 经典的目标检测算法使用滑动窗法依次判断所有可能的区域,提取人工设定的特征(HOG,SIFT).本文则预先提取一系列较可能是物体的候选区域,之后仅在这些候选区域上用深度网络提取特征, ...
随机推荐
- 20155327 嵌入式C语言课堂补交
嵌入式C语言 题目要求 在作业本上完成附图作业,要认真看题目要求. 提交作业截图 作弊本学期成绩清零(有雷同的,不管是给别人传答案,还是找别人要答案都清零) 题目分析 分析一:提取插入时间 根据老师上 ...
- 20155334 实验四:Android程序设计
20155334实验四:Android程序设计 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件 ...
- 微信小程序居中代码
html页面: { text-align:center; } wxss页面: { width: 100%; height: 100%; display: flex; justify-content: ...
- Matplotlib API汉化
Pyplot API 示例汇总:https://matplotlib.org/gallery/index.html#api-examples 该matplotlib.pyplot模块包含的功能允许您快 ...
- JavaWeb(十三)——使用Session防止表单重复提交
在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...
- perf + 火焰图用法 小结
要对新服务做性能测试,分析代码热点,初识perf,做下总结 perf + 火焰图用法 perf简介 Perf (Performance Event), Linux 系统原生提供的性能分析工具, 会返回 ...
- Android手机测试-ddms&monitor-抓crash,log
1.安装adb offline解决办法: 原因就是android 4.2以上的版本过高,sdk的adb驱动不匹配,需要升级.我原本的adb是1.0.29,升级为1.0.31,问题就解决了. 2.安装s ...
- selenium,unittest——下拉菜单操作,百度账号设置修改
#encoding=utf-8from selenium import webdriverimport time,unittest, re,sysfrom HTMLTestRunner import ...
- 【SpringCloud】第十一篇: 断路器监控(Hystrix Dashboard)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- 4.1 所有类型都从 System.Object 派生
"运行时"要求各个类型最终都从 System.Object 派生.(显示继承/隐式继承) 提供公共方法(public): Equals 判断两个对象相等,true 表示相等. Ge ...