无法关闭 faster-rcnn (PyTorch) 的批量规范层

Not able to switch off batch norm layers for faster-rcnn (PyTorch)

我正在尝试关闭用于评估模式的 faster-rcnn 模型中的批量归一化层。

我正在对 atm 进行健全性检查:

@torch.no_grad()
def evaluate_loss(model, data_loader, device):
    val_loss = 0
    model.train()
    for images, targets in data_loader:
        # check that all layers are in train mode
        # for name, module in model.named_modules():
        #     if hasattr(module, 'training'):
        #         print('{} is training {}'.format(name, module.training))
        #         # set bn layers to eval
        for module in model.modules():
            if isinstance(module, torch.nn.BatchNorm2d):
                module.eval()
        # bn layers are now in eval
        for name, module in model.named_modules():
            if hasattr(module, 'training'):
                print('{} is training {}'.format(name, module.training))

但是,所有批量规范层仍处于训练模式。当我用例如 Conv2d 替换它时,我得到了 False 的预期行为。这是输出的示例片段:

backbone.body.layer4.0.conv1 is training True
backbone.body.layer4.0.bn1 is training True
backbone.body.layer4.0.conv2 is training True
backbone.body.layer4.0.bn2 is training True
backbone.body.layer4.0.conv3 is training True
backbone.body.layer4.0.bn3 is training True

为什么会这样?我该怎么做才能关闭这些图层?我已经用 torch.nn.

提供的所有批处理规范变体进行了尝试

因此,经过进一步调查并打印出 faster-rcnn 提供的所有模块后,预留模型使用 FrozenBatchNorm2d 而不是 BatchNorm2d

此外,与文档当前所述不同,您必须调用 torchvision.ops.misc.FrozenBatchNorm2d 而不是 torchvision.ops.FrozenBatchNorm2d

此外,由于图层已经冻结,因此无需“关闭”这些图层,因此可能不需要 model.eval()