class_docstr: Head of `VarifocalNet (VFNet): An IoU-aware Dense Object
  Detector.<https://arxiv.org/abs/2008.13367>`_.
  
  The VFNet predicts IoU-aware classification scores which mix the
  object presence confidence and object localization accuracy as the
  detection score. It is built on the FCOS architecture and uses ATSS
  for defining positive/negative training examples. The VFNet is trained
  with Varifocal Loss and empolys star-shaped deformable convolution to
  extract features for a bbox.
  
  Args:
      num_classes (int): Number of categories excluding the background
          category.
      in_channels (int): Number of channels in the input feature map.
      regress_ranges (Sequence[Tuple[int, int]]): Regress range of multiple
          level points.
      center_sampling (bool): If true, use center sampling. Defaults to False.
      center_sample_radius (float): Radius of center sampling. Defaults to 1.5.
      sync_num_pos (bool): If true, synchronize the number of positive
          examples across GPUs. Defaults to True
      gradient_mul (float): The multiplier to gradients from bbox refinement
          and recognition. Defaults to 0.1.
      bbox_norm_type (str): The bbox normalization type, 'reg_denom' or
          'stride'. Defaults to reg_denom
      loss_cls_fl (:obj:`ConfigDict` or dict): Config of focal loss.
      use_vfl (bool): If true, use varifocal loss for training.
          Defaults to True.
      loss_cls (:obj:`ConfigDict` or dict): Config of varifocal loss.
      loss_bbox (:obj:`ConfigDict` or dict): Config of localization loss,
          GIoU Loss.
      loss_bbox (:obj:`ConfigDict` or dict): Config of localization
          refinement loss, GIoU Loss.
      norm_cfg (:obj:`ConfigDict` or dict): dictionary to construct and
          config norm layer. Defaults to norm_cfg=dict(type='GN',
          num_groups=32, requires_grad=True).
      use_atss (bool): If true, use ATSS to define positive/negative
          examples. Defaults to True.
      anchor_generator (:obj:`ConfigDict` or dict): Config of anchor
          generator for ATSS.
      init_cfg (:obj:`ConfigDict` or dict or list[dict] or
          list[:obj:`ConfigDict`]): Initialization config dict.
  
  Example:
      >>> self = VFNetHead(11, 7)
      >>> feats = [torch.rand(1, 7, s, s) for s in [4, 8, 16, 32, 64]]
      >>> cls_score, bbox_pred, bbox_pred_refine= self.forward(feats)
      >>> assert len(cls_score) == len(self.scales)
class_docstr_tok: ["Head","of","`","VarifocalNet","(","VFNet",")",":","An","IoU-aware","Dense","Object","Detector.","<","https",":","\/\/arxiv.org\/abs\/2008.13367",">","`","_",".","The","VFNet","predicts","IoU-aware","classification","scores","which","mix","the","object","presence","confidence","and","object","localization","accuracy","as","the","detection","score",".","It","is","built","on","the","FCOS","architecture","and","uses","ATSS","for","defining","positive\/negative","training","examples",".","The","VFNet","is","trained","with","Varifocal","Loss","and","empolys","star-shaped","deformable","convolution","to","extract","features","for","a","bbox",".","Args",":","num_classes","(","int",")",":","Number","of","categories","excluding","the","background","category",".","in_channels","(","int",")",":","Number","of","channels","in","the","input","feature","map",".","regress_ranges","(","Sequence","[","Tuple","[","int",",","int","]","]",")",":","Regress","range","of","multiple","level","points",".","center_sampling","(","bool",")",":","If","true",",","use","center","sampling",".","Defaults","to","False",".","center_sample_radius","(","float",")",":","Radius","of","center","sampling",".","Defaults","to","1.5.","sync_num_pos","(","bool",")",":","If","true",",","synchronize","the","number","of","positive","examples","across","GPUs",".","Defaults","to","True","gradient_mul","(","float",")",":","The","multiplier","to","gradients","from","bbox","refinement","and","recognition",".","Defaults","to","0.1.","bbox_norm_type","(","str",")",":","The","bbox","normalization","type",",","'reg_denom","'","or","'stride","'",".","Defaults","to","reg_denom","loss_cls_fl","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","focal","loss",".","use_vfl","(","bool",")",":","If","true",",","use","varifocal","loss","for","training",".","Defaults","to","True",".","loss_cls","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","varifocal","loss",".","loss_bbox","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","localization","loss",",","GIoU","Loss",".","loss_bbox","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","localization","refinement","loss",",","GIoU","Loss",".","norm_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","dictionary","to","construct","and","config","norm","layer",".","Defaults","to","norm_cfg=dict","(","type='GN","'",",","num_groups=32",",","requires_grad=True",")",".","use_atss","(","bool",")",":","If","true",",","use","ATSS","to","define","positive\/negative","examples",".","Defaults","to","True",".","anchor_generator","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","Config","of","anchor","generator","for","ATSS",".","init_cfg","(",":","obj",":","`","ConfigDict","`","or","dict","or","list","[","dict","]","or","list","[",":","obj",":","`","ConfigDict","`","]",")",":","Initialization","config","dict",".","Example",":",">",">",">","self","=","VFNetHead","(","11",",","7",")",">",">",">","feats","=","[","torch.rand","(","1",",","7",",","s",",","s",")","for","s","in","[","4",",","8",",","16",",","32",",","64","]","]",">",">",">","cls_score",",","bbox_pred",",","bbox_pred_refine=","self.forward","(","feats",")",">",">",">","assert","len","(","cls_score",")","==","len","(","self.scales",")"]
code: def __init__(
      self,
      num_classes: int,
      in_channels: int,
      regress_ranges: RangeType = (
          (-1, 64),
          (64, 128),
          (128, 256),
          (256, 512),
          (512, INF),
      ),
      center_sampling: bool = False,
      center_sample_radius: float = 1.5,
      sync_num_pos: bool = True,
      gradient_mul: float = 0.1,
      bbox_norm_type: str = "reg_denom",
      loss_cls_fl: ConfigType = dict(
          type="FocalLoss",
          use_sigmoid=True,
          gamma=2.0,
          alpha=0.25,
          loss_weight=1.0,
      ),
      use_vfl: bool = True,
      loss_cls: ConfigType = dict(
          type="VarifocalLoss",
          use_sigmoid=True,
          alpha=0.75,
          gamma=2.0,
          iou_weighted=True,
          loss_weight=1.0,
      ),
      loss_bbox: ConfigType = dict(type="GIoULoss", loss_weight=1.5),
      loss_bbox_refine: ConfigType = dict(
          type="GIoULoss", loss_weight=2.0
      ),
      norm_cfg: ConfigType = dict(
          type="GN", num_groups=32, requires_grad=True
      ),
      use_atss: bool = True,
      reg_decoded_bbox: bool = True,
      anchor_generator: ConfigType = dict(
          type="AnchorGenerator",
          ratios=[1.0],
          octave_base_scale=8,
          scales_per_octave=1,
          center_offset=0.0,
          strides=[8, 16, 32, 64, 128],
      ),
      init_cfg: MultiConfig = dict(
          type="Normal",
          layer="Conv2d",
          std=0.01,
          override=dict(
              type="Normal", name="vfnet_cls", std=0.01, bias_prob=0.01
          ),
      ),
      **kwargs,
  ) -> None:
      # dcn base offsets, adapted from reppoints_head.py
      self.num_dconv_points = 9
      self.dcn_kernel = int(np.sqrt(self.num_dconv_points))
      self.dcn_pad = int((self.dcn_kernel - 1) / 2)
      dcn_base = np.arange(-self.dcn_pad, self.dcn_pad + 1).astype(
          np.float64
      )
      dcn_base_y = np.repeat(dcn_base, self.dcn_kernel)
      dcn_base_x = np.tile(dcn_base, self.dcn_kernel)
      dcn_base_offset = np.stack(
          [dcn_base_y, dcn_base_x], axis=1
      ).reshape((-1))
      self.dcn_base_offset = torch.tensor(dcn_base_offset).view(
          1, -1, 1, 1
      )
  
      super(FCOSHead, self).__init__(
          num_classes=num_classes,
          in_channels=in_channels,
          norm_cfg=norm_cfg,
          init_cfg=init_cfg,
          **kwargs,
      )
      self.regress_ranges = regress_ranges
      self.reg_denoms = [
          regress_range[-1] for regress_range in regress_ranges
      ]
      self.reg_denoms[-1] = self.reg_denoms[-2] * 2
      self.center_sampling = center_sampling
      self.center_sample_radius = center_sample_radius
      self.sync_num_pos = sync_num_pos
      self.bbox_norm_type = bbox_norm_type
      self.gradient_mul = gradient_mul
      self.use_vfl = use_vfl
      if self.use_vfl:
          self.loss_cls = MODELS.build(loss_cls)
      else:
          self.loss_cls = MODELS.build(loss_cls_fl)
      self.loss_bbox = MODELS.build(loss_bbox)
      self.loss_bbox_refine = MODELS.build(loss_bbox_refine)
  
      # for getting ATSS targets
      self.use_atss = use_atss
      self.reg_decoded_bbox = reg_decoded_bbox
      self.use_sigmoid_cls = loss_cls.get("use_sigmoid", False)
  
      self.anchor_center_offset = anchor_generator["center_offset"]
  
      self.num_base_priors = self.prior_generator.num_base_priors[0]
  
      if self.train_cfg:
          self.assigner = TASK_UTILS.build(self.train_cfg["assigner"])
          if self.train_cfg.get("sampler", None) is not None:
              self.sampler = TASK_UTILS.build(
                  self.train_cfg["sampler"],
                  default_args=dict(context=self),
              )
          else:
              self.sampler = PseudoSampler()
      # only be used in `get_atss_targets` when `use_atss` is True
      self.atss_prior_generator = TASK_UTILS.build(anchor_generator)
  
      self.fcos_prior_generator = MlvlPointGenerator(
          anchor_generator["strides"],
          self.anchor_center_offset if self.use_atss else 0.5,
      )
  
      # In order to reuse the `get_bboxes` in `BaseDenseHead.
      # Only be used in testing phase.
      self.prior_generator = self.fcos_prior_generator
  
imports: from typing import List, Tuple, Union
  import numpy
  import torch
  import torch.nn
  from mmcv.cnn import ConvModule, Scale
  from mmcv.ops import DeformConv2d
  from torch import Tensor
  from mmdet.registry import MODELS, TASK_UTILS
  from mmdet.structures.bbox import bbox_overlaps
  from mmdet.utils import ConfigType, InstanceList, MultiConfig, OptInstanceList, RangeType, reduce_mean
  from ..task_modules.prior_generators import MlvlPointGenerator
  from ..task_modules.samplers import PseudoSampler
  from ..utils import multi_apply
  from .atss_head import ATSSHead
  from .fcos_head import FCOSHead
label_desc: Use image node_id 1 to create a new VFNetHead object from inherited base classes: ATSSHead, FCOSHead with example: obj = VFNetHead(num_classes, in_channels, regress_ranges, center_sampling, center_sample_radius, sync_num_pos, gradient_mul, bbox_norm_type, loss_cls_fl, use_vfl, loss_cls, loss_bbox, loss_bbox_refine, norm_cfg, use_atss, reg_decoded_bbox, anchor_generator, init_cfg)