rpn

它的示意图如下: 这里借用了 http://blog.csdn.net/zy1034092330/article/details/62044941里的图。

上面Conv layers包含了五层卷积层。 接下来,对于第五层卷积层,进行了3*3的卷积操作,输出了256个通道,当然大小与卷积前的大小相同。

然后开始分别接入了cls层与regression层。对于cls层,使用1*1的卷积操作输出了18(9*2 bg/fg)个通道的feature map,大小不变。而对于regression层,也使用1*1的卷积层输出了36(4*9)个通道的feature map,大小不变。 

对于cls层后又接了一个reshape层,为什么要接这个层呢?引用参考文献[1]的话,其实只是为了便于softmax分类,至于具体原因这就要从caffe的实现形式说起了。在caffe基本数据结构blob中以如下形式保存数据:
blob=[batch_size, channel,height,width]
对应至上面的保存bg/fg anchors的矩阵,其在caffe blob中的存储形式为[1, 2*9, H, W]。而在softmax分类时需要进行fg/bg二分类,所以reshape layer会将其变为[1, 2, 9*H, W]大小,即单独“腾空”出来一个维度以便softmax分类,之后再reshape回复原状。

我们可以用python模拟一下,看如下的程序:

>>> a=np.array([[[1,2],[3,4]],[[5,6],[7,8]],[[9,10],[11,12]],[[13,14],[15,16]]])
>>> a
array([[[ 1,  2],
        [ 3,  4]],

       [[ 5,  6],
        [ 7,  8]],

       [[ 9, 10],
        [11, 12]],

       [[13, 14],
        [15, 16]]])
>>> a.shape
(4L, 2L, 2L)
然后由于caffe中是行优先,numpy也如此,那么reshape一下的结果如下:
>>> b=a.reshape(2,4,2)
>>> b
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6],
        [ 7,  8]],

       [[ 9, 10],
        [11, 12],
        [13, 14],
        [15, 16]]])

从上面可以看出reshape是把相邻通道的矩阵移到它的下面了。这样就剩下两个大的矩阵了,就可以相邻通道之间进行softmax了。 从中其实我们也能发现,对于rpn每个点的18个输出通道,前9个为背景的预测分数,而后9个为前景的预测分数。

假定softmax昨晚后,我们看看是否能够回到原先?

>>> b.reshape(4,2,2)
array([[[ 1,  2],
        [ 3,  4]],

       [[ 5,  6],
        [ 7,  8]],

       [[ 9, 10],
        [11, 12]],

       [[13, 14],
        [15, 16]]])
果然又回到了原始的状态。

而对于regression呢,不需要这样的操作,那么他的36个通道是不是也是如上面18个通道那样呢?即第一个9通道为dx,第二个为dy,第三个为dw,第五个是dh。还是我们比较容易想到的那种,即第一个通道是第一个盒子的回归量(dx1,dy1,dw1,dh1),第二个为(dx2,dy2,dw,2,dh2).....。待后面查看对应的bbox_targets就知道了。先留个坑。

正如图上所示,我们还需要准备一个层rpn-data。

layer {
  name: 'rpn-data'
  type: 'Python'
  bottom: 'rpn_cls_score'
  bottom: 'gt_boxes'
  bottom: 'im_info'
  bottom: 'data'
  top: 'rpn_labels'
  top: 'rpn_bbox_targets'
  top: 'rpn_bbox_inside_weights'
  top: 'rpn_bbox_outside_weights'
  python_param {
    module: 'rpn.anchor_target_layer'
    layer: 'AnchorTargetLayer'
    param_str: "'feat_stride': 16"
  }
}

这一层输入四个量:data,gt_boxes,im_info,rpn_cls_score,其中前三个是我们在前面说过的,

data:         1*3*600*1000
gt_boxes: N*5,                   N为groundtruth box的个数,每一行为(x1, y1, x2, y2, cls) ,而且这里的gt_box是经过缩放的。
im_info: 1*3                   (h,w,scale)

rpn_cls_score是cls层输出的18通道,shape可以看成是1*18*H*W.  

输出为4个量:rpn_labels 、rpn_bbox_targets(回归目标)、rpn_bbox_inside_weights(内权重)、rpn_bbox_outside_weights(外权重)。

通俗地来讲,这一层产生了具体的anchor坐标,并与groundtruth box进行了重叠度计算,输出了kabel与回归目标。

接下来我们来看一下文件anchor_target_layer.py 

[plain] view plain copy
  1. <code class="language-python"> def setup(self, bottom, top):  
  2.         layer_params = yaml.load(self.param_str_)      
  3.         #在第5个卷积层后的feature map上的每个点取anchor,尺度为(8,16,32),结合后面的feat_stride为16,  
  4.         #再缩放回原来的图像大小,正好尺度是(128,256,512),与paper一样。  
  5.         anchor_scales = layer_params.get('scales', (8, 16, 32))    
  6.         self._anchors = generate_anchors(scales=np.array(anchor_scales))  #产生feature map最左上角的那个点对应的anchor(x1,y1,x2,y2),  
  7.         # 尺度为原始图像的尺度(可以看成是Im_info的宽和高尺度,或者是600*1000)。  
  8.         self._num_anchors = self._anchors.shape[0]   #9  
  9.         self._feat_stride = layer_params['feat_stride'] #16  
  10.   
  11.         if DEBUG:  
  12.             print 'anchors:'  
  13.             print self._anchors  
  14.             print 'anchor shapes:'  
  15.             print np.hstack((  # 输出宽和高  
  16.                 self._anchors[:, 2::4] - self._anchors[:, 0::4], #第2列减去第0列  
  17.                 self._anchors[:, 3::4] - self._anchors[:, 1::4], #第3列减去第1列  
  18.             ))  
  19.             self._counts = cfg.EPS  
  20.             self._sums = np.zeros((1, 4))  
  21.             self._squared_sums = np.zeros((1, 4))  
  22.             self._fg_sum = 0  
  23.             self._bg_sum = 0  
  24.             self._count = 0  
  25.   
  26.         # allow boxes to sit over the edge by a small amount    
  27.         self._allowed_border = layer_params.get('allowed_border', 0)      
  28.   
  29.         height, width = bottom[0].data.shape[-2:]   #cls后的feature map的大小  
  30.         if DEBUG:  
  31.             print 'AnchorTargetLayer: height', height, 'width', width  
  32.   
  33.         A = self._num_anchors     
  34.         # labels  
  35.         top[0].reshape(1, 1, A * height, width)     # 显然与rpn_cls_score_reshape保持相同的shape.  
  36.         # bbox_targets  
  37.         top[1].reshape(1, A * 4, height, width)       
  38.         # bbox_inside_weights  
  39.         top[2].reshape(1, A * 4, height, width)  
  40.         # bbox_outside_weights  
  41.         top[3].reshape(1, A * 4, height, width)  
  42. </code>  
setup设置了top输出的shape,并且做了一些准备工作。

接下来看forward函数。

 def forward(self, bottom, top):
        # Algorithm:
        #
        # for each (H, W) location i
        #   generate 9 anchor boxes centered on cell i
        #   apply predicted bbox deltas at cell i to each of the 9 anchors
        # filter out-of-image anchors
        # measure GT overlap

        assert bottom[0].data.shape[0] == 1, \
            'Only single item batches are supported'     # 仅仅支持一张图片

        # map of shape (..., H, W)
        height, width = bottom[0].data.shape[-2:]        
        # GT boxes (x1, y1, x2, y2, label)
        gt_boxes = bottom[1].data                          
        # im_info
        im_info = bottom[2].data[0, :]

        if DEBUG:
            print ''
            print 'im_size: ({}, {})'.format(im_info[0], im_info[1])
            print 'scale: {}'.format(im_info[2])
            print 'height, width: ({}, {})'.format(height, width)
            print 'rpn: gt_boxes.shape', gt_boxes.shape
            print 'rpn: gt_boxes', gt_boxes

        # 1. Generate proposals from bbox deltas and shifted anchors
        shift_x = np.arange(0, width) * self._feat_stride  
        shift_y = np.arange(0, height) * self._feat_stride 
        shift_x, shift_y = np.meshgrid(shift_x, shift_y)
        shifts = np.vstack((shift_x.ravel(), shift_y.ravel(),
                            shift_x.ravel(), shift_y.ravel())).transpose()
        # add A anchors (1, A, 4) to
        # cell K shifts (K, 1, 4) to get
        # shift anchors (K, A, 4)
        # reshape to (K*A, 4) shifted anchors
        A = self._num_anchors
        K = shifts.shape[0]
        all_anchors = (self._anchors.reshape((1, A, 4)) +
                       shifts.reshape((1, K, 4)).transpose((1, 0, 2)))
        all_anchors = all_anchors.reshape((K * A, 4))
        total_anchors = int(K * A)                       # 根据左上角的anchor生成所有的anchor,这里将所有的anchor按照行排列。行:K*A(K= height*width ,A=9),列:4,且按照feature map按行优先这样排下来。

        # only keep anchors inside the image   #取所有在图像内部的anchor
        inds_inside = np.where(
            (all_anchors[:, 0] >= -self._allowed_border) &
            (all_anchors[:, 1] >= -self._allowed_border) &
            (all_anchors[:, 2] < im_info[1] + self._allowed_border) &  # width
            (all_anchors[:, 3] < im_info[0] + self._allowed_border)    # height
        )[0]                                   

        if DEBUG:
            print 'total_anchors', total_anchors
            print 'inds_inside', len(inds_inside)

        # keep only inside anchors
        anchors = all_anchors[inds_inside, :]
        if DEBUG:
            print 'anchors.shape', anchors.shape

        # label: 1 is positive, 0 is negative, -1 is dont care
        labels = np.empty((len(inds_inside), ), dtype=np.float32)
        labels.fill(-1)

        # overlaps between the anchors and the gt boxes
        # overlaps (ex, gt)
        overlaps = bbox_overlaps(
            np.ascontiguousarray(anchors, dtype=np.float),
            np.ascontiguousarray(gt_boxes, dtype=np.float))
        argmax_overlaps = overlaps.argmax(axis=1)   #对于每一个anchor,取其重叠度最大的ground truth的序号
        max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]   #生成max_overlaps,(为一列)即每个anchor对应的最大重叠度
        gt_argmax_overlaps = overlaps.argmax(axis=0)          #对于每个类,选择其对应的最大重叠度的anchor序号
        gt_max_overlaps = overlaps[gt_argmax_overlaps,       
                                   np.arange(overlaps.shape[1])]  #生成gt_max_overlaps,(为一行)即每类对应的最大重叠度
        gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]  #找到那些等于gt_max_overlaps的anchor,这些anchor将参与训练rpn
        # 找到所有overlaps中所有等于gt_max_overlaps的元素,因为gt_max_overlaps对于每个非负类别只保留一个
        # anchor,如果同一列有多个相等的最大IOU overlap值,那么就需要把其他的几个值找到,并在后面将它们
        # 的label设为1,即认为它们是object,毕竟在RPN的cls任务中,只要认为它是否是个object即可,即一个
        # 二分类问题。   (总结)
		
		
		
		# 如下设置了前景(1)、背景(0)以及不关心(-1)的anchor标签
        if not cfg.TRAIN.RPN_CLOBBER_POSITIVES:
            # assign bg labels first so that positive labels can clobber them
            labels[max_overlaps < cfg.TRAIN.RPN_NEGATIVE_OVERLAP] = 0    #对于最大重叠度低于0.3的设为背景

        # fg label: for each gt, anchor with highest overlap  
        labels[gt_argmax_overlaps] = 1   

        # fg label: above threshold IOU
        labels[max_overlaps >= cfg.TRAIN.RPN_POSITIVE_OVERLAP] = 1 

        if cfg.TRAIN.RPN_CLOBBER_POSITIVES:
            # assign bg labels last so that negative labels can clobber positives
            labels[max_overlaps < cfg.TRAIN.RPN_NEGATIVE_OVERLAP] = 0
			
			
		# 取前景与背景的anchor各一半,目前一批有256个anchor.
        # subsample positive labels if we have too many
        num_fg = int(cfg.TRAIN.RPN_FG_FRACTION * cfg.TRAIN.RPN_BATCHSIZE)   #256*0.5=128
        fg_inds = np.where(labels == 1)[0]
        if len(fg_inds) > num_fg:
            disable_inds = npr.choice(
                fg_inds, size=(len(fg_inds) - num_fg), replace=False)
            labels[disable_inds] = -1

        # subsample negative labels if we have too many
        num_bg = cfg.TRAIN.RPN_BATCHSIZE - np.sum(labels == 1)  #另一半256*0.5=128
        bg_inds = np.where(labels == 0)[0]
        if len(bg_inds) > num_bg:
            disable_inds = npr.choice(
                bg_inds, size=(len(bg_inds) - num_bg), replace=False)
            labels[disable_inds] = -1
            #print "was %s inds, disabling %s, now %s inds" % (
                #len(bg_inds), len(disable_inds), np.sum(labels == 0))
        
		#计算了所有在内部的anchor与对应的ground truth的回归量
        bbox_targets = np.zeros((len(inds_inside), 4), dtype=np.float32)
        bbox_targets = _compute_targets(anchors, gt_boxes[argmax_overlaps, :])
         
		 #只有前景类内部权重才非0,参与回归
        bbox_inside_weights = np.zeros((len(inds_inside), 4), dtype=np.float32)
        bbox_inside_weights[labels == 1, :] = np.array(cfg.TRAIN.RPN_BBOX_INSIDE_WEIGHTS) #(1.0, 1.0, 1.0, 1.0)

		# Give the positive RPN examples weight of p * 1 / {num positives}
		# and give negatives a weight of (1 - p)/(num negative)    
		# Set to -1.0 to use uniform example weighting
        bbox_outside_weights = np.zeros((len(inds_inside), 4), dtype=np.float32)
        if cfg.TRAIN.RPN_POSITIVE_WEIGHT < 0:
            # uniform weighting of examples (given non-uniform sampling)
            num_examples = np.sum(labels >= 0)
            positive_weights = np.ones((1, 4)) * 1.0 / num_examples
            negative_weights = np.ones((1, 4)) * 1.0 / num_examples
        else:
            assert ((cfg.TRAIN.RPN_POSITIVE_WEIGHT > 0) &
                    (cfg.TRAIN.RPN_POSITIVE_WEIGHT < 1))
            positive_weights = (cfg.TRAIN.RPN_POSITIVE_WEIGHT /
                                np.sum(labels == 1))
            negative_weights = ((1.0 - cfg.TRAIN.RPN_POSITIVE_WEIGHT) /
                                np.sum(labels == 0))
        bbox_outside_weights[labels == 1, :] = positive_weights  # 前景与背景anchor的外参数相同,都是1/anchor个数
        bbox_outside_weights[labels == 0, :] = negative_weights

        if DEBUG:
            self._sums += bbox_targets[labels == 1, :].sum(axis=0)
            self._squared_sums += (bbox_targets[labels == 1, :] ** 2).sum(axis=0)
            self._counts += np.sum(labels == 1)
            means = self._sums / self._counts
            stds = np.sqrt(self._squared_sums / self._counts - means ** 2)
            print 'means:'
            print means
            print 'stdevs:'
            print stds

        # map up to original set of anchors 生成全部anchor的数据,将非0的数据填入。
        labels = _unmap(labels, total_anchors, inds_inside, fill=-1)
        bbox_targets = _unmap(bbox_targets, total_anchors, inds_inside, fill=0)
        bbox_inside_weights = _unmap(bbox_inside_weights, total_anchors, inds_inside, fill=0)
        bbox_outside_weights = _unmap(bbox_outside_weights, total_anchors, inds_inside, fill=0)

        if DEBUG:
            print 'rpn: max max_overlap', np.max(max_overlaps)
            print 'rpn: num_positive', np.sum(labels == 1)
            print 'rpn: num_negative', np.sum(labels == 0)
            self._fg_sum += np.sum(labels == 1)
            self._bg_sum += np.sum(labels == 0)
            self._count += 1
            print 'rpn: num_positive avg', self._fg_sum / self._count
            print 'rpn: num_negative avg', self._bg_sum / self._count

        # labels 
        labels = labels.reshape((1, height, width, A)).transpose(0, 3, 1, 2)
        labels = labels.reshape((1, 1, A * height, width))
        top[0].reshape(*labels.shape)
        top[0].data[...] = labels

        # bbox_targets
        bbox_targets = bbox_targets \
            .reshape((1, height, width, A * 4)).transpose(0, 3, 1, 2)
        top[1].reshape(*bbox_targets.shape)
        top[1].data[...] = bbox_targets

        # bbox_inside_weights
        bbox_inside_weights = bbox_inside_weights \
            .reshape((1, height, width, A * 4)).transpose(0, 3, 1, 2)
        assert bbox_inside_weights.shape[2] == height
        assert bbox_inside_weights.shape[3] == width
        top[2].reshape(*bbox_inside_weights.shape)
        top[2].data[...] = bbox_inside_weights

        # bbox_outside_weights
        bbox_outside_weights = bbox_outside_weights \
            .reshape((1, height, width, A * 4)).transpose(0, 3, 1, 2)
        assert bbox_outside_weights.shape[2] == height
        assert bbox_outside_weights.shape[3] == width
        top[3].reshape(*bbox_outside_weights.shape)
        top[3].data[...] = bbox_outside_weights

这里已经有详细的注释,总的来说,rpn_cls_score的作用就是告知第五层feature map的宽和高。便于决定生成多少个anchor. 而其他的bottom输入才最终决定top的输出。


首先这里生成了所有feature map各点对应的anchors。生成的方式很特别,先考虑了左上角一个点的anchor生成,考虑到feat_stride=16,所以这个点对应原始图像(这里统一指缩放后image)的(0,0,15,15)感受野。然后取其中心点,生成比例为1:1,1:2,2:1,尺度在128,256,512的9个anchor.然后考虑使用平移生成其他的anchor.

然后过滤掉那些不在图像内部的anchor. 对于剩下的anchor,计算与gt_boxes的重叠度,再分别计算label,bbox_targets,bbox_inside_weights,bbox_outside_weights.


最后将内部的anchor的相关变量扩充到所有的anchor,只不过不在内部的为0即可。尤其值得说的是对于内部的anchor,bbox_targets都进行了运算。但是选取了256个anchor,前景与背景比例为1:1,bbox_inside_weights中只有label=1,即前景才进行了设置。正如论文所说,对于回归项,需要内部参数来约束,bbox_inside_weights正好起到了这个作用。

我们统计一下top的shape:

rpn_labels : (1, 1, 9 * height, width)

rpn_bbox_targets(回归目标): (1, 36,height, width)

rpn_bbox_inside_weights(内权重):(1, 36,height, width)

rpn_bbox_outside_weights(外权重):(1, 36,height, width)


回到stage1_rpn_train.pt,接下里我们就可以利用rpn_cls_score_reshape与rpn_labels计算SoftmaxWithLoss,输出rpn_cls_loss。

而regression可以利用rpn_bbox_pred,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights计算SmoothL1Loss,输出rpn_loss_bbox。

回到我们之前有一个问题rpn_bbox_pred的shape怎么构造的。其实从rpn_bbox_targets的生成过程中可以推断出应该采用后一种,即第一个盒子的回归量(dx1,dy1,dw1,dh1),第二个为(dx2,dy2,dw,2,dh2).....,这样顺序着来。


其实怎么样认为都是从我们方便的角度出发。



至此我们完成了rpn的前向过程,反向过程中只需注意AnchorTargetLayer不参与反向传播。因为它提供的都是源数据。


参考:

1.  http://blog.csdn.net/zy1034092330/article/details/62044941 

2.  Faster RCNN anchor_target_layer.py


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值