def forward(self, input):
    scores = input[0][:, self._num_anchors:, :, :]
    bbox_deltas = input[1]
    im_info = input[2]
    cfg_key = input[3]
    pre_nms_topN = cfg[cfg_key].RPN_PRE_NMS_TOP_N
    post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N
    nms_thresh = cfg[cfg_key].RPN_NMS_THRESH
    min_size = cfg[cfg_key].RPN_MIN_SIZE
    batch_size = bbox_deltas.size(0)
    (feat_height, feat_width) = (scores.size(2), scores.size(3))
    shift_x = (np.arange(0, feat_width) * self._feat_stride)
    shift_y = (np.arange(0, feat_height) * self._feat_stride)
    (shift_x, shift_y) = np.meshgrid(shift_x, shift_y)
    shifts = torch.from_numpy(np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose())
    shifts = shifts.contiguous().type_as(scores).float()
    A = self._num_anchors
    K = shifts.size(0)
    self._anchors = self._anchors.type_as(scores)
    anchors = (self._anchors.view(1, A, 4) + shifts.view(K, 1, 4))
    anchors = anchors.view(1, (K * A), 4).expand(batch_size, (K * A), 4)
    bbox_deltas = bbox_deltas.permute(0, 2, 3, 1).contiguous()
    bbox_deltas = bbox_deltas.view(batch_size, (- 1), 4)
    scores = scores.permute(0, 2, 3, 1).contiguous()
    scores = scores.view(batch_size, (- 1))
    proposals = bbox_transform_inv(anchors, bbox_deltas, batch_size)
    proposals = clip_boxes(proposals, im_info, batch_size)
    scores_keep = scores
    proposals_keep = proposals
    (_, order) = torch.sort(scores_keep, 1, True)
    output = scores.new(batch_size, post_nms_topN, 5).zero_()
    for i in range(batch_size):
        proposals_single = proposals_keep[i]
        scores_single = scores_keep[i]
        order_single = order[i]
        if ((pre_nms_topN > 0) and (pre_nms_topN < scores_keep.numel())):
            order_single = order_single[:pre_nms_topN]
        proposals_single = proposals_single[order_single, :]
        scores_single = scores_single[order_single].view((- 1), 1)
        keep_idx_i = nms(torch.cat((proposals_single, scores_single), 1), nms_thresh, force_cpu=(not cfg.USE_GPU_NMS))
        keep_idx_i = keep_idx_i.long().view((- 1))
        if (post_nms_topN > 0):
            keep_idx_i = keep_idx_i[:post_nms_topN]
        proposals_single = proposals_single[keep_idx_i, :]
        scores_single = scores_single[keep_idx_i, :]
        num_proposal = proposals_single.size(0)
        output[i, :, 0] = i
        output[i, :num_proposal, 1:] = proposals_single
    return output
def forward(self, input):
    scores = input[0][:, self._num_anchors:, :, :]
    bbox_deltas = input[1]
    im_info = input[2]
    cfg_key = input[3]
    pre_nms_topN = cfg[cfg_key].RPN_PRE_NMS_TOP_N
    post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N
    nms_thresh = cfg[cfg_key].RPN_NMS_THRESH
    min_size = cfg[cfg_key].RPN_MIN_SIZE
    batch_size = bbox_deltas.size(0)
    (feat_height, feat_width) = (scores.size(2), scores.size(3))
    shift_x = (np.arange(0, feat_width) * self._feat_stride)
    shift_y = (np.arange(0, feat_height) * self._feat_stride)
    (shift_x, shift_y) = np.meshgrid(shift_x, shift_y)
    shifts = torch.from_numpy(np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose())
    shifts = shifts.contiguous().type_as(scores).float()
    A = self._num_anchors
    K = shifts.size(0)
    self._anchors = self._anchors.type_as(scores)
    anchors = (self._anchors.view(1, A, 4) + shifts.view(K, 1, 4))
    anchors = anchors.view(1, (K * A), 4).expand(batch_size, (K * A), 4)
    bbox_deltas = bbox_deltas.permute(0, 2, 3, 1).contiguous()
    bbox_deltas = bbox_deltas.view(batch_size, (- 1), 4)
    scores = scores.permute(0, 2, 3, 1).contiguous()
    scores = scores.view(batch_size, (- 1))
    proposals = bbox_transform_inv(anchors, bbox_deltas, batch_size)
    proposals = clip_boxes(proposals, im_info, batch_size)
    scores_keep = scores
    proposals_keep = proposals
    (_, order) = torch.sort(scores_keep, 1, True)
    output = scores.new(batch_size, post_nms_topN, 5).zero_()
    for i in range(batch_size):
        proposals_single = proposals_keep[i]
        scores_single = scores_keep[i]
        order_single = order[i]
        if ((pre_nms_topN > 0) and (pre_nms_topN < scores_keep.numel())):
            order_single = order_single[:pre_nms_topN]
        proposals_single = proposals_single[order_single, :]
        scores_single = scores_single[order_single].view((- 1), 1)
        keep_idx_i = nms(torch.cat((proposals_single, scores_single), 1), nms_thresh, force_cpu=(not cfg.USE_GPU_NMS))
        keep_idx_i = keep_idx_i.long().view((- 1))
        if (post_nms_topN > 0):
            keep_idx_i = keep_idx_i[:post_nms_topN]
        proposals_single = proposals_single[keep_idx_i, :]
        scores_single = scores_single[keep_idx_i, :]
        num_proposal = proposals_single.size(0)
        output[i, :, 0] = i
        output[i, :num_proposal, 1:] = proposals_single
    return output