Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotImplementedError when use forward function. #38

Closed
henanjun opened this issue Jun 25, 2019 · 15 comments
Closed

NotImplementedError when use forward function. #38

henanjun opened this issue Jun 25, 2019 · 15 comments

Comments

@henanjun
Copy link

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:

@henanjun henanjun changed the title NotImplementedError when use foward function. NotImplementedError when use forward function. Jun 25, 2019
@lukemelas
Copy link
Owner

What version of PyTorch are you using?

@henanjun
Copy link
Author

Hi, my PyTorch version is 1.1.0.

@lukemelas
Copy link
Owner

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.

@henanjun
Copy link
Author

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

@lukemelas
Copy link
Owner

Sorry about that, try this:
https://colab.research.google.com/drive/1C2vRryOEA95wOLmc56LhF6l1EwM-MeBf

@henanjun
Copy link
Author

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

@lukemelas
Copy link
Owner

Happy to help!

@tmabraham
Copy link

Is there an actual solution for this?

@tienthegainz
Copy link

@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))

@smatsumt
Copy link

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

@smatsumt
Copy link

@lukemelas If the above comment is correct, I'm glad if you update Colaboratory code https://colab.research.google.com/drive/1C2vRryOEA95wOLmc56LhF6l1EwM-MeBf

@lukemelas
Copy link
Owner

Done! :)

@etoilestar
Copy link

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?

@emma-rose22
Copy link

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

@sparshgarg23
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants