maskrcnn_benchmark 代码详解之 inference.py

本文详细介绍了MaskRCNN_benchmark库中的inference.py文件,该文件负责在目标检测任务中执行边框预测的最后步骤,以得出最终的预测结果。

前言:

     在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,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值