crnn.pytorch icon indicating copy to clipboard operation
crnn.pytorch copied to clipboard

KeyError: 'rnn.1.embedding.weight'

Open Amore-HDU opened this issue 7 years ago • 9 comments

First, I train a model use very large datasets. I want use this model to fine tuning my new datasets with the same keys.But when code load pre model, it occurs loading pretrained model from ./GenModel/netCRNN_447_10.pth Traceback (most recent call last): File "crnn_main.py", line 110, in if len(model_dict[weig1]) == len(pre_trainmodel[weig1]) and len(model_dict[bias1]) == len(pre_trainmodel[bias1]): KeyError: 'rnn.1.embedding.weight' @meijieru

Amore-HDU avatar Sep 27 '17 09:09 Amore-HDU

@meijieru can you help me to solve this problem?Thank you.

Amore-HDU avatar Sep 28 '17 04:09 Amore-HDU

Occur when run demo?

meijieru avatar Oct 11 '17 06:10 meijieru

Occur when run train @meijieru

Amore-HDU avatar Oct 18 '17 01:10 Amore-HDU

use .get on dictionary instead of indexing it will solve the error

VenkteshV avatar Feb 07 '18 16:02 VenkteshV

change this line text = [ self.dict[char.lower() if self._ignore_case else char] for char in text ]

in utils.py to

text = [ self.dict.get(char.lower() if self._ignore_case else char) for char in text ]

VenkteshV avatar Feb 07 '18 16:02 VenkteshV

@meijieru @Amore-HDU the error is caused by inconsistent name between defined model and saved mode.(*.pth) Name of params in saved mode is added prefix 'module.'. I have saved this problom below.

  1. first, define a function:

def load_pretrained_model(model, pretrained_param): model_param = model.state_dict() assert(len(model_param) == len(pretrained_param)) for i in model_param.keys(): model_param[i] = pretrained_param['module.'+i] model.load_state_dict(model_param) 2. crnn_main.py change the line: crnn.load_state_dict(torch.load(opt.crnn)) to: load_pretrained_model(crnn, torch.load(opt.crnn))

zhengmzong avatar Feb 26 '18 08:02 zhengmzong

I do not solve the problem using the method ,could you help me . @zhengmzong

DLUTfangping avatar May 14 '18 12:05 DLUTfangping

Did you solve the problem? @Amore-HDU

DLUTfangping avatar May 14 '18 13:05 DLUTfangping

I have solved it by the code: default

bianlongpeng avatar Sep 25 '18 07:09 bianlongpeng