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: 筛除小boxes框，nms处理，根据预测概率获取前post_nms_top_n个目标
  Args:
      proposals: 预测的bbox坐标
      objectness: 预测的目标概率
      image_shapes: batch中每张图片的size信息
      num_anchors_per_level: 每个预测特征层上预测anchors的数目
  
  Returns:
docstr_tok: ["\u7b5b\u9664\u5c0fboxes\u6846\uff0cnms\u5904\u7406\uff0c\u6839\u636e\u9884\u6d4b\u6982\u7387\u83b7\u53d6\u524dpost_nms_top_n\u4e2a\u76ee\u6807","Args",":","proposals",":","\u9884\u6d4b\u7684bbox\u5750\u6807","objectness",":","\u9884\u6d4b\u7684\u76ee\u6807\u6982\u7387","image_shapes",":","batch\u4e2d\u6bcf\u5f20\u56fe\u7247\u7684size\u4fe1\u606f","num_anchors_per_level",":","\u6bcf\u4e2a\u9884\u6d4b\u7279\u5f81\u5c42\u4e0a\u9884\u6d4banchors\u7684\u6570\u76ee","Returns",":"]
code: def filter_proposals(
      self, proposals, objectness, image_shapes, num_anchors_per_level
  ):
      # type: (Tensor, Tensor, List[Tuple[int, int]], List[int]) -> Tuple[List[Tensor], List[Tensor]]
      """
      筛除小boxes框，nms处理，根据预测概率获取前post_nms_top_n个目标
      Args:
          proposals: 预测的bbox坐标
          objectness: 预测的目标概率
          image_shapes: batch中每张图片的size信息
          num_anchors_per_level: 每个预测特征层上预测anchors的数目
  
      Returns:
  
      """
      num_images = proposals.shape[0]
      device = proposals.device
  
      # do not backprop throught objectness
      objectness = objectness.detach()
      objectness = objectness.reshape(num_images, -1)
  
      # Returns a tensor of size size filled with fill_value
      # levels负责记录分隔不同预测特征层上的anchors索引信息
      levels = [
          torch.full((n,), idx, dtype=torch.int64, device=device)
          for idx, n in enumerate(num_anchors_per_level)
      ]
      levels = torch.cat(levels, 0)
  
      # Expand this tensor to the same size as objectness
      levels = levels.reshape(1, -1).expand_as(objectness)
  
      # select top_n boxes independently per level before applying nms
      # 获取每张预测特征图上预测概率排前pre_nms_top_n的anchors索引值
      top_n_idx = self._get_top_n_idx(objectness, num_anchors_per_level)
  
      image_range = torch.arange(num_images, device=device)
      batch_idx = image_range[:, None]  # [batch_size, 1]
  
      # 根据每个预测特征层预测概率排前pre_nms_top_n的anchors索引值获取相应概率信息
      objectness = objectness[batch_idx, top_n_idx]
      levels = levels[batch_idx, top_n_idx]
      # 预测概率排前pre_nms_top_n的anchors索引值获取相应bbox坐标信息
      proposals = proposals[batch_idx, top_n_idx]
  
      objectness_prob = torch.sigmoid(objectness)
  
      final_boxes = []
      final_scores = []
      # 遍历每张图像的相关预测信息
      for boxes, scores, lvl, img_shape in zip(
          proposals, objectness_prob, levels, image_shapes
      ):
          # 调整预测的boxes信息，将越界的坐标调整到图片边界上
          boxes = box_ops.clip_boxes_to_image(boxes, img_shape)
  
          # 返回boxes满足宽，高都大于min_size的索引
          keep = box_ops.remove_small_boxes(boxes, self.min_size)
          boxes, scores, lvl = boxes[keep], scores[keep], lvl[keep]
  
          # 移除小概率boxes，参考下面这个链接
          # https://github.com/pytorch/vision/pull/3205
          keep = torch.where(torch.ge(scores, self.score_thresh))[
              0
          ]  # ge: >=
          boxes, scores, lvl = boxes[keep], scores[keep], lvl[keep]
  
          # non-maximum suppression, independently done per level
          keep = box_ops.batched_nms(
              boxes, scores, lvl, self.nms_thresh
          )
  
          # keep only topk scoring predictions
          keep = keep[: self.post_nms_top_n()]
          boxes, scores = boxes[keep], scores[keep]
  
          final_boxes.append(boxes)
          final_scores.append(scores)
      return final_boxes, final_scores
  
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 6 for calling the RegionProposalNetwork obj's underlying member method code with example usage: obj.filter_proposals(proposals, objectness, image_shapes, num_anchors_per_level) and returns: final_boxes,  final_scores