forward() takes 1 positional argument but 2 were given
I'm trying to build a Model using EfficientNet-B0.
The details of the Model are shown in the code below.
I got the following error when I tried to learn.

TypeError                                 Traceback (most recent call last)
'''
&lt;ipython-input-17-fb3850894108&gt; in forward(self, *x)
     24         #x: bs*N x 3 x 128 x 128
     25         print(x.shape)     #([384, 3, 224, 224])
---&gt; 26         x = self.enc(x)
     27         #x: bs*N x C x 4 x 4
     28         shape = x.shape

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/container.py in forward(self, input)
     98     def forward(self, input):
     99         for module in self:
--&gt; 100             input = module(input)
    101         return input
    102 

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

TypeError: forward() takes 1 positional argument but 2 were given


I suspect that m.children() may have an effect.
If anyone knows the cause of this error, please let me know.
Thank you.

class Model(nn.Module):
    def __init__(self, arch='efficientnet-b0', n=6, pre=True):
        super().__init__()
        m = EfficientNet.from_pretrained('efficientnet-b0')
        #print(*list(m.children())
        nc = m._fc.in_features
        print(nc)
        self.enc = nn.Sequential(*list(m.children())[:-2])
        #nc = list(m.children())[-2].in_features
        self.head = nn.Sequential(*list(m.children())[-2:])
        self.head._fc = nn.Linear(nc, n)
        #self.enc = nn.Sequential(*list(m.children()))
        #print('fc_infeatures : {}'.format(nc))
        #self.head = nn.Sequential(Flatten(),nn.Linear(nc,512),
        #                    relu(),nn.BatchNorm1d(512), nn.Dropout(0.5),nn.Linear(512,n))


    def forward(self, *x):
        print(x[0].shape)  #([32, 3, 224, 224])
        shape = x[0].shape
        n = len(x)           # n = 12
        #torch.stack直後では32*12*3*224*224(bs x N x 3 x 224 x 224)
        x = torch.stack(x,1).view(-1,shape[-3],shape[-2],shape[-1])
        #x: bs*N x 3 x 128 x 128
        print(x.shape)     #([384, 3, 224, 224])
        x = self.enc(x)
        #x: bs*N x C x 4 x 4
        shape = x.shape
        #concatenate the output for tiles into a single map
        x = x.view(-1,n,shape[1],shape[2],shape[3]).permute(0,2,1,3,4).contiguous()\
          .view(-1,shape[1],shape[2]*n,shape[3])
        #x: bs x C x N*4 x 4
        x = self.head(x)
        return x



Now you can easily get the network without the last layers by using the include_top parameter:
m = EfficientNet.from_pretrained('efficientnet-b0', include_top=False)

What it does, as can be easily seen in the code, is not calling forward method for the last layers (AveragePool, Dropout, FC).
Other alternative approaches can be summarized as followings:

Define a new MyEfficientNet that override the forward method to avoid calling the last layers

Overwrite the layers you don't need with an identity layer. For example:
m._fc = nn.Identity()


The first approach should be preferred when using the efficientnet as a features extractor while the others should be used when more customisation is needed.
