π Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
fix: align the performance of the torch version of the PP-OCRv6 Rec tiny model
SWHLb64a46ffd4fc
diff --git a/python/rapidocr/default_models.yaml b/python/rapidocr/default_models.yamlindex e1b031f..a5474dc 100644--- a/python/rapidocr/default_models.yaml+++ b/python/rapidocr/default_models.yaml@@ -802,8 +802,8 @@ torch:SHA256: 40c40ff2d432888f53697d73b37d8b280c65ad66696d6412834a90b40cebc81drec:multi_PP-OCRv6_rec_tiny:- model_dir: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.9.1/torch/PP-OCRv6/rec/PP-OCRv6_rec_tiny.pth- SHA256: 82a9b90ec19bcdbc6f2cd54f7b08394aeb01d947b9dfc43e483b90fb30238b85+ model_dir: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/torch/PP-OCRv6/rec/PP-OCRv6_rec_tiny.pth+ SHA256: dceca3cd44855565e9c10391f22767aa2937c2195df3ce7dd0d9d3d5b6bfa011dict_url: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.9.1/paddle/PP-OCRv6/rec/PP-OCRv6_rec_tiny/ppocrv6_tiny_dict.txtmulti_PP-OCRv6_rec_small:model_dir: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.9.1/torch/PP-OCRv6/rec/PP-OCRv6_rec_small.pthdiff --git a/python/rapidocr/inference_engine/pytorch/networks/arch_config.yaml b/python/rapidocr/inference_engine/pytorch/networks/arch_config.yamlindex f92ce39..30c51cc 100644--- a/python/rapidocr/inference_engine/pytorch/networks/arch_config.yaml+++ b/python/rapidocr/inference_engine/pytorch/networks/arch_config.yaml@@ -9,6 +9,8 @@ PP-OCRv6_rec_tiny:name: MultiHeadout_channels_list:CTCLabelDecode: 6906 #'blank' + ...(6904) + ' '+ NRTRLabelDecode: 6909+ build_gtc_head: falsehead_list:- CTCHead:Neck:@@ -16,6 +18,7 @@ PP-OCRv6_rec_tiny:Head:mid_channels: 80use_guide: true+ guide_layer_type: ppocrv6_tinyfc_decay: 0.00001- NRTRHead:nrtr_dim: 384diff --git a/python/rapidocr/inference_engine/pytorch/networks/backbones/__init__.py b/python/rapidocr/inference_engine/pytorch/networks/backbones/__init__.pyindex ea18e42..eb4cfcc 100644--- a/python/rapidocr/inference_engine/pytorch/networks/backbones/__init__.py+++ b/python/rapidocr/inference_engine/pytorch/networks/backbones/__init__.py@@ -23,16 +23,14 @@ def build_backbone(config, model_type):from .rec_lcnetv4 import PPLCNetV4from .rec_pphgnetv2 import PPHGNetV2_B4- support_dict = [- "MobileNetV3",- "ResNet",- "ResNet_vd",- "ResNet_SAST",- "PPLCNetV3",- "PPHGNet_small",- "PPHGNetV2_B4",- "PPLCNetV4",- ]+ support_dict = {+ "MobileNetV3": MobileNetV3,+ "PPLCNetV3": PPLCNetV3,+ "PPLCNetV4": PPLCNetV4,+ "PPHGNet_small": PPHGNet_small,+ "PPHGNetV2_B4": PPHGNetV2_B4,+ }+elif model_type == "rec" or model_type == "cls":from .rec_hgnet import PPHGNet_smallfrom .rec_lcnetv3 import PPLCNetV3@@ -45,22 +43,16 @@ def build_backbone(config, model_type):from .rec_pplcnet import PPLCNetfrom .rec_svtrnet import SVTRNet- support_dict = [- "MobileNetV1Enhance",- "MobileNetV3",- "ResNet",- "ResNetFPN",- "MTB",- "ResNet31",- "SVTRNet",- "ViTSTR",- "DenseNet",- "PPLCNetV3",- "PPHGNet_small",- "PPHGNetV2_B4",- "PPLCNetV4",- "PPLCNet",- ]+ support_dict = {+ "MobileNetV1Enhance": MobileNetV1Enhance,+ "MobileNetV3": MobileNetV3,+ "SVTRNet": SVTRNet,+ "PPLCNetV3": PPLCNetV3,+ "PPLCNetV4": PPLCNetV4,+ "PPHGNet_small": PPHGNet_small,+ "PPHGNetV2_B4": PPHGNetV2_B4,+ "PPLCNet": PPLCNet,+ }else:raise NotImplementedError@@ -70,5 +62,5 @@ def build_backbone(config, model_type):model_type, support_dict))- module_class = eval(module_name)(**config)+ module_class = support_dict[module_name](**config)return module_classdiff --git a/python/rapidocr/inference_engine/pytorch/networks/heads/rec_ctc_head.py b/python/rapidocr/inference_engine/pytorch/networks/heads/rec_ctc_head.pyindex d11c7eb..dda4502 100644--- a/python/rapidocr/inference_engine/pytorch/networks/heads/rec_ctc_head.py+++ b/python/rapidocr/inference_engine/pytorch/networks/heads/rec_ctc_head.py@@ -11,27 +11,44 @@ class CTCHead(nn.Module):mid_channels=None,return_feats=False,use_guide=False,+ guide_layer_type="default",**kwargs,):super(CTCHead, self).__init__()self.use_guide = use_guideif use_guide:- # Depthwise-separable Conv1d block:- # DW Conv1d(k=5, groups=in_channels) β BN β ReLU β PW Conv1d(k=1) β BN- self.guide_layer = nn.Sequential(- nn.Conv1d(- in_channels,- in_channels,- 5,- padding=2,- groups=in_channels,- bias=True,- ),- nn.BatchNorm1d(in_channels),- nn.ReLU(),- nn.Conv1d(in_channels, in_channels, 1, bias=True),- nn.BatchNorm1d(in_channels),- )+ if guide_layer_type == "ppocrv6_tiny":+ self.guide_layer = nn.Sequential(+ nn.Conv1d(+ in_channels,+ in_channels,+ 5,+ padding=2,+ groups=in_channels,+ bias=False,+ ),+ nn.BatchNorm1d(in_channels),+ nn.Hardswish(),+ nn.Conv1d(in_channels, in_channels, 1, bias=False),+ nn.BatchNorm1d(in_channels),+ nn.Hardswish(),+ )+ else:+ self.guide_layer = nn.Sequential(+ nn.Conv1d(+ in_channels,+ in_channels,+ 5,+ padding=2,+ groups=in_channels,+ bias=True,+ ),+ nn.BatchNorm1d(in_channels),+ nn.ReLU(),+ nn.Conv1d(in_channels, in_channels, 1, bias=True),+ nn.BatchNorm1d(in_channels),+ )+if mid_channels is None:self.fc = nn.Linear(in_channels,diff --git a/python/rapidocr/inference_engine/pytorch/networks/heads/rec_multi_head.py b/python/rapidocr/inference_engine/pytorch/networks/heads/rec_multi_head.pyindex cd14349..a104f73 100644--- a/python/rapidocr/inference_engine/pytorch/networks/heads/rec_multi_head.py+++ b/python/rapidocr/inference_engine/pytorch/networks/heads/rec_multi_head.py@@ -1,7 +1,10 @@+import copy+from torch import nnfrom ..necks.rnn import Im2Seq, SequenceEncoderfrom .rec_ctc_head import CTCHead+from .rec_nrtr_head import Transformerclass FCTranspose(nn.Module):@@ -20,7 +23,8 @@ class FCTranspose(nn.Module):class MultiHead(nn.Module):def __init__(self, in_channels, out_channels_list, **kwargs):super().__init__()- self.head_list = kwargs.pop("head_list")+ self.head_list = copy.deepcopy(kwargs.pop("head_list"))+ self.build_gtc_head = kwargs.pop("build_gtc_head", False)self.gtc_head = "sar"assert len(self.head_list) >= 2@@ -29,7 +33,25 @@ class MultiHead(nn.Module):if name == "SARHead":passelif name == "NRTRHead":- pass+ if not self.build_gtc_head:+ continue+ gtc_args = self.head_list[idx][name]+ max_text_length = gtc_args.get("max_text_length", 25)+ nrtr_dim = gtc_args.get("nrtr_dim", 256)+ num_decoder_layers = gtc_args.get("num_decoder_layers", 4)+ self.before_gtc = nn.Sequential(+ nn.Flatten(2), FCTranspose(in_channels, nrtr_dim)+ )+ self.gtc_head = Transformer(+ d_model=nrtr_dim,+ nhead=nrtr_dim // 32,+ num_encoder_layers=-1,+ beam_size=-1,+ num_decoder_layers=num_decoder_layers,+ max_len=max_text_length,+ dim_feedforward=nrtr_dim * 4,+ out_channels=out_channels_list["NRTRLabelDecode"],+ )elif name == "CTCHead":# ctc neckself.encoder_reshape = Im2Seq(in_channels)@@ -65,6 +87,6 @@ class MultiHead(nn.Module):sar_out = self.sar_head(x, data[1:])["res"]head_out["sar"] = sar_outelse:- gtc_out = self.gtc_head(self.before_gtc(x), data[1:])["res"]+ gtc_out = self.gtc_head(self.before_gtc(x), data[1:])head_out["nrtr"] = gtc_outreturn head_outdiff --git a/python/rapidocr/inference_engine/pytorch/networks/necks/__init__.py b/python/rapidocr/inference_engine/pytorch/networks/necks/__init__.pyindex bc5c479..9252952 100644--- a/python/rapidocr/inference_engine/pytorch/networks/necks/__init__.py+++ b/python/rapidocr/inference_engine/pytorch/networks/necks/__init__.py@@ -19,24 +19,18 @@ def build_neck(config):from .db_fpn import DBFPN, LKPAN, RSEFPN, RepLKFPN, RepLKPANfrom .rnn import SequenceEncoder- support_dict = [- "FPN",- "DBFPN",- "EASTFPN",- "SASTFPN",- "SequenceEncoder",- "PGFPN",- "TableFPN",- "RSEFPN",- "LKPAN",- "FCEFPN",- "RepLKFPN",- "RepLKPAN",- ]+ support_dict = {+ "DBFPN": DBFPN,+ "SequenceEncoder": SequenceEncoder,+ "RSEFPN": RSEFPN,+ "LKPAN": LKPAN,+ "RepLKFPN": RepLKFPN,+ "RepLKPAN": RepLKPAN,+ }module_name = config.pop("name")assert module_name in support_dict, Exception("neck only support {}".format(support_dict))- module_class = eval(module_name)(**config)+ module_class = support_dict[module_name](**config)return module_classdiff --git a/python/rapidocr/inference_engine/pytorch/networks/necks/db_fpn.py b/python/rapidocr/inference_engine/pytorch/networks/necks/db_fpn.pyindex 9fcf01c..512ea13 100644--- a/python/rapidocr/inference_engine/pytorch/networks/necks/db_fpn.py+++ b/python/rapidocr/inference_engine/pytorch/networks/necks/db_fpn.py@@ -24,8 +24,9 @@ class DSConv(nn.Module):**kwargs,):super(DSConv, self).__init__()- if groups == None:+ if groups is None:groups = in_channels+self.if_act = if_actself.act = actself.conv1 = nn.Conv2d(