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: 计算RPN损失，包括类别损失（前景与背景），bbox regression损失
  Arguments:
      objectness (Tensor)：预测的前景概率
      pred_bbox_deltas (Tensor)：预测的bbox regression
      labels (List[Tensor])：真实的标签 1, 0, -1（batch中每一张图片的labels对应List的一个元素中）
      regression_targets (List[Tensor])：真实的bbox regression
  
  Returns:
      objectness_loss (Tensor) : 类别损失
      box_loss (Tensor)：边界框回归损失
docstr_tok: ["\u8ba1\u7b97RPN\u635f\u5931\uff0c\u5305\u62ec\u7c7b\u522b\u635f\u5931\uff08\u524d\u666f\u4e0e\u80cc\u666f\uff09\uff0cbbox","regression\u635f\u5931","Arguments",":","objectness","(","Tensor",")","\uff1a\u9884\u6d4b\u7684\u524d\u666f\u6982\u7387","pred_bbox_deltas","(","Tensor",")","\uff1a\u9884\u6d4b\u7684bbox","regression","labels","(","List","[","Tensor","]",")","\uff1a\u771f\u5b9e\u7684\u6807\u7b7e","1",",","0",",","-1\uff08batch\u4e2d\u6bcf\u4e00\u5f20\u56fe\u7247\u7684labels\u5bf9\u5e94List\u7684\u4e00\u4e2a\u5143\u7d20\u4e2d\uff09","regression_targets","(","List","[","Tensor","]",")","\uff1a\u771f\u5b9e\u7684bbox","regression","Returns",":","objectness_loss","(","Tensor",")",":","\u7c7b\u522b\u635f\u5931","box_loss","(","Tensor",")","\uff1a\u8fb9\u754c\u6846\u56de\u5f52\u635f\u5931"]
code: def compute_loss(
      self, objectness, pred_bbox_deltas, labels, regression_targets
  ):
      # type: (Tensor, Tensor, List[Tensor], List[Tensor]) -> Tuple[Tensor, Tensor]
      """
      计算RPN损失，包括类别损失（前景与背景），bbox regression损失
      Arguments:
          objectness (Tensor)：预测的前景概率
          pred_bbox_deltas (Tensor)：预测的bbox regression
          labels (List[Tensor])：真实的标签 1, 0, -1（batch中每一张图片的labels对应List的一个元素中）
          regression_targets (List[Tensor])：真实的bbox regression
  
      Returns:
          objectness_loss (Tensor) : 类别损失
          box_loss (Tensor)：边界框回归损失
      """
      # 按照给定的batch_size_per_image, positive_fraction选择正负样本
      sampled_pos_inds, sampled_neg_inds = self.fg_bg_sampler(labels)
      # 将一个batch中的所有正负样本List(Tensor)分别拼接在一起，并获取非零位置的索引
      # sampled_pos_inds = torch.nonzero(torch.cat(sampled_pos_inds, dim=0)).squeeze(1)
      sampled_pos_inds = torch.where(
          torch.cat(sampled_pos_inds, dim=0)
      )[0]
      # sampled_neg_inds = torch.nonzero(torch.cat(sampled_neg_inds, dim=0)).squeeze(1)
      sampled_neg_inds = torch.where(
          torch.cat(sampled_neg_inds, dim=0)
      )[0]
  
      # 将所有正负样本索引拼接在一起
      sampled_inds = torch.cat(
          [sampled_pos_inds, sampled_neg_inds], dim=0
      )
      objectness = objectness.flatten()
  
      labels = torch.cat(labels, dim=0)
      regression_targets = torch.cat(regression_targets, dim=0)
  
      # 计算边界框回归损失
      box_loss = det_utils.smooth_l1_loss(
          pred_bbox_deltas[sampled_pos_inds],
          regression_targets[sampled_pos_inds],
          beta=1 / 9,
          size_average=False,
      ) / (sampled_inds.numel())
  
      # 计算目标预测概率损失
      objectness_loss = F.binary_cross_entropy_with_logits(
          objectness[sampled_inds], labels[sampled_inds]
      )
  
      return objectness_loss, box_loss
  
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 7 for calling the RegionProposalNetwork obj's underlying member method code with example usage: obj.compute_loss(objectness, pred_bbox_deltas, labels, regression_targets) and returns: objectness_loss,  box_loss