@ptrblck i have  a efficientnet model and i want to replace all of it’s batchnorm layers with groupnorm,how do i do it? here is my model code :

out_dim = 5
enet_type = 'efficientnet-b0'

pretrained_model = {
    'efficientnet-b0': '../input/efficientnet-pytorch/efficientnet-b0-08094119.pth'
}

    
class enetv2(nn.Module):
    def __init__(self, backbone, out_dim):
        super(enetv2, self).__init__()
        self.enet = enet.EfficientNet.from_name(backbone)
        self.enet.load_state_dict(torch.load(pretrained_model[backbone]))

        self.myfc = nn.Linear(self.enet._fc.in_features, out_dim)
        self.enet._fc = nn.Identity()

    def extract(self, x):
        return self.enet(x)

    def forward(self, x):
        x = self.extract(x)
        x = self.myfc(x)
        return x
model = enetv2(enet_type, out_dim=out_dim)
model = model.to(device)

with your code i get  this error :

AttributeError                            Traceback (most recent call last)
 in 
2     if isinstance(module, nn.BatchNorm2d):
3         # Get current bn layer
----> 4         bn = getattr(model, name)
5         # Create new gn layer
6         gn = nn.GroupNorm(1, bn.num_features)
/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in getattr(self, name)
592                 return modules[name]
593         raise AttributeError("’{}’ object has no attribute ‘{}’".format(
–> 594             type(self).name, name))
595
596     def setattr(self, name, value):
AttributeError: ‘enetv2’ object has no attribute ‘enet._bn0’