class_docstr: Guided-Anchor-based head (GA-RPN, GA-RetinaNet, etc.).
  
  This GuidedAnchorHead will predict high-quality feature guided
  anchors and locations where anchors will be kept in inference.
  There are mainly 3 categories of bounding-boxes.
  
  - Sampled 9 pairs for target assignment. (approxes)
  - The square boxes where the predicted anchors are based on. (squares)
  - Guided anchors.
  
  Please refer to https://arxiv.org/abs/1901.03278 for more details.
  
  Args:
      num_classes (int): Number of classes.
      in_channels (int): Number of channels in the input feature map.
      feat_channels (int): Number of hidden channels. Defaults to 256.
      approx_anchor_generator (:obj:`ConfigDict` or dict): Config dict
          for approx generator
      square_anchor_generator (:obj:`ConfigDict` or dict): Config dict
          for square generator
      anchor_coder (:obj:`ConfigDict` or dict): Config dict for anchor coder
      bbox_coder (:obj:`ConfigDict` or dict): Config dict for bbox coder
      reg_decoded_bbox (bool): If true, the regression loss would be
          applied directly on decoded bounding boxes, converting both
          the predicted boxes and regression targets to absolute
          coordinates format. Defaults to False. It should be `True` when
          using `IoULoss`, `GIoULoss`, or `DIoULoss` in the bbox head.
      deform_groups: (int): Group number of DCN in FeatureAdaption module.
          Defaults to 4.
      loc_filter_thr (float): Threshold to filter out unconcerned regions.
          Defaults to 0.01.
      loss_loc (:obj:`ConfigDict` or dict): Config of location loss.
      loss_shape (:obj:`ConfigDict` or dict): Config of anchor shape loss.
      loss_cls (:obj:`ConfigDict` or dict): Config of classification loss.
      loss_bbox (:obj:`ConfigDict` or dict): Config of bbox regression loss.
      init_cfg (:obj:`ConfigDict` or list[:obj:`ConfigDict`] or dict or             list[dict], optional): Initialization config dict.
class_docstr_tok: ["Guided-Anchor-based","head","(","GA-RPN",",","GA-RetinaNet",",","etc.",")",".","This","GuidedAnchorHead","will","predict","high-quality","feature","guided","anchors","and","locations","where","anchors","will","be","kept","in","inference",".","There","are","mainly","3","categories","of","bounding-boxes",".","-","Sampled","9","pairs","for","target","assignment",".","(","approxes",")","-","The","square","boxes","where","the","predicted","anchors","are","based","on",".","(","squares",")","-","Guided","anchors",".","Please","refer","to","https",":","\/\/arxiv.org\/abs\/1901.03278","for","more","details",".","Args",":","num_classes","(","int",")",":","Number","of","classes",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","feat_channels","(","int",")",":","Number","of","hidden","channels",".","Defaults","to","256.","approx_anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","approx","generator","square_anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","square","generator","anchor_coder","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","anchor","coder","bbox_coder","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","dict","for","bbox","coder","reg_decoded_bbox","(","bool",")",":","If","true",",","the","regression","loss","would","be","applied","directly","on","decoded","bounding","boxes",",","converting","both","the","predicted","boxes","and","regression","targets","to","absolute","coordinates","format",".","Defaults","to","False",".","It","should","be","`","True","`","when","using","`","IoULoss","`",",","`","GIoULoss","`",",","or","`","DIoULoss","`","in","the","bbox","head",".","deform_groups",":","(","int",")",":","Group","number","of","DCN","in","FeatureAdaption","module",".","Defaults","to","4.","loc_filter_thr","(","float",")",":","Threshold","to","filter","out","unconcerned","regions",".","Defaults","to","0.01.","loss_loc","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","location","loss",".","loss_shape","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","anchor","shape","loss",".","loss_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","classification","loss",".","loss_bbox","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","bbox","regression","loss",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","list","[",":","obj",":","`","ConfigDict","`","]","or","dict","or","list","[","dict","]",",","optional",")",":","Initialization","config","dict","."]
docstr: 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:
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``bboxes`` and ``labels``
          attributes.
      featmap_sizes (list[tuple]): Multi level sizes of each feature
          maps.
  
  Returns:
      tuple: Returns a tuple containing location targets.
docstr_tok: ["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",":","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``","and","``","labels","``","attributes",".","featmap_sizes","(","list","[","tuple","]",")",":","Multi","level","sizes","of","each","feature","maps",".","Returns",":","tuple",":","Returns","a","tuple","containing","location","targets","."]
code: def ga_loc_targets(
      self,
      batch_gt_instances: InstanceList,
      featmap_sizes: List[Tuple[int, int]],
  ) -> tuple:
      """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:
          batch_gt_instances (list[:obj:`InstanceData`]): Batch of
              gt_instance. It usually includes ``bboxes`` and ``labels``
              attributes.
          featmap_sizes (list[tuple]): Multi level sizes of each feature
              maps.
  
      Returns:
          tuple: Returns a tuple containing location targets.
      """
      anchor_scale = self.approx_anchor_generator.octave_base_scale
      anchor_strides = self.approx_anchor_generator.strides
      # Currently only supports same stride in x and y direction.
      for stride in anchor_strides:
          assert stride[0] == stride[1]
      anchor_strides = [stride[0] for stride in anchor_strides]
  
      center_ratio = self.train_cfg["center_ratio"]
      ignore_ratio = self.train_cfg["ignore_ratio"]
      img_per_gpu = len(batch_gt_instances)
      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=batch_gt_instances[0].bboxes.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 = batch_gt_instances[img_id].bboxes
          scale = torch.sqrt(
              (gt_bboxes[:, 2] - gt_bboxes[:, 0])
              * (gt_bboxes[:, 3] - gt_bboxes[:, 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
  
imports: from typing import List, Optional, Tuple
  import torch
  import torch.nn
  from mmcv.ops import DeformConv2d, MaskedConv2d
  from mmengine.model import BaseModule
  from mmengine.structures import InstanceData
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptConfigType, OptInstanceList
  from ..layers import multiclass_nms
  from ..task_modules.prior_generators import anchor_inside_flags, calc_region
  from ..task_modules.samplers import PseudoSampler
  from ..utils import images_to_levels, multi_apply, unmap
  from .anchor_head import AnchorHead
label_desc: Use image node_id 8 for calling the GuidedAnchorHead obj's underlying member method code with example usage: obj.ga_loc_targets(batch_gt_instances, featmap_sizes) and returns: all_loc_targets,  all_loc_weights,  loc_avg_factor