docstr: 为每个proposal匹配对应的gt_box，并划分到正负样本中
  Args:
      proposals:
      gt_boxes:
      gt_labels:
  
  Returns:
docstr_tok: ["\u4e3a\u6bcf\u4e2aproposal\u5339\u914d\u5bf9\u5e94\u7684gt_box\uff0c\u5e76\u5212\u5206\u5230\u6b63\u8d1f\u6837\u672c\u4e2d","Args",":","proposals",":","gt_boxes",":","gt_labels",":","Returns",":"]
code: def assign_targets_to_proposals(self, proposals, gt_boxes, gt_labels):
      # type: (List[Tensor], List[Tensor], List[Tensor]) -> Tuple[List[Tensor], List[Tensor]]
      """
      为每个proposal匹配对应的gt_box，并划分到正负样本中
      Args:
          proposals:
          gt_boxes:
          gt_labels:
  
      Returns:
  
      """
      matched_idxs = []
      labels = []
      # 遍历每张图像的proposals, gt_boxes, gt_labels信息
      for (
          proposals_in_image,
          gt_boxes_in_image,
          gt_labels_in_image,
      ) in zip(proposals, gt_boxes, gt_labels):
          if gt_boxes_in_image.numel() == 0:  # 该张图像中没有gt框，为背景
              # background image
              device = proposals_in_image.device
              clamped_matched_idxs_in_image = torch.zeros(
                  (proposals_in_image.shape[0],),
                  dtype=torch.int64,
                  device=device,
              )
              labels_in_image = torch.zeros(
                  (proposals_in_image.shape[0],),
                  dtype=torch.int64,
                  device=device,
              )
          else:
              #  set to self.box_similarity when https://github.com/pytorch/pytorch/issues/27495 lands
              # 计算proposal与每个gt_box的iou重合度
              match_quality_matrix = box_ops.box_iou(
                  gt_boxes_in_image, proposals_in_image
              )
  
              # 计算proposal与每个gt_box匹配的iou最大值，并记录索引，
              # iou < low_threshold索引值为 -1， low_threshold <= iou < high_threshold索引值为 -2
              matched_idxs_in_image = self.proposal_matcher(
                  match_quality_matrix
              )
  
              # 限制最小值，防止匹配标签时出现越界的情况
              # 注意-1, -2对应的gt索引会调整到0,获取的标签类别为第0个gt的类别（实际上并不是）,后续会进一步处理
              clamped_matched_idxs_in_image = (
                  matched_idxs_in_image.clamp(min=0)
              )
              # 获取proposal匹配到的gt对应标签
              labels_in_image = gt_labels_in_image[
                  clamped_matched_idxs_in_image
              ]
              labels_in_image = labels_in_image.to(dtype=torch.int64)
  
              # label background (below the low threshold)
              # 将gt索引为-1的类别设置为0，即背景，负样本
              bg_inds = (
                  matched_idxs_in_image
                  == self.proposal_matcher.BELOW_LOW_THRESHOLD
              )  # -1
              labels_in_image[bg_inds] = 0
  
              # label ignore proposals (between low and high threshold)
              # 将gt索引为-2的类别设置为-1, 即废弃样本
              ignore_inds = (
                  matched_idxs_in_image
                  == self.proposal_matcher.BETWEEN_THRESHOLDS
              )  # -2
              labels_in_image[
                  ignore_inds
              ] = -1  # -1 is ignored by sampler
  
          matched_idxs.append(clamped_matched_idxs_in_image)
          labels.append(labels_in_image)
      return matched_idxs, labels
  
file_path: deep-learning-for-image-processing/pytorch_object_detection/faster_rcnn/network_files/roi_head.py
imports: from typing import Optional, List, Dict, Tuple
  import torch
  from torch import Tensor
  import torch.nn.functional
  from .None import det_utils
  from .None import boxes
label_desc: Use image node_id 2 for calling the RoIHeads obj's underlying member method code with example usage: obj.assign_targets_to_proposals(proposals, gt_boxes, gt_labels) and returns: matched_idxs,  labels