EfficientNet-PyTorch
EfficientNet-PyTorch copied to clipboard
Error when loading pretrained model with num_classes!=1000
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?
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
Upgrading PyTorch version solved my problem! Thank you very much! Is there any sample or hyper-parameters configuration available?
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
Same for me. Python 3.5 torch 1.1 - every new loading pretrained weights I have to reinstall torch or efnet.
U can try this: from efficientnet_pytorch import EfficientNet model = EfficientNet.from_pretrained('efficientnet-b5') model._fc.out_features = 45 print(model)
AttributeError: 'EfficientNet' object has no attribute 'conv1'
I am getting this error, why?