Change first convolution layer of Densenet

I want to convert first convolution layer from 3 channel to 4 channel.

DenseNet(
  (features): Sequential(
    (conv0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)        
    (norm0): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)     
    (relu0): ReLU(inplace=True)

It should be like

DenseNet(
  (features): Sequential(
    (conv0): Conv2d(4, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)        
    (norm0): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)     
    (relu0): ReLU(inplace=True)

I did

model= models.densenet121(pretrained=True)
                model.conv0 = nn.Conv2d(4, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
                set_parameter_requires_grad(model, feature_extract)
                num_ftrs = model.classifier.in_features
                model.classifier = nn.Linear(num_ftrs, num_classes)

But it is not working

What kind of error are you seeing?

Thanks @ptrblck. It is showing RuntimeError: Given groups=1, weight of size 64 3 7 7, expected input[16, 4, 224, 224] to have 3 channels, but got 4 channels instead
I have changed the convolution layer as

model.conv0 = nn.Conv2d(4, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)

But When I print the model, it is showing

(conv0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False) 

The conv0 is under the Sequential features, not an attribute of the model itself.

I think you might be able to swap it with indexing it like you would a list
model.features[0] = nn.Conv2d(4, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)

1 Like

@futscdav Thanks. Its working.