(gt_bboxes_list,
                  featmap_sizes,
                  anchor_scale,
                  anchor_strides,
                  center_ratio=0.2,
                  ignore_ratio=0.5)
return all_loc_targets, all_loc_weights, loc_avg_factor
Compute location targets for guided anchoring.

    Each feature map is divided into positive, negative and ignore regions.
    - positive regions: target 1, weight 1
    - ignore regions: target 0, weight 0
    - negative regions: target 0, weight 0.1

    Args:
        gt_bboxes_list (list[Tensor]): Gt bboxes of each image.
        featmap_sizes (list[tuple]): Multi level sizes of each feature maps.
        anchor_scale (int): Anchor scale.
        anchor_strides ([list[int]]): Multi level anchor strides.
        center_ratio (float): Ratio of center region.
        ignore_ratio (float): Ratio of ignore region.

    Returns:
        tuple
Compute location targets for guided anchoring.
Compute location targets for guided anchoring .
def ga_loc_target(gt_bboxes_list,
                  featmap_sizes,
                  anchor_scale,
                  anchor_strides,
                  center_ratio=0.2,
                  ignore_ratio=0.5):
    """Compute location targets for guided anchoring.

    Each feature map is divided into positive, negative and ignore regions.
    - positive regions: target 1, weight 1
    - ignore regions: target 0, weight 0
    - negative regions: target 0, weight 0.1

    Args:
        gt_bboxes_list (list[Tensor]): Gt bboxes of each image.
        featmap_sizes (list[tuple]): Multi level sizes of each feature maps.
        anchor_scale (int): Anchor scale.
        anchor_strides ([list[int]]): Multi level anchor strides.
        center_ratio (float): Ratio of center region.
        ignore_ratio (float): Ratio of ignore region.

    Returns:
        tuple
    """
    img_per_gpu = len(gt_bboxes_list)
    num_lvls = len(featmap_sizes)
    r1 = (1 - center_ratio) / 2
    r2 = (1 - ignore_ratio) / 2
    all_loc_targets = []
    all_loc_weights = []
    all_ignore_map = []
    for lvl_id in range(num_lvls):
        h, w = featmap_sizes[lvl_id]
        loc_targets = torch.zeros(
            img_per_gpu,
            1,
            h,
            w,
            device=gt_bboxes_list[0].device,
            dtype=torch.float32)
        loc_weights = torch.full_like(loc_targets, -1)
        ignore_map = torch.zeros_like(loc_targets)
        all_loc_targets.append(loc_targets)
        all_loc_weights.append(loc_weights)
        all_ignore_map.append(ignore_map)
    for img_id in range(img_per_gpu):
        gt_bboxes = gt_bboxes_list[img_id]
        scale = torch.sqrt((gt_bboxes[:, 2] - gt_bboxes[:, 0] + 1) *
                           (gt_bboxes[:, 3] - gt_bboxes[:, 1] + 1))
        min_anchor_size = scale.new_full(
            (1, ), float(anchor_scale * anchor_strides[0]))
        # assign gt bboxes to different feature levels w.r.t. their scales
        target_lvls = torch.floor(
            torch.log2(scale) - torch.log2(min_anchor_size) + 0.5)
        target_lvls = target_lvls.clamp(min=0, max=num_lvls - 1).long()
        for gt_id in range(gt_bboxes.size(0)):
            lvl = target_lvls[gt_id].item()
            # rescaled to corresponding feature map
            gt_ = gt_bboxes[gt_id, :4] / anchor_strides[lvl]
            # calculate ignore regions
            ignore_x1, ignore_y1, ignore_x2, ignore_y2 = calc_region(
                gt_, r2, featmap_sizes[lvl])
            # calculate positive (center) regions
            ctr_x1, ctr_y1, ctr_x2, ctr_y2 = calc_region(
                gt_, r1, featmap_sizes[lvl])
            all_loc_targets[lvl][img_id, 0, ctr_y1:ctr_y2 + 1, ctr_x1:ctr_x2 +
                                 1] = 1
            all_loc_weights[lvl][img_id, 0, ignore_y1:ignore_y2 +
                                 1, ignore_x1:ignore_x2 + 1] = 0
            all_loc_weights[lvl][img_id, 0, ctr_y1:ctr_y2 + 1, ctr_x1:ctr_x2 +
                                 1] = 1
            # calculate ignore map on nearby low level feature
            if lvl > 0:
                d_lvl = lvl - 1
                # rescaled to corresponding feature map
                gt_ = gt_bboxes[gt_id, :4] / anchor_strides[d_lvl]
                ignore_x1, ignore_y1, ignore_x2, ignore_y2 = calc_region(
                    gt_, r2, featmap_sizes[d_lvl])
                all_ignore_map[d_lvl][img_id, 0, ignore_y1:ignore_y2 +
                                      1, ignore_x1:ignore_x2 + 1] = 1
            # calculate ignore map on nearby high level feature
            if lvl < num_lvls - 1:
                u_lvl = lvl + 1
                # rescaled to corresponding feature map
                gt_ = gt_bboxes[gt_id, :4] / anchor_strides[u_lvl]
                ignore_x1, ignore_y1, ignore_x2, ignore_y2 = calc_region(
                    gt_, r2, featmap_sizes[u_lvl])
                all_ignore_map[u_lvl][img_id, 0, ignore_y1:ignore_y2 +
                                      1, ignore_x1:ignore_x2 + 1] = 1
    for lvl_id in range(num_lvls):
        # ignore negative regions w.r.t. ignore map
        all_loc_weights[lvl_id][(all_loc_weights[lvl_id] < 0)
                                & (all_ignore_map[lvl_id] > 0)] = 0
        # set negative regions with weight 0.1
        all_loc_weights[lvl_id][all_loc_weights[lvl_id] < 0] = 0.1
    # loc average factor to balance loss
    loc_avg_factor = sum(
        [t.size(0) * t.size(-1) * t.size(-2) for t in all_loc_targets]) / 200
    return all_loc_targets, all_loc_weights, loc_avg_factor
def ga_loc_target ( gt_bboxes_list , featmap_sizes , anchor_scale , anchor_strides , center_ratio = 0.2 , ignore_ratio = 0.5 ) : img_per_gpu = len ( gt_bboxes_list ) num_lvls = len ( featmap_sizes ) r1 = ( 1 - center_ratio ) / 2 r2 = ( 1 - ignore_ratio ) / 2 all_loc_targets = [ ] all_loc_weights = [ ] all_ignore_map = [ ] for lvl_id in range ( num_lvls ) : h , w = featmap_sizes [ lvl_id ] loc_targets = torch . zeros ( img_per_gpu , 1 , h , w , device = gt_bboxes_list [ 0 ] . device , dtype = torch . float32 ) loc_weights = torch . full_like ( loc_targets , - 1 ) ignore_map = torch . zeros_like ( loc_targets ) all_loc_targets . append ( loc_targets ) all_loc_weights . append ( loc_weights ) all_ignore_map . append ( ignore_map ) for img_id in range ( img_per_gpu ) : gt_bboxes = gt_bboxes_list [ img_id ] scale = torch . sqrt ( ( gt_bboxes [ : , 2 ] - gt_bboxes [ : , 0 ] + 1 ) * ( gt_bboxes [ : , 3 ] - gt_bboxes [ : , 1 ] + 1 ) ) min_anchor_size = scale . new_full ( ( 1 , ) , float ( anchor_scale * anchor_strides [ 0 ] ) ) # assign gt bboxes to different feature levels w.r.t. their scales target_lvls = torch . floor ( torch . log2 ( scale ) - torch . log2 ( min_anchor_size ) + 0.5 ) target_lvls = target_lvls . clamp ( min = 0 , max = num_lvls - 1 ) . long ( ) for gt_id in range ( gt_bboxes . size ( 0 ) ) : lvl = target_lvls [ gt_id ] . item ( ) # rescaled to corresponding feature map gt_ = gt_bboxes [ gt_id , : 4 ] / anchor_strides [ lvl ] # calculate ignore regions ignore_x1 , ignore_y1 , ignore_x2 , ignore_y2 = calc_region ( gt_ , r2 , featmap_sizes [ lvl ] ) # calculate positive (center) regions ctr_x1 , ctr_y1 , ctr_x2 , ctr_y2 = calc_region ( gt_ , r1 , featmap_sizes [ lvl ] ) all_loc_targets [ lvl ] [ img_id , 0 , ctr_y1 : ctr_y2 + 1 , ctr_x1 : ctr_x2 + 1 ] = 1 all_loc_weights [ lvl ] [ img_id , 0 , ignore_y1 : ignore_y2 + 1 , ignore_x1 : ignore_x2 + 1 ] = 0 all_loc_weights [ lvl ] [ img_id , 0 , ctr_y1 : ctr_y2 + 1 , ctr_x1 : ctr_x2 + 1 ] = 1 # calculate ignore map on nearby low level feature if lvl > 0 : d_lvl = lvl - 1 # rescaled to corresponding feature map gt_ = gt_bboxes [ gt_id , : 4 ] / anchor_strides [ d_lvl ] ignore_x1 , ignore_y1 , ignore_x2 , ignore_y2 = calc_region ( gt_ , r2 , featmap_sizes [ d_lvl ] ) all_ignore_map [ d_lvl ] [ img_id , 0 , ignore_y1 : ignore_y2 + 1 , ignore_x1 : ignore_x2 + 1 ] = 1 # calculate ignore map on nearby high level feature if lvl < num_lvls - 1 : u_lvl = lvl + 1 # rescaled to corresponding feature map gt_ = gt_bboxes [ gt_id , : 4 ] / anchor_strides [ u_lvl ] ignore_x1 , ignore_y1 , ignore_x2 , ignore_y2 = calc_region ( gt_ , r2 , featmap_sizes [ u_lvl ] ) all_ignore_map [ u_lvl ] [ img_id , 0 , ignore_y1 : ignore_y2 + 1 , ignore_x1 : ignore_x2 + 1 ] = 1 for lvl_id in range ( num_lvls ) : # ignore negative regions w.r.t. ignore map all_loc_weights [ lvl_id ] [ ( all_loc_weights [ lvl_id ] < 0 ) & ( all_ignore_map [ lvl_id ] > 0 ) ] = 0 # set negative regions with weight 0.1 all_loc_weights [ lvl_id ] [ all_loc_weights [ lvl_id ] < 0 ] = 0.1 # loc average factor to balance loss loc_avg_factor = sum ( [ t . size ( 0 ) * t . size ( - 1 ) * t . size ( - 2 ) for t in all_loc_targets ] ) / 200 return all_loc_targets , all_loc_weights , loc_avg_factor