def _sample_rois(roidb, fg_rois_per_image, rois_per_image, num_classes):
    """Generate a random sample of RoIs comprising foreground and background
    examples.
    """
    # label = class RoI has max overlap with
    labels = roidb['max_classes']
    overlaps = roidb['max_overlaps']
    rois = roidb['boxes']

    # Select foreground RoIs as those with >= FG_THRESH overlap
    fg_inds = []
    for i in xrange(1, num_classes):
        fg_inds.extend(np.where((labels == i) & (overlaps >= cfg.TRAIN.FG_THRESH))[0])
    fg_inds = np.array(fg_inds)

    # 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:
        fg_inds = npr.choice(fg_inds, size=fg_rois_per_this_image,
                             replace=False)

    bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
    # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
    bg_inds = []
    for i in xrange(1, num_classes):
        bg_inds.extend( np.where((labels == i) & (overlaps < cfg.TRAIN.BG_THRESH_HI) &
                        (overlaps >= cfg.TRAIN.BG_THRESH_LO))[0] )

    if len(bg_inds) < bg_rois_per_this_image:
        for i in xrange(1, num_classes):
            bg_inds.extend( np.where((labels == i) & (overlaps < cfg.TRAIN.BG_THRESH_HI))[0] )

    if len(bg_inds) < bg_rois_per_this_image:
        bg_inds.extend( np.where(overlaps < cfg.TRAIN.BG_THRESH_HI)[0] )
    bg_inds = np.array(bg_inds, dtype=np.int32)

    # Compute number of background RoIs to take from this image (guarding
    # against there being fewer than desired)
    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:
        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).astype(int)
    # print '{} foregrounds and {} backgrounds'.format(fg_inds.size, bg_inds.size)
    # Select sampled values from various arrays:
    labels = labels[keep_inds]
    # Clamp labels for the background RoIs to 0
    labels[fg_rois_per_this_image:] = 0
    overlaps = overlaps[keep_inds]
    rois = rois[keep_inds]
    sublabels = sublabels[keep_inds]
    sublabels[fg_rois_per_this_image:] = 0

    bbox_targets, bbox_loss_weights = \
            _get_bbox_regression_labels(roidb['bbox_targets'][keep_inds, :],
                                        num_classes)

    if cfg.TRAIN.VIEWPOINT or cfg.TEST.VIEWPOINT:
        viewpoints = viewpoints[keep_inds]
        view_targets, view_loss_weights = \
                _get_viewpoint_estimation_labels(viewpoints, labels, num_classes)
        return labels, overlaps, rois, bbox_targets, bbox_loss_weights, sublabels, view_targets, view_loss_weights

    return labels, overlaps, rois, bbox_targets, bbox_loss_weights, sublabels
Generate a random sample of RoIs comprising foreground and background
    examples.
    
lib/roi_data_layer/minibatch2.py