crnn.pytorch
crnn.pytorch copied to clipboard
KeyError: 'rnn.1.embedding.weight'
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
@meijieru can you help me to solve this problem?Thank you.
Occur when run demo?
Occur when run train @meijieru
use .get on dictionary instead of indexing it will solve the error
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 ]
@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.
- 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))
I do not solve the problem using the method ,could you help me . @zhengmzong
Did you solve the problem? @Amore-HDU
I have solved it by the code: