RCNN

一种把目标图像分割转化为CNN分类问题进行目标检测的方法。
 
Ross B. Girshick的RCNN使用region proposal(具体用的是Selective Search Koen van de Sande: Segmentation as Selective Search for Object Recognition)来得到有可能得到是object的若干(大概10^3量级)图像局部区域,然后把这些区域分别输入到CNN中,得到区域的feature,再在feature上加上分类器,判断feature对应的区域是属于具体某类object还是背景。当然,RBG还用了区域对应的feature做了针对boundingbox的回归,用来修正预测的boundingbox的位置。RCNN在VOC2007上的mAP是58%左右。

RCNN存在着重复计算的问题(proposal的region有几千个,多数都是互相重叠,重叠部分会被多次重复提取feature),于是RBG借鉴Kaiming He的SPP-net的思路单枪匹马搞出了Fast-RCNN,跟RCNN最大区别就是Fast-RCNN将proposal的region映射到CNN的最后一层conv layer的feature map上,这样一张图片只需要提取一次feature,大大提高了速度,也由于流程的整合以及其他原因,在VOC2007上的mAP也提高到了68%。

探索是无止境的。Fast-RCNN的速度瓶颈在Region proposal上,于是RBG和Kaiming He一帮人将Region proposal也交给CNN来做,提出了Faster-RCNN。Fater-RCNN中的region proposal netwrok实质是一个Fast-RCNN,这个Fast-RCNN输入的region proposal的是固定的(把一张图片划分成n*n个区域,每个区域给出9个不同ratio和scale的proposal),输出的是对输入的固定proposal是属于背景还是前景的判断和对齐位置的修正(regression)。Region proposal network的输出再输入第二个Fast-RCNN做更精细的分类和Boundingbox的位置修正。Fater-RCNN速度更快了,而且用VGG net作为feature extractor时在VOC2007上mAP能到73%。

个人觉得制约RCNN框架内的方法精度提升的瓶颈是将dectection问题转化成了对图片局部区域的分类问题后,不能充分利用图片局部object在整个图片中的context信息。可能RBG也意识到了这一点,所以他最新的一篇文章YOLOhttp://arxiv.org/abs/1506.02640)又回到了regression的方法下,这个方法效果很好,在VOC2007上mAP能到63.4%,而且速度非常快,能达到对视频的实时处理(油管视频:https://www.youtube.com/channel/UC7ev3hNVkx4DzZ3LO19oebg),虽然不如Fast-RCNN,但是比传统的实时方法精度提升了太多,而且我觉得还有提升空间。

安装

1.下载https://github.com/sergeyk/selective_search_ijcv_with_python运行demo编译必要的函数,
复制到<python>\Lib\site-packages\中重命名为selective_search_ijcv_with_python
2.下载ImageNet的RCNN的Caffe模型bvlc_reference_rcnn_ilsvrc13.caffemodel和deploy.prototxt
放到<caffe>\models\bvlc_reference_rcnn_ilsvrc13文件夹
3.将<caffe>\examples\images\fish-bike.jpg复制到<caffe>\Build\x64\Release\pycaffe
4.将caffe_ilsvrc12.tar.gz解压到caffe-windows\data\ilsvrc12
5.将ilsvrc_2012_mean.npy复制到caffe-windows\Build\x64\Release\pycaffe\caffe\imagenet
4.将caffe_ilsvrc12.tar.gz解压到caffe-windows\data\ilsvrc12
5.Cmd到<caffe>\Build\x64\Release\pycaffe目录运行下面代码(det_input.txt 为pycaffe中需要检测的图片名)

python detect.py
--crop_mode=selective_search
--pretrained_model=改成你自己的路径\models\bvlc_reference_rcnn_ilsvrc13\bvlc_reference_rcnn_ilsvrc13.caffemodel
--model_def=改成你自己的路径\models\bvlc_reference_rcnn_ilsvrc13\deploy.prototxt
--gpu
--raw_scale=
C:\Users\改成你自己\Desktop\det_input.txt
C:\Users\改成你自己\Desktop\det_output.h5

6.如果出现错误ValueError: 'axis' entry 2 is out of bounds [-2, 2)
依据https://github.com/BVLC/caffe/issues/2041
将<caffe>\Build\x64\Release\pycaffe\caffe\detector.py第86行的 out[self.outputs[0]].squeeze(axis=(2,3))
修改为 out[self.outputs[0]].squeeze()

7.运行下面python代码获取检测结果

 import numpy as np
import pandas as pd
import matplotlib.pyplot as plt df = pd.read_hdf('det_output.h5', 'df')
print(df.shape)
print(df.iloc[0])
with open('a\caffe-windows\data\ilsvrc12\det_synset_words.txt') as f:
labels_df = pd.DataFrame([
{
'synset_id': l.strip().split(' ')[0],
'name': ' '.join(l.strip().split(' ')[1:]).split(',')[0]
}
for l in f.readlines()
])
labels_df.sort_values(by='synset_id')# sort('synset_id')
predictions_df = pd.DataFrame(np.vstack(df.prediction.values), columns=labels_df['name'])
print(predictions_df.iloc[0]) max_s = predictions_df.max(0)
max_s = max_s.sort_values(ascending=False) # Find, print, and display the top detections: person and bicycle.
i = predictions_df[max_s.keys()[0]].argmax() #person
j = predictions_df[max_s.keys()[1]].argmax() #bicycle # Show top predictions for top detection.
f = pd.Series(df['prediction'].iloc[i], index=labels_df['name'])
print('Top detection:')
print(f.sort_values(ascending=False)[:5]) # Show top predictions for second-best detection.
f = pd.Series(df['prediction'].iloc[j], index=labels_df['name'])
print('Second-best detection:')
print(f.sort_values(ascending=False)[:5]) # Show top detection in red, second-best top detection in blue.
im = plt.imread('a\caffe-windows\examples\images\\fish-bike.jpg')
plt.imshow(im)
currentAxis = plt.gca() det = df.iloc[i]
coords = (det['xmin'], det['ymin']), det['xmax'] - det['xmin'], det['ymax'] - det['ymin']
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor='r', linewidth=5)) det = df.iloc[j]
coords = (det['xmin'], det['ymin']), det['xmax'] - det['xmin'], det['ymax'] - det['ymin']
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor='b', linewidth=5))
plt.show()

输出结果

Top detection:
name
person 1.835771
swimming trunks -1.150371
rubber eraser -1.231106
turtle -1.266038
plastic bag -1.303266
dtype: float32 Second-best detection:
name
bicycle 0.866110
unicycle -0.359140
scorpion -0.811621
lobster -0.982891
lamp -1.096809
dtype: float32
 

最新文章

  1. javascript除法如何取整
  2. ThinkPHP3.2.2 Widget扩展以及widget demo实例
  3. MySql数据备份与恢复小结
  4. zendstudio 出现failed to create the java machine转
  5. MySQL创建复合索引
  6. 做XH2.54杜邦线材料-导线
  7. 浅谈用java解析xml文档(三)
  8. &lt;三&gt; jQuery 选择器
  9. HW2.9
  10. maven项目转换成dynamic项目
  11. hibernate相关知识
  12. react-redux源码解析
  13. 在iPhoneApp中加载PDF
  14. Ubuntu创建、删除文件与目录
  15. 51NOD 1821 最优集合 [并查集]
  16. _trigger
  17. OpenGL.Tutorial03_Matrices_测试
  18. Synchronized的几种用法
  19. [2018.05].NET Core 3 and Support for Windows Desktop Applications
  20. php省市联动实现

热门文章

  1. MFC编程入门之十一(对话框:模态对话框及其弹出过程)
  2. Go文件操作
  3. 项目打包文件build.xml
  4. 【Jenkins】Windows下安装&amp;访问jenkins
  5. 使用ajax异步提交表单数据(史上最完整的版本)
  6. Linux (Ubuntu) 下配置VPN服务器
  7. juery常用
  8. VC只运行一个程序实例
  9. 你会用Python做出装逼的东西吗
  10. 系统不支持curl