class_docstr: Implements RetinaNet.
  
  The input to the model is expected to be a list of tensors, each of shape [C, H, W], one for each
  image, and should be in 0-1 range. Different images can have different sizes.
  
  The behavior of the model changes depending if it is in training or evaluation mode.
  
  During training, the model expects both the input tensors, as well as a targets (list of dictionary),
  containing:
      - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with
        ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
      - labels (Int64Tensor[N]): the class label for each ground-truth box
  
  The model returns a Dict[Tensor] during training, containing the classification and regression
  losses.
  
  During inference, the model requires only the input tensors, and returns the post-processed
  predictions as a List[Dict[Tensor]], one for each input image. The fields of the Dict are as
  follows:
      - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with
        ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
      - labels (Int64Tensor[N]): the predicted labels for each image
      - scores (Tensor[N]): the scores for each prediction
  
  Args:
      backbone (nn.Module): the network used to compute the features for the model.
          It should contain an out_channels attribute, which indicates the number of output
          channels that each feature map has (and it should be the same for all feature maps).
          The backbone should return a single Tensor or an OrderedDict[Tensor].
      num_classes (int): number of output classes of the model (excluding the background).
      min_size (int): minimum size of the image to be rescaled before feeding it to the backbone
      max_size (int): maximum size of the image to be rescaled before feeding it to the backbone
      image_mean (Tuple[float, float, float]): mean values used for input normalization.
          They are generally the mean values of the dataset on which the backbone has been trained
          on
      image_std (Tuple[float, float, float]): std values used for input normalization.
          They are generally the std values of the dataset on which the backbone has been trained on
      anchor_generator (AnchorGenerator): module that generates the anchors for a set of feature
          maps.
      head (nn.Module): Module run on top of the feature pyramid.
          Defaults to a module containing a classification and regression module.
      score_thresh (float): Score threshold used for postprocessing the detections.
      nms_thresh (float): NMS threshold used for postprocessing the detections.
      detections_per_img (int): Number of best detections to keep after NMS.
      fg_iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be
          considered as positive during training.
      bg_iou_thresh (float): maximum IoU between the anchor and the GT box so that they can be
          considered as negative during training.
      topk_candidates (int): Number of best detections to keep before NMS.
class_docstr_tok: ["Implements","RetinaNet",".","The","input","to","the","model","is","expected","to","be","a","list","of","tensors",",","each","of","shape","[","C",",","H",",","W","]",",","one","for","each","image",",","and","should","be","in","0-1","range",".","Different","images","can","have","different","sizes",".","The","behavior","of","the","model","changes","depending","if","it","is","in","training","or","evaluation","mode",".","During","training",",","the","model","expects","both","the","input","tensors",",","as","well","as","a","targets","(","list","of","dictionary",")",",","containing",":","-","boxes","(","``","FloatTensor","[","N",",","4","]","``",")",":","the","ground-truth","boxes","in","``","[","x1",",","y1",",","x2",",","y2","]","``","format",",","with","``","0","<","=","x1","<","x2","<","=","W","``","and","``","0","<","=","y1","<","y2","<","=","H","``",".","-","labels","(","Int64Tensor","[","N","]",")",":","the","class","label","for","each","ground-truth","box","The","model","returns","a","Dict","[","Tensor","]","during","training",",","containing","the","classification","and","regression","losses",".","During","inference",",","the","model","requires","only","the","input","tensors",",","and","returns","the","post-processed","predictions","as","a","List","[","Dict","[","Tensor","]","]",",","one","for","each","input","image",".","The","fields","of","the","Dict","are","as","follows",":","-","boxes","(","``","FloatTensor","[","N",",","4","]","``",")",":","the","predicted","boxes","in","``","[","x1",",","y1",",","x2",",","y2","]","``","format",",","with","``","0","<","=","x1","<","x2","<","=","W","``","and","``","0","<","=","y1","<","y2","<","=","H","``",".","-","labels","(","Int64Tensor","[","N","]",")",":","the","predicted","labels","for","each","image","-","scores","(","Tensor","[","N","]",")",":","the","scores","for","each","prediction","Args",":","backbone","(","nn.Module",")",":","the","network","used","to","compute","the","features","for","the","model",".","It","should","contain","an","out_channels","attribute",",","which","indicates","the","number","of","output","channels","that","each","feature","map","has","(","and","it","should","be","the","same","for","all","feature","maps",")",".","The","backbone","should","return","a","single","Tensor","or","an","OrderedDict","[","Tensor","]",".","num_classes","(","int",")",":","number","of","output","classes","of","the","model","(","excluding","the","background",")",".","min_size","(","int",")",":","minimum","size","of","the","image","to","be","rescaled","before","feeding","it","to","the","backbone","max_size","(","int",")",":","maximum","size","of","the","image","to","be","rescaled","before","feeding","it","to","the","backbone","image_mean","(","Tuple","[","float",",","float",",","float","]",")",":","mean","values","used","for","input","normalization",".","They","are","generally","the","mean","values","of","the","dataset","on","which","the","backbone","has","been","trained","on","image_std","(","Tuple","[","float",",","float",",","float","]",")",":","std","values","used","for","input","normalization",".","They","are","generally","the","std","values","of","the","dataset","on","which","the","backbone","has","been","trained","on","anchor_generator","(","AnchorGenerator",")",":","module","that","generates","the","anchors","for","a","set","of","feature","maps",".","head","(","nn.Module",")",":","Module","run","on","top","of","the","feature","pyramid",".","Defaults","to","a","module","containing","a","classification","and","regression","module",".","score_thresh","(","float",")",":","Score","threshold","used","for","postprocessing","the","detections",".","nms_thresh","(","float",")",":","NMS","threshold","used","for","postprocessing","the","detections",".","detections_per_img","(","int",")",":","Number","of","best","detections","to","keep","after","NMS",".","fg_iou_thresh","(","float",")",":","minimum","IoU","between","the","anchor","and","the","GT","box","so","that","they","can","be","considered","as","positive","during","training",".","bg_iou_thresh","(","float",")",":","maximum","IoU","between","the","anchor","and","the","GT","box","so","that","they","can","be","considered","as","negative","during","training",".","topk_candidates","(","int",")",":","Number","of","best","detections","to","keep","before","NMS","."]
code: def __init__(
      self,
      backbone,
      num_classes,
      # transform parameters
      min_size=800,
      max_size=1333,
      image_mean=None,
      image_std=None,
      # Anchor parameters
      anchor_generator=None,
      head=None,
      proposal_matcher=None,
      score_thresh=0.05,
      nms_thresh=0.5,
      detections_per_img=100,
      fg_iou_thresh=0.5,
      bg_iou_thresh=0.4,
      topk_candidates=1000,
  ):
      super(RetinaNet, self).__init__()
  
      if not hasattr(backbone, "out_channels"):
          raise ValueError(
              "backbone should contain an attribute out_channels "
              "specifying the number of output channels (assumed to be the "
              "same for all the levels)"
          )
  
      self.backbone = backbone
  
      assert isinstance(
          anchor_generator, (AnchorsGenerator, type(None))
      )
  
      if anchor_generator is None:
          # 原论文中说在每个预测特征层上除了使用给定的尺度x外，还要额外添加x*2^(1/3)和x*2^(2/3)这两个尺度
          # 五个预测特征层采用的原始尺度分别为32， 64， 128， 256， 512
          # 注意尺度和面积的关系，面积=尺度^2
          anchor_sizes = tuple(
              (x, int(x * 2 ** (1.0 / 3)), int(x * 2 ** (2.0 / 3)))
              for x in [32, 64, 128, 256, 512]
          )
          # 对于每个预测特征层上anchors，都会使用三种比例
          aspect_ratios = ((0.5, 1.0, 2.0),) * len(anchor_sizes)
          anchor_generator = AnchorsGenerator(
              anchor_sizes, aspect_ratios
          )
  
      self.anchor_generator = anchor_generator
  
      if head is None:
          head = RetinaNetHead(
              backbone.out_channels,  # in_channels
              anchor_generator.num_anchors_per_location()[
                  0
              ],  # num_anchors
              num_classes,
          )  # num_classes
      self.head = head
  
      if proposal_matcher is None:
          proposal_matcher = det_utils.Matcher(
              fg_iou_thresh,
              bg_iou_thresh,
              allow_low_quality_matches=True,
          )
      self.proposal_matcher = proposal_matcher
  
      self.box_coder = det_utils.BoxCoder(weights=(1.0, 1.0, 1.0, 1.0))
  
      if image_mean is None:
          image_mean = [0.485, 0.456, 0.406]
      if image_std is None:
          image_std = [0.229, 0.224, 0.225]
  
      self.transform = GeneralizedRCNNTransform(
          min_size, max_size, image_mean, image_std
      )
  
      self.score_thresh = score_thresh
      self.nms_thresh = nms_thresh
      self.detections_per_img = detections_per_img
      self.topk_candidates = topk_candidates
  
      # used only on torchscript mode
      self._has_warned = False
  
imports: import math
  import warnings
  from collections import OrderedDict
  from typing import Dict, List, Tuple, Optional, Union
  import torch
  from torch import nn, Tensor
  from .None import det_utils
  from .anchor_utils import AnchorsGenerator
  from .None import boxes
  from .losses import sigmoid_focal_loss
  from .transform import GeneralizedRCNNTransform
label_desc: Use image node_id 1 to create a new RetinaNet object from inherited base classes: nn with example: obj = RetinaNet(backbone, num_classes, min_size, max_size, image_mean, image_std, anchor_generator, head, proposal_matcher, score_thresh, nms_thresh, detections_per_img, fg_iou_thresh, bg_iou_thresh, topk_candidates)