1. 程式人生 > >py-faster-rcnn原始碼AnchorTargetLayer

py-faster-rcnn原始碼AnchorTargetLayer

本文介紹了在solver中出現的用python定義的layer,顧名思義,該layer主要功能是產生anchor,並對anchor進行評分等操作,詳細見程式碼註釋。

 class AnchorTargetLayer(caffe.Layer):
"""
Assign anchors to ground-truth targets. Produces anchor classification
labels and bounding-box regression targets.
"""
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

setup函式

首先讀取了,在.prototxt中定義的相關引數,事實上只有feat\_stride,一般被定義為16.
然後設定了相關引數比如\_anchors,由一個工具py中的方法generate\_anchors產生,通常為如下九個,有興趣的讀者不妨在紙上畫一畫,便可知道其中奧祕,在這裡賣個關子:)
 anchors =  
 (xmin  ymin xmax ymax)
 -83   -39   100    56
 -175   -87   192   104
 -359  -183   376   200
 -55   -55    72    72
 -119  -119   136   136
 -247  -247   264   264
 -35   -79    52    96
 -79  -167    96   184
 -167  -343   184   360
以及一些其他需要用到的的屬性。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
def setup(self, bottom, top):
    layer_params = yaml.load(self.param_str_)
    anchor_scales = layer_params.get('scales', (8, 16, 32))
    self._anchors = generate_anchors(scales=np.array(anchor_scales))
    self._num_anchors = self._anchors.shape[0]
    self._feat_stride = layer_params['feat_stride'
] #fg指的是前景 fore ground bg指的是背景 back ground self._counts = cfg.EPS self._sums = np.zeros((1, 4)) self._squared_sums = np.zeros((1, 4)) self._fg_sum = 0 self._bg_sum = 0 self._count = 0 # allow boxes to sit over the edge by a small amount self._allowed_border = layer_params.get('allowed_border'
, 0) height, width = bottom[0].data.shape[-2:] #A 一般為 9 A = self._num_anchors # 在這裡將top的維度結構reshape # labels top[0].reshape(1, 1, A * height, width) # bbox_targets top[1].reshape(1, A * 4, height, width) # bbox_inside_weights top[2].reshape(1, A * 4, height, width) # bbox_outside_weights top[3].reshape(1, A * 4, height, width)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

forward

前向傳播: 在函式開頭的註釋已經闡述的很清楚了,對於每一個(H,W)位置點,都產生九個不同形狀的anchor,在網路結構定義中H=61,W=36你會發現這裡的H x feat_stride以及W x feat_stride正好約等於rescale以後的每張圖的大小,好像是(900 x 533)? 然後僅僅保留範圍在原圖中的anchor,大概裁掉了2/3這樣,並分別計算這些anchor與每個ground truth的重合度。

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    
    # 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, :]

    #在61 x 36每一個位置點上生成九個anchor,你可以想象成在一張圖中均勻地取了61 x 36個點,然後 shift_x和shift_y分別是這些點在圖中的偏移位置,讓這些偏移值加上每個anchor的四個座標點。然後就獲得了一個all_anchors,一個(K*A,4)大的二維陣列。
    # 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,inds_inside是在影象內部的anchor的索引陣列
    # only keep anchors inside the image
    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]
    # keep only inside anchors
    anchors = all_anchors[inds_inside, :]

    # label: 1 is positive, 0 is negative, -1 is dont care
    labels = np.empty((len(inds_inside), ), dtype=np.float32)
    labels.fill(-1)
    #這裡overlaps是計算所有anchor與ground-truth的重合度,它是一個len(anchors) x len(gt_boxes)的二維陣列,每個元素是各個anchor和gt_boxes的overlap值,這個overlap值的計算是這樣的:
    overlap = (重合部分面積) / (anchor面積 + gt_boxes面積 - 重合部分面積)
    · argmax_overlaps是每個anchor對應最大overlap的gt_boxes的下標
    · max_overlaps是每個anchor對應最大的overlap值
    相對應的
    · gt_argmax_overlaps是每個gt_boxes對應最大overlap的anchor的下標
    · gt_max_overlaps是每個gt_boxes對應最大的overlap值
    # 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)
    max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]
    gt_argmax_overlaps = overlaps.argmax(axis=0)
    gt_max_overlaps = overlaps[gt_argmax_overlaps,
    np.arange(overlaps.shape[1])]
    #加上這一步是因為有很多overlap並列第一
    gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]
    #接下來是打標籤的工作
    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

    # 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

    #接下來兩步工作是為了讓正樣本與負樣本嚴格保持1:1
    # subsample positive labels if we have too many
    num_fg = int(cfg.TRAIN.RPN_FG_FRACTION * cfg.TRAIN.RPN_BATCHSIZE)
    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)
    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的偏移值,詳細的計算方法在論文中提到,在fast-rcnn/bbox_transform.py中的bbox_transform函式也非常容易看懂
    bbox_targets = np.zeros((len(inds_inside), 4), dtype=np.float32)
    bbox_targets = _compute_targets(anchors, gt_boxes[argmax_overlaps, :])

    #這裡是inside_weight和out_weight的計算。- -#不過好像全程都是1
    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)

    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
    bbox_outside_weights[labels == 0, :] = negative_weights


    #還記得文初將all_anchors裁減掉了2/3左右,僅僅保留在影象內的anchor嗎,這裡就是將其復原作為下一層的輸入了,並reshape成相應的格式
    # map up to original set of anchors
    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)


    # 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

    def backward(self, top, propagate_down, bottom):
    """This layer does not propagate gradients."""
    pass

    def reshape(self, bottom, top):
    """Reshaping happens during the call to forward."""
    pass
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168

_unmap

上個函式將all_anchors裁減掉了2/3左右,僅僅保留在影象內的anchor,這裡就是將其復原作為下一層的輸入了,並reshape成相應的格式


def _unmap(data, count, inds, fill=0):
    """ Unmap a subset of item (data) back to the original set of items (of
    size count) """
    if len(data.shape) == 1:
    ret = np.empty((count, ), dtype=np.float32)
    ret.fill(fill)
    ret[inds] = data
    else:
    ret = np.empty((count, ) + data.shape[1:], dtype=np.float32)
    ret.fill(fill)
    ret[inds, :] = data
    return ret
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

_compute_targets

計算與每個anchor最大重合度的ground-truth的(x,y,width,height)的偏移值


def _compute_targets(ex_rois, gt_rois):
"""Compute bounding-box regression targets for an image."""

assert ex_rois.shape[0] == gt_rois.shape[0]
assert ex_rois.shape[1] == 4
assert gt_rois.shape[1] == 5

return bbox_transform(ex_rois, gt_rois[:, :4]).astype(np.float32, copy=False)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-8cccb36679.css" rel="stylesheet">
            </div>