class_docstr: Anchor-free head used in `FCOS <https://arxiv.org/abs/1904.01355>`_.
  
  The FCOS head does not use anchor boxes. Instead bounding boxes are
  predicted at each pixel and a centerness measure is used to supress
  low-quality predictions.
  Here norm_on_bbox, centerness_on_reg, dcn_on_last_conv are training
  tricks used in official repo, which will bring remarkable mAP gains
  of up to 4.9. Please see https://github.com/tianzhi0549/FCOS for
  more detail.
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      strides (list[int] | list[tuple[int, int]]): Strides of points
          in multiple feature levels. Default: (4, 8, 16, 32, 64).
      regress_ranges (tuple[tuple[int, int]]): Regress range of multiple
          level points.
      center_sampling (bool): If true, use center sampling. Default: False.
      center_sample_radius (float): Radius of center sampling. Default: 1.5.
      norm_on_bbox (bool): If true, normalize the regression targets
          with FPN strides. Default: False.
      centerness_on_reg (bool): If true, position centerness on the
          regress branch. Please refer to https://github.com/tianzhi0549/FCOS/issues/89#issuecomment-516877042.
          Default: False.
      conv_bias (bool | str): If specified as `auto`, it will be decided by the
          norm_cfg. Bias of conv will be set as True if `norm_cfg` is None, otherwise
          False. Default: "auto".
      loss_cls (dict): Config of classification loss.
      loss_bbox (dict): Config of localization loss.
      loss_centerness (dict): Config of centerness loss.
      norm_cfg (dict): dictionary to construct and config norm layer.
          Default: norm_cfg=dict(type='GN', num_groups=32, requires_grad=True).
  
  Example:
      >>> self = FCOSHead(11, 7)
      >>> feats = [torch.rand(1, 7, s, s) for s in [4, 8, 16, 32, 64]]
      >>> cls_score, bbox_pred, centerness = self.forward(feats)
      >>> assert len(cls_score) == len(self.scales)
class_docstr_tok: ["Anchor-free","head","used","in","`","FCOS","<","https",":","\/\/arxiv.org\/abs\/1904.01355",">","`","_",".","The","FCOS","head","does","not","use","anchor","boxes",".","Instead","bounding","boxes","are","predicted","at","each","pixel","and","a","centerness","measure","is","used","to","supress","low-quality","predictions",".","Here","norm_on_bbox",",","centerness_on_reg",",","dcn_on_last_conv","are","training","tricks","used","in","official","repo",",","which","will","bring","remarkable","mAP","gains","of","up","to","4.9",".","Please","see","https",":","\/\/github.com\/tianzhi0549\/FCOS","for","more","detail",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","strides","(","list","[","int","]","|","list","[","tuple","[","int",",","int","]","]",")",":","Strides","of","points","in","multiple","feature","levels",".","Default",":","(","4",",","8",",","16",",","32",",","64",")",".","regress_ranges","(","tuple","[","tuple","[","int",",","int","]","]",")",":","Regress","range","of","multiple","level","points",".","center_sampling","(","bool",")",":","If","true",",","use","center","sampling",".","Default",":","False",".","center_sample_radius","(","float",")",":","Radius","of","center","sampling",".","Default",":","1.5.","norm_on_bbox","(","bool",")",":","If","true",",","normalize","the","regression","targets","with","FPN","strides",".","Default",":","False",".","centerness_on_reg","(","bool",")",":","If","true",",","position","centerness","on","the","regress","branch",".","Please","refer","to","https",":","\/\/github.com\/tianzhi0549\/FCOS\/issues\/89","#","issuecomment-516877042",".","Default",":","False",".","conv_bias","(","bool","|","str",")",":","If","specified","as","`","auto","`",",","it","will","be","decided","by","the","norm_cfg",".","Bias","of","conv","will","be","set","as","True","if","`","norm_cfg","`","is","None",",","otherwise","False",".","Default",":","``","auto","''",".","loss_cls","(","dict",")",":","Config","of","classification","loss",".","loss_bbox","(","dict",")",":","Config","of","localization","loss",".","loss_centerness","(","dict",")",":","Config","of","centerness","loss",".","norm_cfg","(","dict",")",":","dictionary","to","construct","and","config","norm","layer",".","Default",":","norm_cfg=dict","(","type='GN","'",",","num_groups=32",",","requires_grad=True",")",".","Example",":",">",">",">","self","=","FCOSHead","(","11",",","7",")",">",">",">","feats","=","[","torch.rand","(","1",",","7",",","s",",","s",")","for","s","in","[","4",",","8",",","16",",","32",",","64","]","]",">",">",">","cls_score",",","bbox_pred",",","centerness","=","self.forward","(","feats",")",">",">",">","assert","len","(","cls_score",")","==","len","(","self.scales",")"]
docstr: Compute loss of the head.
  
  Args:
      cls_scores (list[Tensor]): Box scores for each scale level,
          each is a 4D-tensor, the channel number is
          num_points * num_classes.
      bbox_preds (list[Tensor]): Box energies / deltas for each scale
          level, each is a 4D-tensor, the channel number is
          num_points * 4.
      centernesses (list[Tensor]): Centerss for each scale level, each
          is a 4D-tensor, the channel number is num_points * 1.
      gt_bboxes (list[Tensor]): Ground truth bboxes for each image with
          shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
      gt_labels (list[Tensor]): class indices corresponding to each box
      img_metas (list[dict]): Meta information of each image, e.g.,
          image size, scaling factor, etc.
      gt_bboxes_ignore (None | list[Tensor]): specify which bounding
          boxes can be ignored when computing the loss.
  
  Returns:
      dict[str, Tensor]: A dictionary of loss components.
docstr_tok: ["Compute","loss","of","the","head",".","Args",":","cls_scores","(","list","[","Tensor","]",")",":","Box","scores","for","each","scale","level",",","each","is","a","4D-tensor",",","the","channel","number","is","num_points","*","num_classes",".","bbox_preds","(","list","[","Tensor","]",")",":","Box","energies","\/","deltas","for","each","scale","level",",","each","is","a","4D-tensor",",","the","channel","number","is","num_points","*","4.","centernesses","(","list","[","Tensor","]",")",":","Centerss","for","each","scale","level",",","each","is","a","4D-tensor",",","the","channel","number","is","num_points","*","1.","gt_bboxes","(","list","[","Tensor","]",")",":","Ground","truth","bboxes","for","each","image","with","shape","(","num_gts",",","4",")","in","[","tl_x",",","tl_y",",","br_x",",","br_y","]","format",".","gt_labels","(","list","[","Tensor","]",")",":","class","indices","corresponding","to","each","box","img_metas","(","list","[","dict","]",")",":","Meta","information","of","each","image",",","e.g.",",","image","size",",","scaling","factor",",","etc",".","gt_bboxes_ignore","(","None","|","list","[","Tensor","]",")",":","specify","which","bounding","boxes","can","be","ignored","when","computing","the","loss",".","Returns",":","dict","[","str",",","Tensor","]",":","A","dictionary","of","loss","components","."]
code: def loss(
      self,
      cls_scores,
      bbox_preds,
      centernesses,
      gt_bboxes,
      gt_labels,
      img_metas,
      gt_bboxes_ignore=None,
  ):
      """Compute loss of the head.
  
      Args:
          cls_scores (list[Tensor]): Box scores for each scale level,
              each is a 4D-tensor, the channel number is
              num_points * num_classes.
          bbox_preds (list[Tensor]): Box energies / deltas for each scale
              level, each is a 4D-tensor, the channel number is
              num_points * 4.
          centernesses (list[Tensor]): Centerss for each scale level, each
              is a 4D-tensor, the channel number is num_points * 1.
          gt_bboxes (list[Tensor]): Ground truth bboxes for each image with
              shape (num_gts, 4) in [tl_x, tl_y, br_x, br_y] format.
          gt_labels (list[Tensor]): class indices corresponding to each box
          img_metas (list[dict]): Meta information of each image, e.g.,
              image size, scaling factor, etc.
          gt_bboxes_ignore (None | list[Tensor]): specify which bounding
              boxes can be ignored when computing the loss.
  
      Returns:
          dict[str, Tensor]: A dictionary of loss components.
      """
      assert len(cls_scores) == len(bbox_preds) == len(centernesses)
      featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
      all_level_points = self.get_points(
          featmap_sizes, bbox_preds[0].dtype, bbox_preds[0].device
      )
      labels, bbox_targets = self.get_targets(
          all_level_points, gt_bboxes, gt_labels
      )
  
      num_imgs = cls_scores[0].size(0)
      # flatten cls_scores, bbox_preds and centerness
      flatten_cls_scores = [
          cls_score.permute(0, 2, 3, 1).reshape(
              -1, self.cls_out_channels
          )
          for cls_score in cls_scores
      ]
      flatten_bbox_preds = [
          bbox_pred.permute(0, 2, 3, 1).reshape(-1, 4)
          for bbox_pred in bbox_preds
      ]
      flatten_centerness = [
          centerness.permute(0, 2, 3, 1).reshape(-1)
          for centerness in centernesses
      ]
      flatten_cls_scores = torch.cat(flatten_cls_scores)
      flatten_bbox_preds = torch.cat(flatten_bbox_preds)
      flatten_centerness = torch.cat(flatten_centerness)
      flatten_labels = torch.cat(labels)
      flatten_bbox_targets = torch.cat(bbox_targets)
      # repeat points to align with bbox_preds
      flatten_points = torch.cat(
          [points.repeat(num_imgs, 1) for points in all_level_points]
      )
  
      # FG cat_id: [0, num_classes -1], BG cat_id: num_classes
      bg_class_ind = self.num_classes
      pos_inds = (
          ((flatten_labels >= 0) & (flatten_labels < bg_class_ind))
          .nonzero()
          .reshape(-1)
      )
      num_pos = len(pos_inds)
      loss_cls = self.loss_cls(
          flatten_cls_scores,
          flatten_labels,
          avg_factor=num_pos + num_imgs,
      )  # avoid num_pos is 0
  
      pos_bbox_preds = flatten_bbox_preds[pos_inds]
      pos_centerness = flatten_centerness[pos_inds]
  
      if num_pos > 0:
          pos_bbox_targets = flatten_bbox_targets[pos_inds]
          pos_centerness_targets = self.centerness_target(
              pos_bbox_targets
          )
          pos_points = flatten_points[pos_inds]
          pos_decoded_bbox_preds = distance2bbox(
              pos_points, pos_bbox_preds
          )
          pos_decoded_target_preds = distance2bbox(
              pos_points, pos_bbox_targets
          )
          # centerness weighted iou loss
          loss_bbox = self.loss_bbox(
              pos_decoded_bbox_preds,
              pos_decoded_target_preds,
              weight=pos_centerness_targets,
              avg_factor=pos_centerness_targets.sum(),
          )
          loss_centerness = self.loss_centerness(
              pos_centerness, pos_centerness_targets
          )
      else:
          loss_bbox = pos_bbox_preds.sum()
          loss_centerness = pos_centerness.sum()
  
      return dict(
          loss_cls=loss_cls,
          loss_bbox=loss_bbox,
          loss_centerness=loss_centerness,
      )
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Scale, normal_init
  from mmcv.runner import force_fp32
  from mmdet.core import distance2bbox, multi_apply, multiclass_nms
  from ..builder import HEADS, build_loss
  from .anchor_free_head import AnchorFreeHead
label_desc: Use image node_id 6 for calling the FCOSHead obj's underlying member method code with example usage: obj.loss(cls_scores, bbox_preds, centernesses, gt_bboxes, gt_labels, img_metas, gt_bboxes_ignore) and returns: dict