Hi,
I have a pretrained ResNet model and want to change all of its Conv2d and BatchNorm2d layers to their respective 3d counterparts.
Is their an “automatic”, iterative way of doing this? I really don’t want to replace every module manually because I also want to try different architectures.
So far I tried this, but it does not change the modules:
for module in model.modules():
if(isinstance(module, nn.Conv2d)):
kernel_size = module.kernel_size[0]
stride = module.stride[0]
padding = module.padding[0]
weight = module.weight.unsqueeze(2) / kernel_size
weight = torch.cat([weight for _ in range(0, kernel_size)], dim=2)
bias = module.bias
if(bias is None):
module = nn.Conv3d(in_channels=module.weight.shape[1], out_channels=module.weight.shape[0],
kernel_size=kernel_size, padding=padding, stride=stride, bias=False)
else:
module = nn.Conv3d(in_channels=module.weight.shape[1], out_channels=module.weight.shape[0],
kernel_size=kernel_size, padding=padding, stride=stride, bias=True)
module.bias = bias
module.weight.data = weight
elif(isinstance(module, nn.BatchNorm2d)):
weight = module.weight
bias = module.bias
module = nn.BatchNorm3d(weight.shape[0])
module.weight = weight
module.bias = bias
The model still has only Conv2d and BN2d layers afterwards. Any alternative in iterating over all modules?