Finetuning EfficientNet in Pytorch

Hi everyone,

I would like to use an EfficientNet for image classification. Since my inputimage has 6 instead of 3 channels, I guess I need to change some layers. Currently I define my model as follows:

class Classifier(nn.Module):
def init(self,n_classes = 4):
super(Classifier, self).init()
self.effnet = EfficientNet.from_name(‘efficientnet-b4’)
self.l1 = nn.Linear(2000 , 256)
self.dropout = nn.Dropout(0.5)
self.l2 = nn.Linear(256,n_classes) # number of classes is 4
self.relu = nn.LeakyReLU()
def forward(self, input):
x = self.effnet(input)
x = x.view(x.size(0),-1)
x = self.dropout(self.relu(self.l1(x)))
x = self.l2(x)
return x

Can someone tell me how I can change layers of the EfficientNet in pytorch?

You could print the modules via print(self.effnet) and look for the first conv layer in the model.
Once you’ve found it, e.g. as

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

you could replace it using the attribute name:

self.effnet.conv1 = nn.Conv2d(6, 64, 7, 2, 3, bias=False)

to change the number of input channels to 6.
(Note that I have used a ResNet to show the workflow, so the layer name might differ in your case.)

Thanks, that helped a lot!