
代码:
features = self.backbone(images.tensors)
# #这一步应该是修正一下,统一一下格式
#因为当backbone使用了fpn的时候,会有多个feature_map,被存放到了OrderedDict中
#而没有使用的时候,只会返回一个feature_map
#因此当只有1个的时候,也将其存在OrderedDict中,统一格式
if isinstance(features, torch.Tensor):
features = OrderedDict([("0", features)])
#rpn
proposals, proposal_losses = self.rpn(images, features, targets)
#roi_heads:roi_pooling + 分类
detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
#后处理,进行NMS 同时将 box 通过 original_images_size映射回原图
detections = self.transform.postprocess(detections, images.image_sizes, original_image_sizes) # type: ignore[operator]
losses = {}
losses.update(detector_losses)
losses.update(proposal_losses)
——————————————————————————————————————————————
@torch.jit.unused
def eager_outputs(self, losses, detections):
# type: (Dict[str, Tensor], List[Dict[str, Tensor]]) -> Union[Dict[str, Tensor], List[Dict[str, Tensor]]]
if self.training:
return losses
return detections
if torch.jit.is_scripting():
if not self._has_warned:
warnings.warn("RCNN always returns a (Losses, Detections) tuple in scripting")
self._has_warned = True
return losses, detections
else:
##训练模式下,只返回losses;推断模式下 只返回detections
return self.eager_outputs(losses, detections)