3

I'm working on fine tuning the first 10 layers of VGG19 net to extract features from images. But I'm getting the below error which I couldn't find a get around:

Traceback (most recent call last):
  File "TODO-train_from_scratch.py", line 390, in <module>
    main()
  File "TODO-train_from_scratch.py", line 199, in main
    model.load_state_dict(weights_load)
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 339, in load_state_dict
    raise KeyError('missing keys in state_dict: "{}"'.format(missing)) 

Corresponding snippet from training code is:

# create model
vgg19 = models.vgg19(pretrained = True)

vgg19_state_dict = vgg19.state_dict()

vgg19_keys = vgg19_state_dict.keys()    

model = get_model()

weights_load = {}   

for i in range(20):
    weights_load[model.state_dict().keys()[i]] = vgg19_state_dict[vgg19_keys[i]]

model.load_state_dict(weights_load)
model = torch.nn.DataParallel(model).cuda()
mkocabas
  • 703
  • 6
  • 19
  • Why not directly use `model.load_state_dict(vg19_state_dict)`? – Vivek Kumar Jun 19 '17 at 12:20
  • Since I just want to use the first 10 layers of VGG19. – mkocabas Jun 19 '17 at 12:30
  • 2
    I think my answer on how to best extract layers from a different model fits your purposes: https://stackoverflow.com/questions/44146655/how-to-convert-pretrained-fc-layers-to-conv-layers-in-pytorch/44410334#44410334 Let me know if not. – mbpaulus Jun 19 '17 at 16:31

0 Answers0