Cooduck / OCR-Invoice_Identification

基于CTPN+CRNN的OCR项目,用于发票识别
6 stars 1 forks source link

你好楼主,我在训练crnn的时候报以下错误,怎么解决呢? #3

Open szzwy opened 7 months ago

szzwy commented 7 months ago

Traceback (most recent call last): File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/train_pytorch_ctc.py", line 177, in val(crnn, test_dataset, criterion, loss_avg.val()) File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/train_pytorch_ctc.py", line 44, in val num_correct, num_all = val_model(config.val_infofile,net,True,log_file='compare-'+config.saved_model_prefix+'.log') File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/crnn_train/online_test.py", line 32, in val_model res = val_on_image(img,model,gpu) File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/crnn_train/online_test.py", line 65, in val_on_image preds = model( image ) File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl return self._call_impl(*args, kwargs) File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl return forward_call(*args, *kwargs) File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/crnn_train/crnn.py", line 70, in forward x = self.pool1(self.relu1(self.conv1(input))) File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl return self._call_impl(args, kwargs) File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl return forward_call(*args, **kwargs) File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/conv.py", line 460, in forward return self._conv_forward(input, self.weight, self.bias) File "/root/miniconda3/lib/python3.10/site-packages/torch/nn/modules/conv.py", line 456, in _conv_forward return F.conv2d(input, weight, bias, self.stride, RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

我有用显卡

Cooduck commented 7 months ago

你好,如果你使用的是显卡进行训练,请将config.py文件中cuda=False改为cuda=True。若还是不行,请参考这篇博客 https://blog.csdn.net/qq_38832757/article/details/113630383

szzwy commented 7 months ago

OK了,还有一个小问题,我跑完epoch后为啥它没有输出训练好的权重呢? 楼主假期快乐:) ![Uploading screenshot-1714486516334.png…]()

Cooduck commented 7 months ago

你好,不好意思,是我忘记上传预训练模型了,导致训练时的loss很大、accuracy很低,所以就没有保存权重。 https://drive.google.com/file/d/1WjiL7T4cq2Ug7KGBsEWf1tzee2OZeRqw/view?usp=sharing 请在上条链接中下载预训练模型并解压到工作目录中,再进行训练,应该就没问题了。 也祝你假期愉快:)

szzwy commented 7 months ago

楼主,我测试成功了,但是很奇怪,跟ctpn时候一样换上我的数据集又不行了报错如下: 65293 Not Covering Char: - - 65293 Traceback (most recent call last): File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/train_pytorch_ctc.py", line 166, in cost = trainBatch(crnn, criterion, optimizer, train_iter) File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/train_pytorch_ctc.py", line 63, in trainBatch text, length = converter.encode(cpu_texts) File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/crnntrain/utils.py", line 139, in encode text, = self.encode(text) File "/root/OCR-Invoice_Identification-master/train_code/crnn_train_code/crnn_train/utils.py", line 146, in encode return (torch.IntTensor(text), torch.IntTensor(length)) TypeError: new(): invalid data type 'str'

格式应该是没问题的 screenshot-1714552058782

szzwy commented 7 months ago

txt的编码方式我也换成了ansi,但是还是不行,是在不会了T-T

Cooduck commented 7 months ago

你好,这个问题出在用于字符转整数索引的字典中没有"-"这个字符,即这句报错信息”Not Covering Char: - - 65293“。 你需要将"-"字符添加到字典中,具体操作为把在keys.py的文件中,把
alphabet_list = pkl.load(open('crnn_train/alphabet.pkl','rb')) alphabet = [ord(ch) for ch in alphabet_list] 改为 alphabet_list = pkl.load(open('crnn_train/alphabet.pkl','rb')) alphabet_list.append("-") alphabet = [ord(ch) for ch in alphabet_list]
此外,由于预训练模型使用的是原本的字典,这个修改后的字典会导致预训练模型无法使用,所以需要将train_pytorch_ctc.py中config.pretrained_model = './CRNN-1010.pth'改为config.pretrained_model = '',即不使用预训练模型进行训练。

szzwy commented 6 months ago

我的好楼主,都搞定了,但但但是,它又不输出权重了T-T 这个loos也不大

屏幕截图 2024-05-03 111939
Cooduck commented 6 months ago

你好,不输出权重是因为我设置了精确度到了0.9以上才保存权重,你可以通过增大迭代次数和调整学习率来增大精确度。一般达到0.95以上精确度识别效果才不错

szzwy commented 6 months ago

哈喽楼主,这几天弄论文给我弄嘛麻了.... 想问一下你ctpn和crnn的预训练模型权重是有基于ICDAR数据集进行训练吗

Cooduck commented 6 months ago

你好,我ctpn没有用到预训练模型,是直接拿 https://www.datafountain.cn/competitions/544 这里的数据集进行训练的,然后crnn的预训练模型就不太清楚了,是直接从 https://github.com/courao/ocr.pytorch 这个项目的预训练模型拿来用的,最终效果也不错,能达到98%以上的精度。