def add_bbox_regression_targets(roidb):
    'Add information needed to train bounding-box regressors.'
    assert (len(roidb) > 0)
    assert ('max_classes' in roidb[0]), 'Did you call prepare_roidb first?'
    num_images = len(roidb)
    num_classes = roidb[0]['gt_overlaps'].shape[1]
    for im_i in xrange(num_images):
        rois = roidb[im_i]['boxes']
        max_overlaps = roidb[im_i]['max_overlaps']
        max_classes = roidb[im_i]['max_classes']
        roidb[im_i]['bbox_targets'] = _compute_targets(rois, max_overlaps, max_classes)
    class_counts = (np.zeros((num_classes, 1)) + cfg.EPS)
    sums = np.zeros((num_classes, 4))
    squared_sums = np.zeros((num_classes, 4))
    for im_i in xrange(num_images):
        targets = roidb[im_i]['bbox_targets']
        for cls in xrange(1, num_classes):
            cls_inds = np.where((targets[:, 0] == cls))[0]
            if (cls_inds.size > 0):
                class_counts[cls] += cls_inds.size
                sums[cls, :] += targets[cls_inds, 1:].sum(axis=0)
                squared_sums[cls, :] += (targets[cls_inds, 1:] ** 2).sum(axis=0)
    means = (sums / class_counts)
    stds = np.sqrt(((squared_sums / class_counts) - (means ** 2)))
    for im_i in xrange(num_images):
        targets = roidb[im_i]['bbox_targets']
        for cls in xrange(1, num_classes):
            cls_inds = np.where((targets[:, 0] == cls))[0]
            roidb[im_i]['bbox_targets'][cls_inds, 1:] -= means[cls, :]
            roidb[im_i]['bbox_targets'][cls_inds, 1:] /= stds[cls, :]
    return (means.ravel(), stds.ravel())
Add information needed to train bounding-box regressors.
def add_bbox_regression_targets(roidb):
    
    assert (len(roidb) > 0)
    assert ('max_classes' in roidb[0]), 'Did you call prepare_roidb first?'
    num_images = len(roidb)
    num_classes = roidb[0]['gt_overlaps'].shape[1]
    for im_i in xrange(num_images):
        rois = roidb[im_i]['boxes']
        max_overlaps = roidb[im_i]['max_overlaps']
        max_classes = roidb[im_i]['max_classes']
        roidb[im_i]['bbox_targets'] = _compute_targets(rois, max_overlaps, max_classes)
    class_counts = (np.zeros((num_classes, 1)) + cfg.EPS)
    sums = np.zeros((num_classes, 4))
    squared_sums = np.zeros((num_classes, 4))
    for im_i in xrange(num_images):
        targets = roidb[im_i]['bbox_targets']
        for cls in xrange(1, num_classes):
            cls_inds = np.where((targets[:, 0] == cls))[0]
            if (cls_inds.size > 0):
                class_counts[cls] += cls_inds.size
                sums[cls, :] += targets[cls_inds, 1:].sum(axis=0)
                squared_sums[cls, :] += (targets[cls_inds, 1:] ** 2).sum(axis=0)
    means = (sums / class_counts)
    stds = np.sqrt(((squared_sums / class_counts) - (means ** 2)))
    for im_i in xrange(num_images):
        targets = roidb[im_i]['bbox_targets']
        for cls in xrange(1, num_classes):
            cls_inds = np.where((targets[:, 0] == cls))[0]
            roidb[im_i]['bbox_targets'][cls_inds, 1:] -= means[cls, :]
            roidb[im_i]['bbox_targets'][cls_inds, 1:] /= stds[cls, :]
    return (means.ravel(), stds.ravel())