def _sample_graph(roidb, num_fg_rois, num_rois, num_neg_rels=128):
    '\n    Sample a graph from the foreground rois of an image\n\n    roidb: roidb of an image\n    rois_per_image: maximum number of rois per image\n    '
    gt_rels = roidb['gt_relations']
    fg_gt_ind_assignments = roidb['fg_gt_ind_assignments']
    gt_to_fg_roi_inds = {}
    all_fg_roi_inds = []
    for (ind, gt_ind) in fg_gt_ind_assignments.items():
        if (gt_ind not in gt_to_fg_roi_inds):
            gt_to_fg_roi_inds[gt_ind] = []
        gt_to_fg_roi_inds[gt_ind].append(ind)
        all_fg_roi_inds.append(ind)
    all_fg_roi_inds = np.array(list(set(all_fg_roi_inds)))
    pos_rels = []
    for rel in gt_rels:
        for sub_i in gt_to_fg_roi_inds[rel[0]]:
            for obj_i in gt_to_fg_roi_inds[rel[1]]:
                pos_rels.append([sub_i, obj_i, rel[2]])
    rels = []
    rels_inds = []
    roi_inds = []
    if (len(pos_rels) > 0):
        (_, indices) = np.unique(['{} {}'.format(i, j) for (i, j, k) in pos_rels], return_index=True)
        pos_rels = np.array(pos_rels)[indices, :]
        for rel in pos_rels:
            roi_inds += rel[:2].tolist()
            roi_inds = list(set(roi_inds))
            rels.append(rel)
            rels_inds.append(rel[:2].tolist())
            if (len(roi_inds) >= num_fg_rois):
                break
    roi_candidates = np.setdiff1d(all_fg_roi_inds, roi_inds)
    num_rois_to_sample = min((num_fg_rois - len(roi_inds)), len(roi_candidates))
    if (num_rois_to_sample > 0):
        roi_sample = npr.choice(roi_candidates, size=num_rois_to_sample, replace=False)
        roi_inds = np.hstack([roi_inds, roi_sample])
    sample_rels = []
    sample_rels_inds = []
    for i in roi_inds:
        for j in roi_inds:
            if ((i != j) and ([i, j] not in rels_inds)):
                sample_rels.append([i, j, 0])
                sample_rels_inds.append([i, j])
    if (len(sample_rels) > 0):
        num_neg_rels = np.minimum(len(sample_rels), num_neg_rels)
        inds = npr.choice(np.arange(len(sample_rels)), size=num_neg_rels, replace=False)
        rels += [sample_rels[i] for i in inds]
        rels_inds += [sample_rels_inds[i] for i in inds]
    num_rois_to_sample = (num_rois - len(roi_inds))
    if (num_rois_to_sample > 0):
        bg_roi_inds = _sample_bg_rois(roidb, num_rois_to_sample)
        roi_inds = np.hstack([roi_inds, bg_roi_inds])
    roi_inds = np.array(roi_inds).astype(np.int64)
    return (roi_inds.astype(np.int64), np.array(rels).astype(np.int64))
Sample a graph from the foreground rois of an image

roidb: roidb of an image
rois_per_image: maximum number of rois per image
def _sample_graph(roidb, num_fg_rois, num_rois, num_neg_rels=128):
    '\n    Sample a graph from the foreground rois of an image\n\n    roidb: roidb of an image\n    rois_per_image: maximum number of rois per image\n    '
    gt_rels = roidb['gt_relations']
    fg_gt_ind_assignments = roidb['fg_gt_ind_assignments']
    gt_to_fg_roi_inds = {}
    all_fg_roi_inds = []
    for (ind, gt_ind) in fg_gt_ind_assignments.items():
        if (gt_ind not in gt_to_fg_roi_inds):
            gt_to_fg_roi_inds[gt_ind] = []
        gt_to_fg_roi_inds[gt_ind].append(ind)
        all_fg_roi_inds.append(ind)
    all_fg_roi_inds = np.array(list(set(all_fg_roi_inds)))
    pos_rels = []
    for rel in gt_rels:
        for sub_i in gt_to_fg_roi_inds[rel[0]]:
            for obj_i in gt_to_fg_roi_inds[rel[1]]:
                pos_rels.append([sub_i, obj_i, rel[2]])
    rels = []
    rels_inds = []
    roi_inds = []
    if (len(pos_rels) > 0):
        (_, indices) = np.unique(['{} {}'.format(i, j) for (i, j, k) in pos_rels], return_index=True)
        pos_rels = np.array(pos_rels)[indices, :]
        for rel in pos_rels:
            roi_inds += rel[:2].tolist()
            roi_inds = list(set(roi_inds))
            rels.append(rel)
            rels_inds.append(rel[:2].tolist())
            if (len(roi_inds) >= num_fg_rois):
                break
    roi_candidates = np.setdiff1d(all_fg_roi_inds, roi_inds)
    num_rois_to_sample = min((num_fg_rois - len(roi_inds)), len(roi_candidates))
    if (num_rois_to_sample > 0):
        roi_sample = npr.choice(roi_candidates, size=num_rois_to_sample, replace=False)
        roi_inds = np.hstack([roi_inds, roi_sample])
    sample_rels = []
    sample_rels_inds = []
    for i in roi_inds:
        for j in roi_inds:
            if ((i != j) and ([i, j] not in rels_inds)):
                sample_rels.append([i, j, 0])
                sample_rels_inds.append([i, j])
    if (len(sample_rels) > 0):
        num_neg_rels = np.minimum(len(sample_rels), num_neg_rels)
        inds = npr.choice(np.arange(len(sample_rels)), size=num_neg_rels, replace=False)
        rels += [sample_rels[i] for i in inds]
        rels_inds += [sample_rels_inds[i] for i in inds]
    num_rois_to_sample = (num_rois - len(roi_inds))
    if (num_rois_to_sample > 0):
        bg_roi_inds = _sample_bg_rois(roidb, num_rois_to_sample)
        roi_inds = np.hstack([roi_inds, bg_roi_inds])
    roi_inds = np.array(roi_inds).astype(np.int64)
    return (roi_inds.astype(np.int64), np.array(rels).astype(np.int64))