class_docstr: Implement FCOS in :paper:`fcos`.
class_docstr_tok: ["Implement","FCOS","in",":","paper",":","`","fcos","`","."]
docstr: Match ground-truth boxes to a set of multi-level anchors.
  
  Args:
      gt_boxes: Ground-truth boxes from instances of an image.
      anchors: List of anchors for each feature map (of different scales).
  
  Returns:
      torch.Tensor
          A tensor of shape `(M, R)`, given `M` ground-truth boxes and total
          `R` anchor points from all feature levels, indicating the quality
          of match between m-th box and r-th anchor. Higher value indicates
          better match.
docstr_tok: ["Match","ground-truth","boxes","to","a","set","of","multi-level","anchors",".","Args",":","gt_boxes",":","Ground-truth","boxes","from","instances","of","an","image",".","anchors",":","List","of","anchors","for","each","feature","map","(","of","different","scales",")",".","Returns",":","torch.Tensor","A","tensor","of","shape","`","(","M",",","R",")","`",",","given","`","M","`","ground-truth","boxes","and","total","`","R","`","anchor","points","from","all","feature","levels",",","indicating","the","quality","of","match","between","m-th","box","and","r-th","anchor",".","Higher","value","indicates","better","match","."]
code: def _match_anchors(self, gt_boxes: Boxes, anchors: List[Boxes]):
      """
      Match ground-truth boxes to a set of multi-level anchors.
  
      Args:
          gt_boxes: Ground-truth boxes from instances of an image.
          anchors: List of anchors for each feature map (of different scales).
  
      Returns:
          torch.Tensor
              A tensor of shape `(M, R)`, given `M` ground-truth boxes and total
              `R` anchor points from all feature levels, indicating the quality
              of match between m-th box and r-th anchor. Higher value indicates
              better match.
      """
      # Naming convention: (M = ground-truth boxes, R = anchor points)
      # Anchor points are represented as square boxes of size = stride.
      num_anchors_per_level = [len(x) for x in anchors]
      anchors = Boxes.cat(anchors)  # (R, 4)
      anchor_centers = anchors.get_centers()  # (R, 2)
      anchor_sizes = (
          anchors.tensor[:, 2] - anchors.tensor[:, 0]
      )  # (R, )
  
      lower_bound = anchor_sizes * 4
      lower_bound[: num_anchors_per_level[0]] = 0
      upper_bound = anchor_sizes * 8
      upper_bound[-num_anchors_per_level[-1] :] = float("inf")
  
      gt_centers = gt_boxes.get_centers()
  
      # FCOS with center sampling: anchor point must be close enough to
      # ground-truth box center.
      center_dists = (
          anchor_centers[None, :, :] - gt_centers[:, None, :]
      ).abs_()
      sampling_regions = (
          self.center_sampling_radius * anchor_sizes[None, :]
      )
  
      match_quality_matrix = (
          center_dists.max(dim=2).values < sampling_regions
      )
  
      pairwise_dist = pairwise_point_box_distance(
          anchor_centers, gt_boxes
      )
      pairwise_dist = pairwise_dist.permute(1, 0, 2)  # (M, R, 4)
  
      # The original FCOS anchor matching rule: anchor point must be inside GT.
      match_quality_matrix &= pairwise_dist.min(dim=2).values > 0
  
      # Multilevel anchor matching in FCOS: each anchor is only responsible
      # for certain scale range.
      pairwise_dist = pairwise_dist.max(dim=2).values
      match_quality_matrix &= (pairwise_dist > lower_bound[None, :]) & (
          pairwise_dist < upper_bound[None, :]
      )
      # Match the GT box with minimum area, if there are multiple GT matches.
      gt_areas = gt_boxes.area()  # (M, )
  
      match_quality_matrix = match_quality_matrix.to(torch.float32)
      match_quality_matrix *= 1e8 - gt_areas[:, None]
      return match_quality_matrix
  
imports: import logging
  from typing import List, Optional, Tuple
  import torch
  from fvcore.nn import sigmoid_focal_loss_jit
  from torch import nn
  from torch.nn import functional
  from detectron2.layers import ShapeSpec, batched_nms
  from detectron2.structures import Boxes, ImageList, Instances, pairwise_point_box_distance
  from detectron2.utils.events import get_event_storage
  from ..anchor_generator import DefaultAnchorGenerator
  from ..backbone import Backbone
  from ..box_regression import Box2BoxTransformLinear, _dense_box_regression_loss
  from .dense_detector import DenseDetector
  from .retinanet import RetinaNetHead
label_desc: Use image node_id 3 for calling the FCOS obj's underlying member method code with example usage: obj._match_anchors(gt_boxes, anchors) and returns: match_quality_matrix