2025年maskrcnn_benchmark 代码详解之 box_head.py

maskrcnn_benchmark 代码详解之 box_head.py前言 在 FasterRCNN 或者 MaskRCNN 中 在通过 RPN 筛选出大小不同的一系列预测边框后 需要在 Box Head 层对选出的预测边框进行进一步筛选 去除掉边框 IoU 介于两种阈值之间的预测边框 然后再使用 ROI Pooling 将处理后的预测边框池化为大小一致的边框

大家好,我是讯享网,很高兴认识大家。

前言:

     在FasterRCNN或者MaskRCNN中,在通过RPN筛选出大小不同的一系列预测边框后,需要在Box_Head层对选出的预测边框进行进一步筛选,去除掉边框IoU介于两种阈值之间的预测边框,然后再使用ROI Pooling将处理后的预测边框池化为大小一致的边框。 然后对这些边框进行进一步的特征提取,之后再在提取后的特征上进行进一步的边框预测,得到每一个预测边框的类别得分以及它们的边框回归信息,并以此来得到最终的预测边框以及其类别信息。其代码详解如下:


讯享网

# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. import torch from torch import nn from .roi_box_feature_extractors import make_roi_box_feature_extractor from .roi_box_predictors import make_roi_box_predictor from .inference import make_roi_box_post_processor from .loss import make_roi_box_loss_evaluator class ROIBoxHead(torch.nn.Module): """ Generic Box Head class. """ def __init__(self, cfg, in_channels): super(ROIBoxHead, self).__init__() # 指定ROI层中box_head模块的特征提取类 self.feature_extractor = make_roi_box_feature_extractor(cfg, in_channels) # 指定ROI层中box_head模块的边框预测类 self.predictor = make_roi_box_predictor( cfg, self.feature_extractor.out_channels) # 指定box_head模块特征提取后的一系列操作,包括修正预测边框等 self.post_processor = make_roi_box_post_processor(cfg) # 指定box_head模块的loss评估类 self.loss_evaluator = make_roi_box_loss_evaluator(cfg) def forward(self, features, proposals, targets=None): """ 参数: features (list[Tensor]): feature-maps from possibly several levels从backbone中获取的特征图(可能是多层) proposals (list[BoxList]): proposal boxes预测边框 targets (list[BoxList], optional): the ground-truth targets.基准边框 返回值: x (Tensor): the result of the feature extractor在重新修正后的预测边框中提取的特征信息 proposals (list[BoxList]): during training, the subsampled proposals are returned. During testing, the predicted boxlists are returned重新修正后的最终的预测边框 losses (dict[Tensor]): During training, returns the losses for the head. During testing, returns an empty dict.本层的loss值 """ if self.training: # Faster R-CNN subsamples during training the proposals with a fixed # positive / negative ratio # 在上层得出的预测边框里面,经过特征提取后,重新修正已有的预测边框,去除无效的边框 with torch.no_grad(): proposals = self.loss_evaluator.subsample(proposals, targets) # extract features that will be fed to the final classifier. The # feature_extractor generally corresponds to the pooler + heads # 从修正后的预测边框对应的特征图里进一步提取特征 x = self.feature_extractor(features, proposals) # final classifier that converts the features into predictions # 利用从修正后的预测边框对应的特征进行边框类别和边框偏差信息的预测 class_logits, box_regression = self.predictor(x) if not self.training: # 如果在测试阶段,直接预测图片中的边框 result = self.post_processor((class_logits, box_regression), proposals) return x, result, {} # 计算loss值 loss_classifier, loss_box_reg = self.loss_evaluator( [class_logits], [box_regression] ) # 返回相关信息 return ( x, proposals, dict(loss_classifier=loss_classifier, loss_box_reg=loss_box_reg), ) # todo 构建RoI层Box_Head模块的构造类 def build_roi_box_head(cfg, in_channels): """ Constructs a new box head. By default, uses ROIBoxHead, but if it turns out not to be enough, just register a new class and make it a parameter in the config """ return ROIBoxHead(cfg, in_channels) 

讯享网

 

小讯
上一篇 2025-03-27 10:49
下一篇 2025-02-09 18:00

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/82378.html