Repository: pytorch/vision

Issue:
Need to put `norm_layer` as a parameter.
https://github.com/pytorch/vision/blob/684f48db4e6f619389da3a6957b3edebf794ae79/torchvision/models/detection/backbone_utils.py#L47

It works fine with resnet50_fpn, but when I try to use another backbone, for example `resnext101_32x8d`

`norm_layer=misc_nn_ops.FrozenBatchNorm2d` may trouble with Imagenet pretrained  weights, which use BatchNorm

```
Traceback (most recent call last):
  File "tmp.py", line 4, in <module>
    m = maskrcnn_resnext101_32x8d_rpn(pretrained=True)
  File "/mnt/data/luan/maskrcnn/models.py", line 218, in maskrcnn_resnext101_32x8d_rpn                                                                                      
    "resnext101_32x8d", pretrained=pretrained)
  File "/mnt/data/luan/anaconda3/envs/mask/lib/python3.6/site-packages/torchvision/models/detection/backbone_utils.py", line 47, in resnet_fpn_backbone                     
    norm_layer=misc_nn_ops.FrozenBatchNorm2d)
  File "/mnt/data/luan/anaconda3/envs/mask/lib/python3.6/site-packages/torchvision/models/resnet.py", line 313, in resnext101_32x8d                                         
    pretrained, progress, **kwargs)
  File "/mnt/data/luan/anaconda3/envs/mask/lib/python3.6/site-packages/torchvision/models/resnet.py", line 224, in _resnet                                                  
    model.load_state_dict(state_dict)
  File "/mnt/data/luan/anaconda3/envs/mask/lib/python3.6/site-packages/torch/nn/modules/module.py", line 830, in load_state_dict                                            
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for ResNet:
        Unexpected key(s) in state_dict: "bn1.num_batches_tracked", "layer1.0.bn1.num_batches_tracked", "layer1.0.bn2.num_batches_tracked", "layer1.0.bn3.num_batches_tracked", "layer1.0.downsample.1.num_batches_tracked", "layer1.1.bn1.num_batches_tracked", "layer1.1.bn2.num_batches_tracked", "layer1.1.bn3.num_batches_tracked", "layer1.2.bn1.num_batches_tracked", "layer1.2.bn2.num_batches_tracked", "layer1.2.bn3.num_batches_tracked", "layer2.0.bn1.num_batches_tracked", "layer2.0.bn2.num_batches_tracked", "layer2.0.bn3.num_batches_tracked", "layer2.0.downsample.1.num_batches_tracked", "layer2.1.bn1.num_batches_tracked", "layer2.1.bn2.num_batches_tracked", "layer2.1.bn3.num_batches_tracked", "layer2.2.bn1.num_batches_tracked", "layer2.2.bn2.num_batches_tracked", "layer2.2.bn3.num_batches_tracked", "layer2.3.bn1.num_batches_tracked", "layer2.3.bn2.num_batches_tracked", "layer2.3.bn3.num_batches_tracked", "layer3.0.bn1.num_batches_tracked", "layer3.0.bn2.num_batches_tracked", "layer3.0.bn3.num_batches_tracked", "layer3.0.downsample.1.num_batches_tracked", "layer3.1.bn1.num_batches_tracked", "layer3.1.bn2.num_batches_tracked", "layer3.1.bn3.num_batches_tracked", "layer3.2.bn1.num_batches_tracked",
"layer3.2.bn2.num_batches_tracked", "layer3.2.bn3.num_batches_tracked", "layer3.3.bn1.num_batches_tracked", "layer3.3.bn2.num_batches_tracked", "layer3.3.bn3.num_batches_tracked", "layer3.4.bn1.num_batches_tracked", "layer3.4.bn2.num_batches_tracked", "layer3.4.bn3.num_batches_tracked", "layer3.5.bn1.num_batches_tracked", "layer3.5.bn2.num_batches_tracked", "layer3.5.bn3.num_batches_tracked", "layer3.6.bn1.num_batches_tracked", "layer3.6.bn2.num_batches_tracked", "layer3.6.bn3.num_batches_tracked", "layer3.7.bn1.num_batches_tracked", "layer3.7.bn2.num_batches_tracked", "layer3.7.bn3.num_batches_tracked", "layer3.8.bn1.num_batches_tracked", "layer3.8.bn2.num_batches_tracked", "layer3.8.bn3.num_batches_tracked", "layer3.9.bn1.num_batches_tracked", "layer3.9.bn2.num_batches_tracked", "layer3.9.bn3.num_batches_tracked", "layer3.10.bn1.num_batches_tracked",
"layer3.10.bn2.num_batches_tracked", "layer3.10.bn3.num_batches_tracked", "layer3.11.bn1.num_batches_tracked", "layer3.11.bn2.num_batches_tracked", "layer3.11.bn3.num_batches_tracked", "layer3.12.bn1.num_batches_tracked", "layer3.12.bn2.num_batches_tracked", "layer3.12.bn3.num_batches_tracked", "layer3.13.bn1.num_batches_tracked", "layer3.13.bn2.num_batches_tracked", "layer3.13.bn3.num_batches_tracked", "layer3.14.bn1.num_batches_tracked", "layer3.14.bn2.num_batches_tracked", "layer3.14.bn3.num_batches_tracked", "layer3.15.bn1.num_batches_tracked", "layer3.15.bn2.num_batches_tracked", "layer3.15.bn3.num_batches_tracked", "layer3.16.bn1.num_batches_tracked", "layer3.16.bn2.num_batches_tracked", "layer3.16.bn3.num_batches_tracked", "layer3.17.bn1.num_batches_tracked", "layer3.17.bn2.num_batches_tracked", "layer3.17.bn3.num_batches_tracked", "layer3.18.bn1.num_batches_tracked", "layer3.18.bn2.num_batches_tracked", "layer3.18.bn3.num_batches_tracked", "layer3.19.bn1.num_batches_tracked", "layer3.19.bn2.num_batches_tracked", "layer3.19.bn3.num_batches_tracked", "layer3.20.bn1.num_batches_tracked", "layer3.20.bn2.num_batches_tracked", "layer3.20.bn3.num_batches_tracked", "layer3.21.bn1.num_batches_tracked", "layer3.21.bn2.num_batches_tracked", "layer3.21.bn3.num_batches_tracked", "layer3.22.bn1.num_batches_tracked", "layer3.22.bn2.num_batches_tracked", "layer3.22.bn3.num_batches_tracked", "layer4.0.bn1.num_batches_tracked", "layer4.0.bn2.num_batches_tracked", "layer4.0.bn3.num_batches_tracked", "layer4.0.downsample.1.num_batches_tracked", "layer4.1.bn1.num_batches_tracked", "layer4.1.bn2.num_batches_tracked", "layer4.1.bn3.num_batches_tracked", "layer4.2.bn1.num_batches_tracked", "layer4.2.bn2.num_batches_tracked", "layer4.2.bn3.num_batches_tracked".

```


Discussion:

Resolving patch:
diff --git a/torchvision/models/detection/backbone_utils.py b/torchvision/models/detection/backbone_utils.py
--- a/torchvision/models/detection/backbone_utils.py
+++ b/torchvision/models/detection/backbone_utils.py
@@ -41,10 +41,10 @@ def forward(self, x):
         return x
 
 
-def resnet_fpn_backbone(backbone_name, pretrained):
+def resnet_fpn_backbone(backbone_name, pretrained, norm_layer=misc_nn_ops.FrozenBatchNorm2d):
     backbone = resnet.__dict__[backbone_name](
         pretrained=pretrained,
-        norm_layer=misc_nn_ops.FrozenBatchNorm2d)
+        norm_layer=norm_layer)
     # freeze layers
     for name, parameter in backbone.named_parameters():
         if 'layer2' not in name and 'layer3' not in name and 'layer4' not in name:


Test patch: