ROI Pooling原理及实现

目标检测architecture通常可以分为两个阶段:
(1)region proposal:给定一张输入image找出objects可能存在的所有位置。这一阶段的输出应该是一系列object可能位置的bounding box。这些通常称之为region proposals或者 regions of interest(ROI),在这一过程中用到的方法是基于滑窗的方式和selective search
(2)final classification:确定上一阶段的每个region proposal是否属于目标一类或者背景。
这里写图片描述
这个architecture存在的一些问题是:

  • 产生大量的region proposals 会导致performance problems,很难达到实时目标检测。
  • 在处理速度方面是suboptimal。
  • 无法做到end-to-end training。

这就是ROI pooling提出的根本原因,ROI pooling层能实现training和testing的显著加速,并提高检测accuracy。该层有两个输入:

  1. 从具有多个卷积核池化的深度网络中获得的固定大小的feature maps;
  2. 一个表示所有ROI的N*5的矩阵,其中N表示ROI的数目。第一列表示图像index,其余四列表示其余的左上角和右下角坐标;

ROI pooling具体操作如下:

  1. 根据输入image,将ROI映射到feature map对应位置;
  2. 将映射后的区域划分为相同大小的sections(sections数量与输出的维度相同);
  3. 对每个sections进行max pooling操作;

这样我们就可以从不同大小的方框得到固定大小的相应 的feature maps。值得一提的是,输出的feature maps的大小不取决于ROI和卷积feature maps大小。ROI pooling 最大的好处就在于极大地提高了处理速度。

ROI pooling example
我们有一个8*8大小的feature map,一个ROI,以及输出大小为2*2.

  1. 输入的固定大小的feature map
    这里写图片描述
  2. region proposal 投影之后位置(左上角,右下角坐标):(0,3),(7,8)。
    这里写图片描述
  3. 将其划分为(2*2)个sections(因为输出大小为2*2),我们可以得到:
    这里写图片描述
  4. 对每个section做max pooling,可以得到:
    这里写图片描述

整体过程如下:
这里写图片描述

说明:在此案例中region proposals 是5*7大小的,在pooling之后需要得到2*2的,所以在5*7的特征图划分成2*2的时候不是等分的,行是5/2,第一行得到2,剩下的那一行是3,列是7/2,第一列得到3,剩下那一列是4。

ROI Pooling 就是将大小不同的feature map 池化成大小相同的feature map,利于输出到下一层网络中。

代码实现

$ git clone git@github.com:deepsense-io/roi-pooling.git
$ cd roi-pooling
$ python setup.py install
from __future__ import print_function

import tensorflow as tf
import numpy as np

from roi_pooling.roi_pooling_ops import roi_pooling



# 4x4 feature map with only 1 channel
input_value = [[
    [[1], [2], [4], [4]],
    [[3], [4], [1], [2]],
    [[6], [2], [1], [7]],
    [[1], [3], [2], [8]]
]]
input_value = np.asarray(input_value, dtype='float32')

# regions of interest as lists of:
# feature map index, upper left, bottom right coordinates
rois_value = [
    [0, 0, 0, 1, 3],
    [0, 2, 2, 3, 3],
    [0, 1, 0, 3, 2]
]
rois_value = np.asarray(rois_value, dtype='int32')

# in this case we have 3 RoI pooling operations:
# * channel 0, rectangular region (0, 0) to (1, 3)
#              xx..
#              xx..
#              xx..
#              xx..
#
# * channel 0, rectangular region (2, 2) to (3, 3)
#              ....
#              ....
#              ..xx
#              ..xx
# * channel 0, rectangular region (1, 0) to (3, 2)
#              ....
#              xxx.
#              xxx.
#              xxx.

input_featuremap = tf.placeholder(tf.float32)
rois = tf.placeholder(tf.int32)
input_const = tf.constant(input_value, tf.float32)
rois_const = tf.constant(rois_value, tf.int32)
y = roi_pooling(input_const, rois_const, pool_height=2, pool_width=2)

with tf.Session('') as sess:
    y_output = sess.run(y, feed_dict={input_featuremap: input_value, rois: rois_value})
    print(y_output)   

输出

[[[[ 3.  4.]
   [ 6.  3.]]]


 [[[ 1.  7.]
   [ 2.  8.]]]


 [[[ 4.  4.]
   [ 4.  7.]]]] 
  • 176
    点赞
  • 598
    收藏
    觉得还不错? 一键收藏
  • 40
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 40
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值