(roidb)
return means.ravel(), stds.ravel()
given roidb, add ['bbox_targets'] and normalize bounding box regression targets
    :param roidb: roidb to be processed. must have gone through imdb.prepare_roidb
    :return: means, std variances of targets
given roidb, add ['bbox_targets'] and normalize bounding box regression targets
    :param roidb: roidb to be processed. must have gone through imdb.prepare_roidb
    :return: means, std variances of targets
given roidb add [ bbox_targets ] and normalize bounding box regression targets : param roidb : roidb to be processed . must have gone through imdb . prepare_roidb : return : means std variances of targets
def add_bbox_regression_targets(roidb):
    """
    given roidb, add ['bbox_targets'] and normalize bounding box regression targets
    :param roidb: roidb to be processed. must have gone through imdb.prepare_roidb
    :return: means, std variances of targets
    """
    print('add bounding box regression targets')
    assert len(roidb) > 0
    assert 'max_classes' in roidb[0]

    num_images = len(roidb)
    num_classes = 2 if config.network.class_agnostic else roidb[0]['gt_overlaps'].shape[1]

    for im_i in range(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_bbox_regression_targets(rois, max_overlaps, max_classes, config)

    if config.train.bbox_normalization_precomputed:
        # use fixed / precomputed means and stds instead of empirical values
        means = np.tile(np.array(config.train.bbox_means), (num_classes, 1))
        stds = np.tile(np.array(config.train.bbox_stds), (num_classes, 1))
    else:
        # compute mean, std values
        class_counts = np.zeros((num_classes, 1)) + 1e-14
        sums = np.zeros((num_classes, 4))
        squared_sums = np.zeros((num_classes, 4))
        for im_i in range(num_images):
            targets = roidb[im_i]['bbox_targets']
            for cls in range(1, num_classes):
                cls_indexes = np.where(targets[:, 0] > 0)[0] if config.network.class_agnostic else np.where(targets[:, 0] == cls)[0]
                if cls_indexes.size > 0:
                    class_counts[cls] += cls_indexes.size
                    sums[cls, :] += targets[cls_indexes, 1:].sum(axis=0)
                    squared_sums[cls, :] += (targets[cls_indexes, 1:] ** 2).sum(axis=0)

        means = sums / class_counts
        # var(x) = E(x^2) - E(x)^2
        stds = np.sqrt(squared_sums / class_counts - means ** 2)

    print('bbox target means:')
    print(means)
    print(means[1:, :].mean(axis=0))  # ignore bg class
    print('bbox target stdevs:')
    print(stds)
    print(stds[1:, :].mean(axis=0))  # ignore bg class


    # normalized targets
    for im_i in range(num_images):
        targets = roidb[im_i]['bbox_targets']
        for cls in range(1, num_classes):
            cls_indexes = np.where(targets[:, 0] > 0) if config.network.class_agnostic else np.where(targets[:, 0] == cls)[0]
            roidb[im_i]['bbox_targets'][cls_indexes, 1:] -= means[cls, :]
            roidb[im_i]['bbox_targets'][cls_indexes, 1:] /= stds[cls, :]

    return means.ravel(), stds.ravel()
def add_bbox_regression_targets ( roidb ) : print ( 'add bounding box regression targets' ) assert len ( roidb ) > 0 assert 'max_classes' in roidb [ 0 ] num_images = len ( roidb ) num_classes = 2 if config . network . class_agnostic else roidb [ 0 ] [ 'gt_overlaps' ] . shape [ 1 ] for im_i in range ( 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_bbox_regression_targets ( rois , max_overlaps , max_classes , config ) if config . train . bbox_normalization_precomputed : # use fixed / precomputed means and stds instead of empirical values means = np . tile ( np . array ( config . train . bbox_means ) , ( num_classes , 1 ) ) stds = np . tile ( np . array ( config . train . bbox_stds ) , ( num_classes , 1 ) ) else : # compute mean, std values class_counts = np . zeros ( ( num_classes , 1 ) ) + 1e-14 sums = np . zeros ( ( num_classes , 4 ) ) squared_sums = np . zeros ( ( num_classes , 4 ) ) for im_i in range ( num_images ) : targets = roidb [ im_i ] [ 'bbox_targets' ] for cls in range ( 1 , num_classes ) : cls_indexes = np . where ( targets [ : , 0 ] > 0 ) [ 0 ] if config . network . class_agnostic else np . where ( targets [ : , 0 ] == cls ) [ 0 ] if cls_indexes . size > 0 : class_counts [ cls ] += cls_indexes . size sums [ cls , : ] += targets [ cls_indexes , 1 : ] . sum ( axis = 0 ) squared_sums [ cls , : ] += ( targets [ cls_indexes , 1 : ] ** 2 ) . sum ( axis = 0 ) means = sums / class_counts # var(x) = E(x^2) - E(x)^2 stds = np . sqrt ( squared_sums / class_counts - means ** 2 ) print ( 'bbox target means:' ) print ( means ) print ( means [ 1 : , : ] . mean ( axis = 0 ) ) # ignore bg class print ( 'bbox target stdevs:' ) print ( stds ) print ( stds [ 1 : , : ] . mean ( axis = 0 ) ) # ignore bg class # normalized targets for im_i in range ( num_images ) : targets = roidb [ im_i ] [ 'bbox_targets' ] for cls in range ( 1 , num_classes ) : cls_indexes = np . where ( targets [ : , 0 ] > 0 ) if config . network . class_agnostic else np . where ( targets [ : , 0 ] == cls ) [ 0 ] roidb [ im_i ] [ 'bbox_targets' ] [ cls_indexes , 1 : ] -= means [ cls , : ] roidb [ im_i ] [ 'bbox_targets' ] [ cls_indexes , 1 : ] /= stds [ cls , : ] return means . ravel ( ) , stds . ravel ( )