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

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

Open gcy506067668 opened this issue 6 years ago • 3 comments
trafficstars

when I run your example it comes this:

File "anaconda3/envs/sikuenv/lib/python3.6/site-packages/efficientnet_pytorch/model.py", line 196, in from_pretrained
    load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000))
  File "anaconda3/envs/sikuenv/lib/python3.6/site-packages/efficientnet_pytorch/utils.py", line 298, in load_pretrained_weights
    assert str(res.missing_keys) == str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
AttributeError: 'NoneType' object has no attribute 'missing_keys'

I find this source codes in my python lib:

utils.py ,line 297 ,function model.load_state_dict(state_dict, strict=False) seems return nothing, so variable "res" is NULL, mybe this caused by my torch's version

def load_pretrained_weights(model, model_name, load_fc=True):
    """ Loads pretrained weights, and downloads if loading for the first time. """
    state_dict = model_zoo.load_url(url_map[model_name])
    if load_fc:
        model.load_state_dict(state_dict)
    else:
        state_dict.pop('_fc.weight')
        state_dict.pop('_fc.bias')
        res = model.load_state_dict(state_dict, strict=False)
        assert str(res.missing_keys) == str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
    print('Loaded pretrained weights for {}'.format(model_name))
when I commented line 298,and remove variable res , it is working well again

gcy506067668 avatar Sep 18 '19 12:09 gcy506067668

Almost forgot. Thank you very much for your code.

gcy506067668 avatar Sep 18 '19 12:09 gcy506067668

@gcy506067668 hi, Can you tell me how to solve it? I have the same problem.

yuyifan1991 avatar May 27 '20 08:05 yuyifan1991

@gcy506067668 hi, Can you tell me how to solve it? I have the same problem. 貌似是有些torch版本model.load_state_dict函数不返回任何值,把下面的assert注释掉就可以了

gcy506067668 avatar Aug 11 '20 08:08 gcy506067668