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

Missing keys when loading pretrained weights #278

Open
BavarianToolbox opened this issue Apr 15, 2021 · 7 comments
Open

Missing keys when loading pretrained weights #278

BavarianToolbox opened this issue Apr 15, 2021 · 7 comments

Comments

@BavarianToolbox
Copy link

Python 3.8.7
efficientnet-pytorch==0.7.1

There's an issue loading the pre-trained weights (or any saved weights) when using include_top = False

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0', include_top = False)

Traceback (most recent call last):
  File "test_file.py", line 2, in <module>
    model = EfficientNet.from_pretrained('efficientnet-b0', include_top = False)
  File "/Users/constantinbaumgartner/Desktop/temp_test/test/lib/python3.8/site-packages/efficientnet_pytorch/model.py", line 378, in from_pretrained
    load_pretrained_weights(model, model_name, weights_path=weights_path,
  File "/Users/constantinbaumgartner/Desktop/temp_test/test/lib/python3.8/site-packages/efficientnet_pytorch/utils.py", line 613, in load_pretrained_weights
    assert not ret.unexpected_keys, 'Missing keys when loading pretrained weights: {}'.format(ret.unexpected_keys)
AssertionError: Missing keys when loading pretrained weights: ['_fc.weight', '_fc.bias']

It seems to be related to the latest release 0.7.1 because the issue doesn't present when using the previous version: efficientnet-pytorch==0.7.0

from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0', include_top = False)

Loaded pretrained weights for efficientnet-b0
@domef
Copy link

domef commented Apr 22, 2021

I had the same problem

@lirus7
Copy link

lirus7 commented Apr 27, 2021

With the older version i.e. 0.7.0 the include_top=False doesn't really work. I just tried. The model still had the last fc layers.

You should check out #208

@FrancescoMandru
Copy link

Same problem for me

@BavarianToolbox
Copy link
Author

BavarianToolbox commented May 5, 2021

@lirus7 The include_top=False should work in the earlier version (0.7.0). When you inspect (print) the model it looks like final layers are still part of the architecture but they are in fact ignored during the forward pass when instantiating the model with include_top=False.

You can confirm this by passing a dummy variable through two different version of the model, one with include_top=False, one with include_top=True, and compare the shape of the outputs:

import torch
from efficientnet_pytorch import EfficientNet
model = EfficientNet.from_pretrained('efficientnet-b0')
model_no_top = EfficientNet.from_pretrained('efficientnet-b0', include_top = False)
dummy = torch.randn((1,3,224,224), dtype = torch.float32)
out = model(dummy)
out_no_top = model_no_top(dummy)
print(f'Output shape when include_top = True: {out.shape}')
print(f'Output shape when include_top = False: {out_no_top.shape}')
------------------------------- output -------------------------------
Output shape when include_top = True: torch.Size([1, 1000])
Output shape when include_top = False: torch.Size([1, 1280, 1, 1])

When we include the top the output is as expected: a 2-dimensional 1x1000 tensor, 1 for the batch dimension, 1000 for the possible ImageNet classes. However, when we exclude the top the output is a 4-dimensional tensor: there are two additional dimensions corresponding to the input image width and height dimensions, and the size of the second dimension, 1280, corresponds to the number of output channels in the final convolutional layer of the architecture:

(_conv_head): Conv2dStaticSamePadding(
    320, 1280, kernel_size=(1, 1), stride=(1, 1), bias=False

Hope this helps, please let me know if you're experiencing different behavior when using include_top = False.

@kurnianggoro
Copy link

hello all, please check out this PR #290, it allows the model (latest version) to load the pre-trained weights when include_top is set to False

you can check by running the following code:

from efficientnet_pytorch import EfficientNet
enet = EfficientNet.from_pretrained('efficientnet-b0', include_top=False)

from torch.utils import model_zoo
w = model_zoo.load_url("https://github.com/lukemelas/EfficientNet-PyTorch/releases/download/1.0/efficientnet-b0-355c32eb.pth")

print(w['_conv_stem.weight'][0][0])
print(enet._conv_stem.weight[0][0])

@kieranatkins
Copy link

Hello all, I'm still getting the same error:

assert not ret.unexpected_keys, 'Missing keys when loading pretrained weights {}'.format(ret.unexpected_keys)
AssertionError: Missing keys when loading pretrained weights: ['_fc.weight', '_fc.bias']

when simply running:

backbone = EfficientNet.from_pretrained('efficientnet-b7', include_top=False)

Was a solution ever found? Thanks.

@Ant-Bru
Copy link

Ant-Bru commented Sep 23, 2021

When I don't need the top, in doubt I just set:

model._fc = nn.Identity()

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

7 participants