EfficientNet-PyTorch icon indicating copy to clipboard operation
EfficientNet-PyTorch copied to clipboard

Missing keys when loading pretrained weights

Open BavarianToolbox opened this issue 3 years ago • 7 comments

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

BavarianToolbox avatar Apr 15 '21 19:04 BavarianToolbox

I had the same problem

domef avatar Apr 22 '21 08:04 domef

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 https://github.com/lukemelas/EfficientNet-PyTorch/pull/208/a78e84ec3a3072f68172a0f3e94f343df8f08b80

lirus7 avatar Apr 27 '21 04:04 lirus7

Same problem for me

FrancescoMandru avatar Apr 27 '21 12:04 FrancescoMandru

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

BavarianToolbox avatar May 05 '21 22:05 BavarianToolbox

hello all, please check out this PR https://github.com/lukemelas/EfficientNet-PyTorch/pull/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])

kurnianggoro avatar May 25 '21 07:05 kurnianggoro

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.

kieranatkins avatar Jul 07 '21 10:07 kieranatkins

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

model._fc = nn.Identity()

Ant-Bru avatar Sep 23 '21 20:09 Ant-Bru