前言:
在MaskRCNN、FasterRCNN的最后阶段需要对边框进行一次最后的预测,得到最终的预测边框。在maskrcnn_benchmark中,这一操作由inference.py来完成。其代码详解为:
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import torch
import torch.nn.functional as F
from torch import nn
from maskrcnn_benchmark.structures.bounding_box import BoxList
from maskrcnn_benchmark.structures.boxlist_ops import boxlist_nms
from maskrcnn_benchmark.structures.boxlist_ops import cat_boxlist
from maskrcnn_benchmark.modeling.box_coder import BoxCoder
class PostProcessor(nn.Module):
"""
From a set of classification scores, box regression and proposals,
computes the post-processed boxes, and applies NMS to obtain the
final results
从一系列的分类得分,边框回归以及与之相对应的预测边框中计算出最终预测的边框,并使用
非极大线性抑制(NMS)来得到最终边框
"""
def __init__(
self,
score_thresh=0.05,
nms=0.5,
detections_per_img=100,
box_coder=None,
cls_agnostic_bbox_reg=False
):
"""
Arguments:
score_thresh (float)分类得分的阈值
nms (float)非极大线性抑制(NMS)的阈值
detections_per_img (int)每张图片中检测的目标数
box_coder (BoxCoder)边框编码器,用于计算边框偏差与获得预测边框
"""
super(PostProcessor, self).__init__()
# 将参数保存为私有属性
self.score_thresh = score_thresh
self.nms = nms
self.detections_per_img = detections_per_img
if box_coder is None:
box_coder = BoxCoder(weights=(10., 10., 5., 5.))
self.box_coder = box_coder
self.cls_agnostic_bbox_reg = cls_agnostic_bbox_reg
def forward(self, x, boxes):
"""
参数:
x (tuple[tensor, ten