(roidb, im_scale, batch_idx)
return blob_dict
Generate a random sample of RoIs comprising foreground and background
    examples.
Generate a random sample of RoIs comprising foreground and background
    examples.
Generate a random sample of RoIs comprising foreground and background examples .
def sample_rois(roidb, im_scale, batch_idx):
    """Generate a random sample of RoIs comprising foreground and background
    examples.
    """
    rois_per_image = int(config.train.batch_rois)
    fg_rois_per_image = int(np.round(config.train.fg_fraction * rois_per_image))
    max_overlaps = roidb['max_overlaps']

    # Select foreground RoIs as those with >= FG_THRESH overlap
    fg_inds = np.where(max_overlaps >= config.train.fg_thresh)[0]
    # Guard against the case when an image has fewer than fg_rois_per_image
    # foreground RoIs
    fg_rois_per_this_image = np.minimum(fg_rois_per_image, fg_inds.size)
    # Sample foreground regions without replacement
    if fg_inds.size > 0:
        # print('sample rois use first 128 fg')
        # fg_inds = fg_inds[:fg_rois_per_this_image]
        fg_inds = npr.choice(
            fg_inds, size=fg_rois_per_this_image, replace=False
        )

    # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
    bg_inds = np.where(
        (max_overlaps < config.train.bg_thresh_hi) &
        (max_overlaps >= config.train.bg_thresh_lo)
    )[0]
    # Compute number of background RoIs to take from this image (guarding
    # against there being fewer than desired)
    bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
    bg_rois_per_this_image = np.minimum(bg_rois_per_this_image, bg_inds.size)
    # Sample foreground regions without replacement
    if bg_inds.size > 0:
        # print('sample rois use first 384 bg')
        # bg_inds = bg_inds[:bg_rois_per_this_image]
        bg_inds = npr.choice(
            bg_inds, size=bg_rois_per_this_image, replace=False
        )

    # The indices that we're selecting (both fg and bg)
    keep_inds = np.append(fg_inds, bg_inds)
    # print(keep_inds.shape)
    # Label is the class each RoI has max overlap with
    sampled_labels = roidb['max_classes'][keep_inds]
    sampled_labels[fg_rois_per_this_image:] = 0  # Label bg RoIs with class 0
    sampled_boxes = roidb['boxes'][keep_inds]

    if 'bbox_targets' not in roidb:
        gt_inds = np.where(roidb['gt_classes'] > 0)[0]
        gt_boxes = roidb['boxes'][gt_inds, :]
        gt_assignments = gt_inds[roidb['box_to_gt_ind_map'][keep_inds]]
        bbox_targets = _compute_targets(
            sampled_boxes, gt_boxes[gt_assignments, :], sampled_labels
        )
        bbox_targets, bbox_inside_weights = _expand_bbox_targets(bbox_targets)
    else:
        bbox_targets, bbox_inside_weights = _expand_bbox_targets(
            roidb['bbox_targets'][keep_inds, :]
        )

    bbox_outside_weights = np.array(
        bbox_inside_weights > 0, dtype=bbox_inside_weights.dtype
    )

    # Scale rois and format as (batch_idx, x1, y1, x2, y2)
    sampled_rois = sampled_boxes * im_scale
    repeated_batch_idx = batch_idx * np.ones((sampled_rois.shape[0], 1), np.float32)
    sampled_rois = np.hstack((repeated_batch_idx, sampled_rois))

    nongt_inds = np.where(roidb['gt_classes'][keep_inds] == 0)[0]

    # Base Fast R-CNN blobs
    blob_dict = dict(
        labels_int32=sampled_labels.astype(np.int32, copy=False),
        rois=sampled_rois,
        bbox_targets=bbox_targets,
        bbox_inside_weights=bbox_inside_weights,
        bbox_outside_weights=bbox_outside_weights,
        nongt_inds=nongt_inds
    )

    # Optionally add Mask R-CNN blobs
    if config.network.has_mask_head:
        add_mask_rcnn_blobs(
            blob_dict, sampled_boxes, roidb, im_scale, batch_idx
        )

    return blob_dict
def sample_rois ( roidb , im_scale , batch_idx ) : rois_per_image = int ( config . train . batch_rois ) fg_rois_per_image = int ( np . round ( config . train . fg_fraction * rois_per_image ) ) max_overlaps = roidb [ 'max_overlaps' ] # Select foreground RoIs as those with >= FG_THRESH overlap fg_inds = np . where ( max_overlaps >= config . train . fg_thresh ) [ 0 ] # Guard against the case when an image has fewer than fg_rois_per_image # foreground RoIs fg_rois_per_this_image = np . minimum ( fg_rois_per_image , fg_inds . size ) # Sample foreground regions without replacement if fg_inds . size > 0 : # print('sample rois use first 128 fg') # fg_inds = fg_inds[:fg_rois_per_this_image] fg_inds = npr . choice ( fg_inds , size = fg_rois_per_this_image , replace = False ) # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI) bg_inds = np . where ( ( max_overlaps < config . train . bg_thresh_hi ) & ( max_overlaps >= config . train . bg_thresh_lo ) ) [ 0 ] # Compute number of background RoIs to take from this image (guarding # against there being fewer than desired) bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image bg_rois_per_this_image = np . minimum ( bg_rois_per_this_image , bg_inds . size ) # Sample foreground regions without replacement if bg_inds . size > 0 : # print('sample rois use first 384 bg') # bg_inds = bg_inds[:bg_rois_per_this_image] bg_inds = npr . choice ( bg_inds , size = bg_rois_per_this_image , replace = False ) # The indices that we're selecting (both fg and bg) keep_inds = np . append ( fg_inds , bg_inds ) # print(keep_inds.shape) # Label is the class each RoI has max overlap with sampled_labels = roidb [ 'max_classes' ] [ keep_inds ] sampled_labels [ fg_rois_per_this_image : ] = 0 # Label bg RoIs with class 0 sampled_boxes = roidb [ 'boxes' ] [ keep_inds ] if 'bbox_targets' not in roidb : gt_inds = np . where ( roidb [ 'gt_classes' ] > 0 ) [ 0 ] gt_boxes = roidb [ 'boxes' ] [ gt_inds , : ] gt_assignments = gt_inds [ roidb [ 'box_to_gt_ind_map' ] [ keep_inds ] ] bbox_targets = _compute_targets ( sampled_boxes , gt_boxes [ gt_assignments , : ] , sampled_labels ) bbox_targets , bbox_inside_weights = _expand_bbox_targets ( bbox_targets ) else : bbox_targets , bbox_inside_weights = _expand_bbox_targets ( roidb [ 'bbox_targets' ] [ keep_inds , : ] ) bbox_outside_weights = np . array ( bbox_inside_weights > 0 , dtype = bbox_inside_weights . dtype ) # Scale rois and format as (batch_idx, x1, y1, x2, y2) sampled_rois = sampled_boxes * im_scale repeated_batch_idx = batch_idx * np . ones ( ( sampled_rois . shape [ 0 ] , 1 ) , np . float32 ) sampled_rois = np . hstack ( ( repeated_batch_idx , sampled_rois ) ) nongt_inds = np . where ( roidb [ 'gt_classes' ] [ keep_inds ] == 0 ) [ 0 ] # Base Fast R-CNN blobs blob_dict = dict ( labels_int32 = sampled_labels . astype ( np . int32 , copy = False ) , rois = sampled_rois , bbox_targets = bbox_targets , bbox_inside_weights = bbox_inside_weights , bbox_outside_weights = bbox_outside_weights , nongt_inds = nongt_inds ) # Optionally add Mask R-CNN blobs if config . network . has_mask_head : add_mask_rcnn_blobs ( blob_dict , sampled_boxes , roidb , im_scale , batch_idx ) return blob_dict