parsing_rcnn_model.py
def build_rpn_targets(image_shape, anchors, gt_class_ids, gt_boxes, config):
    """Given the anchors and GT boxes, compute overlaps and identify positive
    anchors and deltas to refine them to match their corresponding GT boxes.

    anchors: [num_anchors, (y1, x1, y2, x2)]
    gt_class_ids: [num_gt_boxes] Integer class IDs.
    gt_boxes: [num_gt_boxes, (y1, x1, y2, x2)]

    Returns:
    rpn_match: [N] (int32) matches between anchors and GT boxes.
               1 = positive anchor, -1 = negative anchor, 0 = neutral
    rpn_bbox: [N, (dy, dx, log(dh), log(dw))] Anchor bbox deltas.
    """
    # RPN Match: 1 = positive anchor, -1 = negative anchor, 0 = neutral
    rpn_match = np.zeros([anchors.shape[0]], dtype=np.int32)
    # RPN bounding boxes: [max anchors per image, (dy, dx, log(dh), log(dw))]
    rpn_bbox = np.zeros((config.RPN_TRAIN_ANCHORS_PER_IMAGE, 4))


    # Compute overlaps [num_anchors, num_gt_boxes]
    overlaps = utils.compute_overlaps(anchors, gt_boxes)

    # Match anchors to GT Boxes
    # If an anchor overlaps a GT box with IoU >= 0.7 then it's positive.
    # If an anchor overlaps a GT box with IoU < 0.3 then it's negative.
    # Neutral anchors are those that don't match the conditions above,
    # and they don't influence the loss function.
    # However, don't keep any GT box unmatched (rare, but happens). Instead,
    # match it to the closest anchor (even if its max IoU is < 0.3).
    #
    # 1. Set negative anchors first. They get overwritten below if a GT box is
    # matched to them. Skip boxes in crowd areas.
    anchor_iou_argmax = np.argmax(overlaps, axis=1)
    anchor_iou_max = overlaps[np.arange(overlaps.shape[0]), anchor_iou_argmax]
    rpn_match[(anchor_iou_max < 0.3)] = -1
    # 2. Set an anchor for each GT box (regardless of IoU value).
    # TODO: If multiple anchors have the same IoU match all of them
    gt_iou_argmax = np.argmax(overlaps, axis=0)
    rpn_match[gt_iou_argmax] = 1
    # 3. Set anchors with high overlap as positive.
    rpn_match[anchor_iou_max >= 0.7] = 1

    # Subsample to balance positive and negative anchors
    # Don't let positives be more than half the anchors
    ids = np.where(rpn_match == 1)[0]
    extra = len(ids) - (config.RPN_TRAIN_ANCHORS_PER_IMAGE // 2)
    if extra > 0:
        # Reset the extra ones to neutral
        ids = np.random.choice(ids, extra, replace=False)
        rpn_match[ids] = 0
    # Same for negative proposals
    ids = np.where(rpn_match == -1)[0]
    extra = len(ids) - (config.RPN_TRAIN_ANCHORS_PER_IMAGE -
                        np.sum(rpn_match == 1))
    if extra > 0:
        # Rest the extra ones to neutral
        ids = np.random.choice(ids, extra, replace=False)
        rpn_match[ids] = 0

    # For positive anchors, compute shift and scale needed to transform them
    # to match the corresponding GT boxes.
    ids = np.where(rpn_match == 1)[0]
    ix = 0  # index into rpn_bbox
    # TODO: use box_refinment() rather than duplicating the code here
    for i, a in zip(ids, anchors[ids]):
        # Closest gt box (it might have IoU < 0.7)
        gt = gt_boxes[anchor_iou_argmax[i]]

        # Convert coordinates to center plus width/height.
        # GT Box
        gt_h = gt[2] - gt[0]
        gt_w = gt[3] - gt[1]
        gt_center_y = gt[0] + 0.5 * gt_h
        gt_center_x = gt[1] + 0.5 * gt_w
        # Anchor
        a_h = a[2] - a[0]
        a_w = a[3] - a[1]
        a_center_y = a[0] + 0.5 * a_h
        a_center_x = a[1] + 0.5 * a_w

        # Compute the bbox refinement that the RPN should predict.
        rpn_bbox[ix] = [
            (gt_center_y - a_center_y) / a_h,
            (gt_center_x - a_center_x) / a_w,
            np.log(gt_h / a_h),
            np.log(gt_w / a_w),
        ]
        # Normalize
        rpn_bbox[ix] /= config.RPN_BBOX_STD_DEV
        ix += 1

    return rpn_match, rpn_bbox
Given the anchors and GT boxes, compute overlaps and identify positive
    anchors and deltas to refine them to match their corresponding GT boxes.

    anchors: [num_anchors, (y1, x1, y2, x2)]
    gt_class_ids: [num_gt_boxes] Integer class IDs.
    gt_boxes: [num_gt_boxes, (y1, x1, y2, x2)]

    Returns:
    rpn_match: [N] (int32) matches between anchors and GT boxes.
               1 = positive anchor, -1 = negative anchor, 0 = neutral
    rpn_bbox: [N, (dy, dx, log(dh), log(dw))] Anchor bbox deltas.
    
Given the anchors and GT boxes, compute overlaps and identify positive
anchors and deltas to refine them to match their corresponding GT boxes.


Given the anchors and GT boxes compute overlaps and identify positive anchors and deltas to refine them to match their corresponding GT boxes .
def build_rpn_targets(image_shape, anchors, gt_class_ids, gt_boxes, config):
    rpn_match = np.zeros([anchors.shape[0]], dtype=np.int32)
    rpn_bbox = np.zeros((config.RPN_TRAIN_ANCHORS_PER_IMAGE, 4))
    overlaps = utils.compute_overlaps(anchors, gt_boxes)
     1. Set negative anchors first. They get overwritten below if a GT box is
     matched to them. Skip boxes in crowd areas.
    anchor_iou_argmax = np.argmax(overlaps, axis=1)
    anchor_iou_max = overlaps[np.arange(overlaps.shape[0]), anchor_iou_argmax]
    rpn_match[(anchor_iou_max < 0.3)] = -1
     2. Set an anchor for each GT box (regardless of IoU value).
     TODO: If multiple anchors have the same IoU match all of them
    gt_iou_argmax = np.argmax(overlaps, axis=0)
    rpn_match[gt_iou_argmax] = 1
     3. Set anchors with high overlap as positive.
    rpn_match[anchor_iou_max >= 0.7] = 1
     Subsample to balance positive and negative anchors
     Don't let positives be more than half the anchors
    ids = np.where(rpn_match == 1)[0]
    extra = len(ids) - (config.RPN_TRAIN_ANCHORS_PER_IMAGE // 2)
    if extra > 0:
         Reset the extra ones to neutral
        ids = np.random.choice(ids, extra, replace=False)
        rpn_match[ids] = 0
     Same for negative proposals
    ids = np.where(rpn_match == -1)[0]
    extra = len(ids) - (config.RPN_TRAIN_ANCHORS_PER_IMAGE -
                        np.sum(rpn_match == 1))
    if extra > 0:
         Rest the extra ones to neutral
        ids = np.random.choice(ids, extra, replace=False)
        rpn_match[ids] = 0
     For positive anchors, compute shift and scale needed to transform them
     to match the corresponding GT boxes.
    ids = np.where(rpn_match == 1)[0]
    ix = 0   index into rpn_bbox
     TODO: use box_refinment() rather than duplicating the code here
    for i, a in zip(ids, anchors[ids]):
         Closest gt box (it might have IoU < 0.7)
        gt = gt_boxes[anchor_iou_argmax[i]]
         Convert coordinates to center plus width/height.
         GT Box
        gt_h = gt[2] - gt[0]
        gt_w = gt[3] - gt[1]
        gt_center_y = gt[0] + 0.5 * gt_h
        gt_center_x = gt[1] + 0.5 * gt_w
         Anchor
        a_h = a[2] - a[0]
        a_w = a[3] - a[1]
        a_center_y = a[0] + 0.5 * a_h
        a_center_x = a[1] + 0.5 * a_w
         Compute the bbox refinement that the RPN should predict.
        rpn_bbox[ix] = [
            (gt_center_y - a_center_y) / a_h,
            (gt_center_x - a_center_x) / a_w,
            np.log(gt_h / a_h),
            np.log(gt_w / a_w),
        ]
         Normalize
        rpn_bbox[ix] /= config.RPN_BBOX_STD_DEV
        ix += 1
    return rpn_match, rpn_bbox
def build_rpn_targets ( image_shape , anchors , gt_class_ids , gt_boxes , config ) : rpn_match = np . zeros ( [ anchors . shape [ 0 ] ] , dtype = np . int32 ) rpn_bbox = np . zeros ( ( config . RPN_TRAIN_ANCHORS_PER_IMAGE , 4 ) ) overlaps = utils . compute_overlaps ( anchors , gt_boxes ) anchor_iou_argmax = np . argmax ( overlaps , axis = 1 ) anchor_iou_max = overlaps [ np . arange ( overlaps . shape [ 0 ] ) , anchor_iou_argmax ] rpn_match [ ( anchor_iou_max < 0.3 ) ] = - 1 gt_iou_argmax = np . argmax ( overlaps , axis = 0 ) rpn_match [ gt_iou_argmax ] = 1 rpn_match [ anchor_iou_max >= 0.7 ] = 1 ids = np . where ( rpn_match == 1 ) [ 0 ] extra = len ( ids ) - ( config . RPN_TRAIN_ANCHORS_PER_IMAGE // 2 ) if extra > 0 : ids = np . random . choice ( ids , extra , replace = False ) rpn_match [ ids ] = 0 ids = np . where ( rpn_match == - 1 ) [ 0 ] extra = len ( ids ) - ( config . RPN_TRAIN_ANCHORS_PER_IMAGE - np . sum ( rpn_match == 1 ) ) if extra > 0 : ids = np . random . choice ( ids , extra , replace = False ) rpn_match [ ids ] = 0 ids = np . where ( rpn_match == 1 ) [ 0 ] ix = 0 for i , a in zip ( ids , anchors [ ids ] ) : gt = gt_boxes [ anchor_iou_argmax [ i ] ] gt_h = gt [ 2 ] - gt [ 0 ] gt_w = gt [ 3 ] - gt [ 1 ] gt_center_y = gt [ 0 ] + 0.5 * gt_h gt_center_x = gt [ 1 ] + 0.5 * gt_w a_h = a [ 2 ] - a [ 0 ] a_w = a [ 3 ] - a [ 1 ] a_center_y = a [ 0 ] + 0.5 * a_h a_center_x = a [ 1 ] + 0.5 * a_w rpn_bbox [ ix ] = [ ( gt_center_y - a_center_y ) / a_h , ( gt_center_x - a_center_x ) / a_w , np . log ( gt_h / a_h ) , np . log ( gt_w / a_w ) , ] rpn_bbox [ ix ] /= config . RPN_BBOX_STD_DEV ix += 1 return rpn_match , rpn_bbox
Given the anchors and GT boxes, compute overlaps and identify positive
anchors and deltas to refine them to match their corresponding GT boxes.
Given the anchors and GT boxes compute overlaps and identify positive anchors and deltas to refine them to match their corresponding GT boxes .
"""Given the anchors and GT boxes, compute overlaps and identify positive
    anchors and deltas to refine them to match their corresponding GT boxes.

    anchors: [num_anchors, (y1, x1, y2, x2)]
    gt_class_ids: [num_gt_boxes] Integer class IDs.
    gt_boxes: [num_gt_boxes, (y1, x1, y2, x2)]

    Returns:
    rpn_match: [N] (int32) matches between anchors and GT boxes.
               1 = positive anchor, -1 = negative anchor, 0 = neutral
    rpn_bbox: [N, (dy, dx, log(dh), log(dw))] Anchor bbox deltas.
    """ # RPN Match: 1 = positive anchor, -1 = negative anchor, 0 = neutral # RPN bounding boxes: [max anchors per image, (dy, dx, log(dh), log(dw))] # Compute overlaps [num_anchors, num_gt_boxes] # Match anchors to GT Boxes # If an anchor overlaps a GT box with IoU >= 0.7 then it's positive. # If an anchor overlaps a GT box with IoU < 0.3 then it's negative. # Neutral anchors are those that don't match the conditions above, # and they don't influence the loss function. # However, don't keep any GT box unmatched (rare, but happens). Instead, # match it to the closest anchor (even if its max IoU is < 0.3). # # 1. Set negative anchors first. They get overwritten below if a GT box is # matched to them. Skip boxes in crowd areas. # 2. Set an anchor for each GT box (regardless of IoU value). # TODO: If multiple anchors have the same IoU match all of them # 3. Set anchors with high overlap as positive. # Subsample to balance positive and negative anchors # Don't let positives be more than half the anchors # Reset the extra ones to neutral # Same for negative proposals # Rest the extra ones to neutral # For positive anchors, compute shift and scale needed to transform them # to match the corresponding GT boxes. # index into rpn_bbox # TODO: use box_refinment() rather than duplicating the code here # Closest gt box (it might have IoU < 0.7) # Convert coordinates to center plus width/height. # GT Box # Anchor # Compute the bbox refinement that the RPN should predict. # Normalize
None
image_shape
anchors
gt_class_ids
gt_boxes
config