lukemelas/EfficientNet-PyTorch

NotImplementedError when use forward function.

henanjun opened this issue ยท 15 comments

I try define a model based on pretrained EfficientNet as below. But I get a NotImplementedError: when use 'forward' function. However, when I use other pretrained CNN e.g., resnet18 from torchvision, there is no such problem. Can anyone help me? Thanks a lot

'Model definition'
class EfficientNet_scene(nn.Module):

def __init__(self,model_name='efficientnet-b0',class_num=45,initfc_type='normal',gain=0.2):
    super(EfficientNet_scene, self).__init__()
    model = EfficientNet.from_pretrained(model_name)
    aul = [*model.children()]
    self.features = nn.Sequential(*aul[:-1])
    self.fc = nn.Linear(aul[-1].in_features,class_num)

    if hasattr(self.fc, 'bias') and self.fc.bias is not None:
        nn.init.constant_(self.fc.bias.data, 0.0)
    if initfc_type == 'normal':
        nn.init.normal_(self.fc.weight.data, 0.0, gain)
    elif initfc_type == 'xavier':
        nn.init.xavier_normal_(self.fc.weight.data, gain=gain)
    elif initfc_type == 'kaiming':
        nn.init.kaiming_normal_(self.fc.weight.data, a=0, mode='fan_in')
    elif initfc_type == 'orthogonal':
        nn.init.orthogonal_(self.fc.weight.data, gain=gain)

def forward(self,x):
    x = self.features(x)
    x = self.fc(x)
    return x

net = EfficientNet_scene()
image = torch.randn(1,3,224,224)
b = net(image)

Error information:
NotImplementedError Traceback (most recent call last)
in ()
37 print(net)
38 image = torch.randn(1,3,224,224)
---> 39 b = net(image)
40 print(b)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

in forward(self, x)
30
31 def forward(self,x):
---> 32 x = self.features(x)
33 x = x.reshape(x.size(0), -1)
34 x = self.fc(x)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
90 def forward(self, input):
91 for module in self._modules.values():
---> 92 input = module(input)
93 return input
94

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
491 result = self._slow_forward(*input, **kwargs)
492 else:
--> 493 result = self.forward(*input, **kwargs)
494 for hook in self._forward_hooks.values():
495 hook_result = hook(self, input, result)

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in forward(self, *input)
86 registered hooks while the latter silently ignores them.
87 """
---> 88 raise NotImplementedError
89
90 def register_buffer(self, name, tensor):

NotImplementedError:

What version of PyTorch are you using?

Hi, my PyTorch version is 1.1.0.

Thanks for the quick reply. I've replicated your issue and I agree it's odd.

For now, you can use model.extract_features to get the features instead of stacking the layers yourself. For example: https://colab.research.google.com/drive/1C2vRryOEA95wOLmc56LhF6l1EwM-MeBf?authuser=2#scrollTo=8O54Fd0ccgmJ&uniqifier=1

I agree that at the moment, this is a little hackier than I would like.

Hi, Thanks for your kind reply. However, I meet a problem when enter the link above.

Notebook loading error
There was an error loading this notebook. Ensure that the file is accessible and try again.
Invalid Credentials

It solves my problem. Thank you very much. :)

Happy to help!

Is there an actual solution for this?

@lukemelas your solution doesn't seem to run correctly anymore. It got:
RuntimeError: Given groups=1, weight of size 1280 320 1 1, expected input[1, 1280, 7, 7] to have 320 channels, but got 1280 channels instead
in:
x = self.model._bn1(self.model._conv_head(x))

I delete x = self.model._bn1(self.model._conv_head(x)), and it looks work fine!

Now extract_feature did it already, so we don't need it. The change is made by the commit In bd3c392

def extract_features(self, inputs):
""" Returns output of the final convolution layer """
# Stem
x = relu_fn(self._bn0(self._conv_stem(inputs)))
# Blocks
for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx) / len(self._blocks)
x = block(x, drop_connect_rate=drop_connect_rate)
# Head
x = relu_fn(self._bn1(self._conv_head(x)))
return x

Done! :)

thanks for your answer, but it seems that extract_features is a function not in module, so we can't modify the parameters in these networks, right?

Can this colab notebook be made available again? I am experiencing the same issue as OP

seems the colab notebook is not available,i am facing the same issue