Transpose CNN weights


I am trying to automate the process of transposing a CNN. This basically means that I am trying to transfer the weights from a CNN to a NN with ConvTranspose layers (instead of Conv2d). Is there any automation or “hacky” way to do it?

You could permute the weight tensor and copy_ it to the transposed conv layer:

conv = nn.Conv2d(16, 32, (3, 4))
# torch.Size([32, 16, 3, 4])

conv_trans = nn.ConvTranspose2d(16, 32, (3, 4))
# torch.Size([16, 32, 3, 4])

with torch.no_grad():
    conv_trans.weight.copy_(conv.weight.permute(1, 0, 2, 3))

I think that will do the job :slight_smile: