python - Faster R-CNN torchvision 实现的澄清

标签 python pytorch resnet torchvision faster-rcnn

我正在挖掘 source code我正在研究 torchvision 的 Faster R-CNN 实现,但我面临着一些我不太明白的事情。也就是说,假设我想创建一个更快的 R-CNN 模型,而不是在 COCO 上进行预训练,并在 ImageNet 上预训练主干,然后只获取主干,我执行以下操作:

plain_backbone = fasterrcnn_resnet50_fpn(pretrained=False, pretrained_backbone=True).backbone.body

这与所示的主干设置方式一致 herehere 。但是,当我通过模型传递图像时,结果与直接设置 resnet50 所获得的结果并不相符。即:

# Regular resnet50, pretrained on ImageNet, without the classifier and the average pooling layer
resnet50_1 = torch.nn.Sequential(*(list(torchvision.models.resnet50(pretrained=True).children())[:-2]))
resnet50_1.eval()
# Resnet50, extract from the Faster R-CNN, also pre-trained on ImageNet
resnet50_2 = fasterrcnn_resnet50_fpn(pretrained=False, pretrained_backbone=True).backbone.body
resnet50_2.eval()
# Loading a random image, converted to torch.Tensor, rescalled to [0, 1] (not that it matters)
image = transforms.ToTensor()(Image.open("random_images/random.jpg")).unsqueeze(0)
# Obtaining the model outputs
with torch.no_grad():
    # Output from the regular resnet50
    output_1 = resnet50_1(image)
    # Output from the resnet50 extracted from the Faster R-CNN
    output_2 = resnet50_2(image)["3"]
    # Their outputs aren't the same, which I would assume they should be
    np.testing.assert_almost_equal(output_1.numpy(), output_2.numpy())

期待您的想法!

最佳答案

这是因为 fasterrcnn_resnet50_fpn 使用自定义标准化层 (FrozenBatchNorm2d),而不是默认的 BatchNorm2D。它们非常相似,但我怀疑微小的数字差异会导致问题。

如果您指定用于标准 resnet 的相同归一化层,它将通过检查:

import torch
import torchvision
from torchvision.models.detection.faster_rcnn import fasterrcnn_resnet50_fpn
import numpy as np
from torchvision.ops import misc as misc_nn_ops

# Regular resnet50, pretrained on ImageNet, without the classifier and the average pooling layer
resnet50_1 = torch.nn.Sequential(*(list(torchvision.models.resnet50(pretrained=True, norm_layer=misc_nn_ops.FrozenBatchNorm2d).children())[:-2]))
resnet50_1.eval()
# Resnet50, extract from the Faster R-CNN, also pre-trained on ImageNet
resnet50_2 = fasterrcnn_resnet50_fpn(pretrained=False, pretrained_backbone=True).backbone.body
resnet50_2.eval()
# am too lazy to get a real image
image = torch.ones((1, 3, 224, 224))
# Obtaining the model outputs
with torch.no_grad():
    # Output from the regular resnet50
    output_1 = resnet50_1(image)
    # Output from the resnet50 extracted from the Faster R-CNN
    output_2 = resnet50_2(image)["3"]
    # Passes
    np.testing.assert_almost_equal(output_1.numpy(), output_2.numpy())

关于python - Faster R-CNN torchvision 实现的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65305682/

相关文章:

python - 运行时错误 ("grad can be implicitly created only for scalar outputs")

python - 具有多个值的 Tensor 的 bool 值在 Pytorch 中不明确

Python:私有(private)内部枚举类中的静态方法

python - 使用索引分割 Numpy 数组

python - 使用 Pandas 进行数据分组

python-3.x - 运行时错误 : Error(s) in loading state_dict for ResNet:

python - TensorFlow 分配内存 : Allocation of 38535168 exceeds 10% of system memory

python - 如何知道 optparse 选项是在命令行中传递的还是作为默认选项传递的

tensorflow - keras.preprocessing.text.Tokenizer 在 Pytorch 中等效吗?

deep-learning - ResNet-101 特征图形状