`create_body` input argument `n_in=3` even for network architecture with more than three input channel

Hi guys, I have a question regarding the input argument n_in in the create_body

I’ve successfully created a multi-channel (more than three) Dynamic Unet (with 5 input channels), by 1) first creating a datablock following this post by Maurício, and 2) created a multi-channel Resnet architecture as encoder body following the github implementation here.

Everything is working fine. But I then want to dig a little deeper about the construction of Dynamic Unet by following the function fastai.vision.learner.unet_learner.

This function takes an input argument n_in which is then passed to the line body = create_body(arch, n_in, pretrained, ifnone(cut, meta['cut'])).

Since I have build a model architecture with 5 input channel, I thought I’d assign 5 to the n_in, but got an error, and turns out the _update_first_layer function it is calling need to ensure this value is set to 3:
assert getattr(first_layer, 'in_channels') == 3, f'Unexpected number of input channels, found {getattr(first_layer, "in_channels")} while expecting 3'.

I found this to be confusing. My understanding is that we only want make sure that the input channel to be three in order to load the pretrained weights. But the current code if n_in is not equal to 3, it will also fail even if pretrained is set to false:

    if pretrained:
        _load_pretrained_weights(new_layer, first_layer)

If that’s the case, that’s the point of this n_in parameters? (I need to set n_in = 3 even my network architecture expect 5 input channel …)

I’m not sure if it is a “bug”, or there is something that I misunderstand … Thank you in advance for anyone’s help for my understanding of this!

1 Like