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

Error when loading pretrained model with num_classes!=1000

Open CosimoRulli opened this issue 4 years ago • 6 comments

I am trying to load a pretrained model with a different number of classes to apply transfer learning on cifar10. When I run

model = EfficientNet.from_pretrained('efficientnet-b1', num_classes=23)

I got this error:

AttributeError Traceback (most recent call last) in ----> 1 model = EfficientNet.from_pretrained('efficientnet-b1', num_classes=23)

~/anaconda3/envs/compression/lib/python3.6/site-packages/efficientnet_pytorch/model.py in from_pretrained(cls, model_name, num_classes) 194 def from_pretrained(cls, model_name, num_classes=1000): 195 model = EfficientNet.from_name(model_name, override_params={'num_classes': num_classes}) --> 196 load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000)) 197 return model 198

~/anaconda3/envs/compression/lib/python3.6/site-packages/efficientnet_pytorch/utils.py in load_pretrained_weights(model, model_name, load_fc) 294 state_dict.pop('_fc.bias') 295 res = model.load_state_dict(state_dict, strict=False) --> 296 assert str(res.missing_keys) == str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights' 297 print('Loaded pretrained weights for {}'.format(model_name))

AttributeError: 'NoneType' object has no attribute 'missing_keys'

Pytorch version 1.0.1. Can anyone help me?

CosimoRulli avatar Jul 12 '19 15:07 CosimoRulli

Hi @CosimoRulli ,

Perhaps it's a PyTorch version thing?

I made a Colab to test and it is working (at least with PyTorch 1.1): https://colab.research.google.com/drive/1WOWwWK73y_fdVOVU_9GZVICB6jTcTnC3

lukemelas avatar Jul 12 '19 16:07 lukemelas

Upgrading PyTorch version solved my problem! Thank you very much! Is there any sample or hyper-parameters configuration available?

CosimoRulli avatar Jul 13 '19 07:07 CosimoRulli

It's a problem with python's version - I am running torch 1.1.0 everywhere, however in python 3.5 I get: >>> model = EfficientNet.from_pretrained('efficientnet-b7', num_classes=8) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/vakko/EfficientNet-PyTorch/efficientnet_pytorch/model.py", line 196, in from_pretrained load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000)) File "/home/vakko/EfficientNet-PyTorch/efficientnet_pytorch/utils.py", line 298, in load_pretrained_weights assert str(res.missing_keys) == str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights' AssertionError: issue loading pretrained weights

while in python 3.6 it loads just fine.... any ideas on this?

P.S. it obviously works if you comment the assertion out but for some reason "res.missing_keys" does not even exist in python 3.5

vakkov avatar Aug 02 '19 14:08 vakkov

Same for me. Python 3.5 torch 1.1 - every new loading pretrained weights I have to reinstall torch or efnet.

oleg-yaroshevskiy avatar Aug 06 '19 19:08 oleg-yaroshevskiy

U can try this: from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b5') model._fc.out_features = 45 print(model)

shiqi1994 avatar Feb 18 '20 08:02 shiqi1994

AttributeError: 'EfficientNet' object has no attribute 'conv1'

I am getting this error, why?

Taitai6521 avatar May 09 '21 11:05 Taitai6521