Add a fourth input channel to resnet18. Or: Why does copying weights between nn.Conv2d result in a non-leaf node?

I’m trying to add a fourth channel to a pre-trained Resnet.
When I copy the weights over, the Parameter loses its leaf node characteristics.
I’ve tried to set needs gradients etc. but no luck.
What do i need to do to make the newly copied weights a leaf_node ?

Thanks!

Here’s the example code:

model_ft = models.resnet18(pretrained=True)
#load the weigths into the new convolution, copy the last plane of the orig into the new
new_input_conv = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)
new_input_conv.weight[:,0] = model_ft.conv1.weight[:,0].clone()
new_input_conv.weight[:,1] = model_ft.conv1.weight[:,1].clone()
new_input_conv.weight[:,2] = model_ft.conv1.weight[:,2].clone()
new_input_conv.weight[:,3] = model_ft.conv1.weight[:,2].clone()

new_input_conv.weight.is_leaf

Out[1]: False

Ok I figured this out. For some reason the weights need to be detached and reassigned as a Parameter with grad re-enabled.

new_input_conv.weight = nn.Parameter(new_input_conv.weight.detach().requires_grad_(True))