class_docstr: Implements Region Proposal Network (RPN).
  
  Arguments:
      anchor_generator (AnchorGenerator): module that generates the anchors for a set of feature
          maps.
      head (nn.Module): module that computes the objectness and regression deltas
      fg_iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be
          considered as positive during training of the RPN.
      bg_iou_thresh (float): maximum IoU between the anchor and the GT box so that they can be
          considered as negative during training of the RPN.
      batch_size_per_image (int): number of anchors that are sampled during training of the RPN
          for computing the loss
      positive_fraction (float): proportion of positive anchors in a mini-batch during training
          of the RPN
      pre_nms_top_n (Dict[str]): number of proposals to keep before applying NMS. It should
          contain two fields: training and testing, to allow for different values depending
          on training or evaluation
      post_nms_top_n (Dict[str]): number of proposals to keep after applying NMS. It should
          contain two fields: training and testing, to allow for different values depending
          on training or evaluation
      nms_thresh (float): NMS threshold used for postprocessing the RPN proposals
class_docstr_tok: ["Implements","Region","Proposal","Network","(","RPN",")",".","Arguments",":","anchor_generator","(","AnchorGenerator",")",":","module","that","generates","the","anchors","for","a","set","of","feature","maps",".","head","(","nn.Module",")",":","module","that","computes","the","objectness","and","regression","deltas","fg_iou_thresh","(","float",")",":","minimum","IoU","between","the","anchor","and","the","GT","box","so","that","they","can","be","considered","as","positive","during","training","of","the","RPN",".","bg_iou_thresh","(","float",")",":","maximum","IoU","between","the","anchor","and","the","GT","box","so","that","they","can","be","considered","as","negative","during","training","of","the","RPN",".","batch_size_per_image","(","int",")",":","number","of","anchors","that","are","sampled","during","training","of","the","RPN","for","computing","the","loss","positive_fraction","(","float",")",":","proportion","of","positive","anchors","in","a","mini-batch","during","training","of","the","RPN","pre_nms_top_n","(","Dict","[","str","]",")",":","number","of","proposals","to","keep","before","applying","NMS",".","It","should","contain","two","fields",":","training","and","testing",",","to","allow","for","different","values","depending","on","training","or","evaluation","post_nms_top_n","(","Dict","[","str","]",")",":","number","of","proposals","to","keep","after","applying","NMS",".","It","should","contain","two","fields",":","training","and","testing",",","to","allow","for","different","values","depending","on","training","or","evaluation","nms_thresh","(","float",")",":","NMS","threshold","used","for","postprocessing","the","RPN","proposals"]
docstr: 计算每个anchors最匹配的gt，并划分为正样本，背景以及废弃的样本
  Args：
      anchors: (List[Tensor])
      targets: (List[Dict[Tensor])
  Returns:
      labels: 标记anchors归属类别（1, 0, -1分别对应正样本，背景，废弃的样本）
              注意，在RPN中只有前景和背景，所有正样本的类别都是1，0代表背景
      matched_gt_boxes：与anchors匹配的gt
docstr_tok: ["\u8ba1\u7b97\u6bcf\u4e2aanchors\u6700\u5339\u914d\u7684gt\uff0c\u5e76\u5212\u5206\u4e3a\u6b63\u6837\u672c\uff0c\u80cc\u666f\u4ee5\u53ca\u5e9f\u5f03\u7684\u6837\u672c","Args\uff1a","anchors",":","(","List","[","Tensor","]",")","targets",":","(","List","[","Dict","[","Tensor","]",")","Returns",":","labels",":","\u6807\u8bb0anchors\u5f52\u5c5e\u7c7b\u522b\uff081",",","0",",","-1\u5206\u522b\u5bf9\u5e94\u6b63\u6837\u672c\uff0c\u80cc\u666f\uff0c\u5e9f\u5f03\u7684\u6837\u672c\uff09","\u6ce8\u610f\uff0c\u5728RPN\u4e2d\u53ea\u6709\u524d\u666f\u548c\u80cc\u666f\uff0c\u6240\u6709\u6b63\u6837\u672c\u7684\u7c7b\u522b\u90fd\u662f1\uff0c0\u4ee3\u8868\u80cc\u666f","matched_gt_boxes\uff1a\u4e0eanchors\u5339\u914d\u7684gt"]
code: def assign_targets_to_anchors(self, anchors, targets):
      # type: (List[Tensor], List[Dict[str, Tensor]]) -> Tuple[List[Tensor], List[Tensor]]
      """
      计算每个anchors最匹配的gt，并划分为正样本，背景以及废弃的样本
      Args：
          anchors: (List[Tensor])
          targets: (List[Dict[Tensor])
      Returns:
          labels: 标记anchors归属类别（1, 0, -1分别对应正样本，背景，废弃的样本）
                  注意，在RPN中只有前景和背景，所有正样本的类别都是1，0代表背景
          matched_gt_boxes：与anchors匹配的gt
      """
      labels = []
      matched_gt_boxes = []
      # 遍历每张图像的anchors和targets
      for anchors_per_image, targets_per_image in zip(anchors, targets):
          gt_boxes = targets_per_image["boxes"]
          if gt_boxes.numel() == 0:
              device = anchors_per_image.device
              matched_gt_boxes_per_image = torch.zeros(
                  anchors_per_image.shape,
                  dtype=torch.float32,
                  device=device,
              )
              labels_per_image = torch.zeros(
                  (anchors_per_image.shape[0],),
                  dtype=torch.float32,
                  device=device,
              )
          else:
              # 计算anchors与真实bbox的iou信息
              # set to self.box_similarity when https://github.com/pytorch/pytorch/issues/27495 lands
              match_quality_matrix = box_ops.box_iou(
                  gt_boxes, anchors_per_image
              )
              # 计算每个anchors与gt匹配iou最大的索引（如果iou<0.3索引置为-1，0.3<iou<0.7索引为-2）
              matched_idxs = self.proposal_matcher(match_quality_matrix)
              # get the targets corresponding GT for each proposal
              # NB: need to clamp the indices because we can have a single
              # GT in the image, and matched_idxs can be -2, which goes
              # out of bounds
              matched_gt_boxes_per_image = gt_boxes[
                  matched_idxs.clamp(min=0)
              ]
  
              labels_per_image = matched_idxs >= 0
              labels_per_image = labels_per_image.to(
                  dtype=torch.float32
              )
  
              # background (negative examples)
              bg_indices = (
                  matched_idxs
                  == self.proposal_matcher.BELOW_LOW_THRESHOLD
              )  # -1
              labels_per_image[bg_indices] = 0.0
  
              # discard indices that are between thresholds
              inds_to_discard = (
                  matched_idxs
                  == self.proposal_matcher.BETWEEN_THRESHOLDS
              )  # -2
              labels_per_image[inds_to_discard] = -1.0
  
          labels.append(labels_per_image)
          matched_gt_boxes.append(matched_gt_boxes_per_image)
      return labels, matched_gt_boxes
  
imports: from typing import List, Optional, Dict, Tuple
  import torch
  from torch import nn, Tensor
  from torch.nn import functional
  import torchvision
  from .None import det_utils
  from .None import boxes
  from .image_list import ImageList
label_desc: Use image node_id 4 for calling the RegionProposalNetwork obj's underlying member method code with example usage: obj.assign_targets_to_anchors(anchors, targets) and returns: labels,  matched_gt_boxes