📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
refactor: refactor paddle inference code (#622)
SWHL5cd7de1e47f7
diff --git a/python/rapidocr/config.yaml b/python/rapidocr/config.yamlindex 173b455..14eaa65 100644--- a/python/rapidocr/config.yaml+++ b/python/rapidocr/config.yaml@@ -59,6 +59,13 @@ EngineConfig:use_npu: falsenpu_ep_cfg:device_id: 0+ envs:+ FLAGS_npu_jit_compile: 0+ FLAGS_use_stride_kernel: 0+ FLAGS_allocator_strategy: "auto_growth"+ CUSTOM_DEVICE_BLACK_LIST: "pad3d,pad3d_grad,set_value,set_value_with_tensor"+ FLAGS_npu_scale_aclnn: "True"+ FLAGS_npu_split_aclnn: "True"use_cuda: falsecuda_ep_cfg:diff --git a/python/rapidocr/inference_engine/paddle.py b/python/rapidocr/inference_engine/paddle.pydeleted file mode 100644index 5ae6008..0000000--- a/python/rapidocr/inference_engine/paddle.py+++ /dev/null@@ -1,267 +0,0 @@-# -*- encoding: utf-8 -*--# @Author: SWHL-# @Contact: [email protected]-import os-import platform-from pathlib import Path-from typing import Optional, Tuple--import numpy as np-import paddle-from omegaconf.errors import ConfigKeyError-from paddle import inference--from ..utils.download_file import DownloadFile, DownloadFileInput-from ..utils.log import logger-from ..utils.typings import OCRVersion-from .base import FileInfo, InferSession---class PaddleInferSession(InferSession):- def __init__(self, cfg, mode: Optional[str] = None) -> None:- self.mode = mode-- # 设置设备特定的环境变量- self._set_device_envs(cfg.engine_cfg)-- pdmodel_path, pdiparams_path = self._setup_model(cfg)- if cfg.ocr_version == OCRVersion.PPOCRV5:- self._init_predictor_v2(cfg, pdmodel_path, pdiparams_path)- else:- self._init_predictor_v1(cfg, pdmodel_path, pdiparams_path)-- def _set_device_envs(self, engine_cfg):- """设置设备特定的环境变量"""-- def _set_envs(envs):- for key, val in envs.items():- os.environ[key] = val- logger.debug(f"{key} has been set to {val}.")-- if engine_cfg.get("use_cuda", False):- envs = {}- _set_envs(envs)-- elif not any(- [- engine_cfg.get(f"use_{dev}", False)- for dev in ["cuda", "npu", "xpu", "mlu", "dcu", "gcu"]- ]- ):- envs = {}- _set_envs(envs)-- # NPU 环境变量- elif engine_cfg.get("use_npu", False):- envs = {- "FLAGS_npu_jit_compile": "0",- "FLAGS_use_stride_kernel": "0",- "FLAGS_allocator_strategy": "auto_growth",- "CUSTOM_DEVICE_BLACK_LIST": "pad3d,pad3d_grad,set_value,set_value_with_tensor",- "FLAGS_npu_scale_aclnn": "True",- "FLAGS_npu_split_aclnn": "True",- }- _set_envs(envs)-- def _setup_model(self, cfg) -> Tuple[Path, Path]:- pdmodel_name = "inference.json"- pdmodel_name_v2 = "inference.pdmodel"- pdiparams_name = "inference.pdiparams"-- model_dir = cfg.get("model_dir", None)- if model_dir is None:- model_info = self.get_model_url(- FileInfo(- engine_type=cfg.engine_type,- ocr_version=cfg.ocr_version,- task_type=cfg.task_type,- lang_type=cfg.lang_type,- model_type=cfg.model_type,- )- )- default_model_dir = model_info["model_dir"]-- try:- pdmodel_path = self.download_model(- model_info, default_model_dir, pdmodel_name- )- except ConfigKeyError as e:- pdmodel_path = self.download_model(- model_info, default_model_dir, pdmodel_name_v2- )- except Exception as e:- raise PaddleInferError(f"Download model error: {e}") from e-- pdiparams_path = self.download_model(- model_info, default_model_dir, pdiparams_name- )-- logger.info(f"Using {pdmodel_path}")- logger.info(f"Using {pdiparams_path}")- return pdmodel_path, pdiparams_path-- model_dir = Path(model_dir)- pdmodel_path = model_dir / pdmodel_name- pdiparams_path = model_dir / pdiparams_name- if not pdmodel_path.exists():- pdmodel_path = model_dir / pdmodel_name_v2- self._verify_model(pdmodel_path)- self._verify_model(pdiparams_path)-- logger.info(f"Using {pdmodel_path}")- logger.info(f"Using {pdiparams_path}")- return pdmodel_path, pdiparams_path-- def download_model(- self, model_info, default_model_dir: str, model_file_name: str- ) -> Path:- model_file_url = f"{default_model_dir}/{model_file_name}"- model_file_path = (- self.DEFAULT_MODEL_PATH / Path(default_model_dir).name / model_file_name- )- DownloadFile.run(- DownloadFileInput(- file_url=model_file_url,- sha256=model_info[model_file_name],- save_path=model_file_path,- logger=logger,- )- )- return model_file_path-- def _init_predictor_v1(self, cfg, pdmodel_path, pdiparams_path):- infer_opts = inference.Config(str(pdmodel_path), str(pdiparams_path))-- if cfg.engine_cfg.use_cuda:- gpu_id = self.get_infer_gpuid()- if gpu_id is None:- logger.warning(- "GPU is not found in current device by nvidia-smi. Please check your device or ignore it if run on jetson."- )- device_id = cfg.engine_cfg.gpu_id- infer_opts.enable_use_gpu(cfg.engine_cfg.gpu_mem, device_id)- logger.info(f"Using GPU device with ID: {device_id}")-- elif cfg.engine_cfg.use_npu:- npu_id = cfg.engine_cfg.get("npu_id", 0)- infer_opts.enable_custom_device("npu", npu_id)- logger.info(f"Using NPU device with ID: {npu_id}")-- else: # CPU- infer_opts.disable_gpu()- logger.info("Using CPU device")-- cpu_nums = os.cpu_count()- infer_num_threads = cfg.engine_cfg.get("cpu_math_library_num_threads", -1)- if infer_num_threads != -1 and 1 <= infer_num_threads <= cpu_nums:- infer_opts.set_cpu_math_library_num_threads(infer_num_threads)- logger.info(f"Set CPU math library threads to: {infer_num_threads}")-- # enable memory optim- infer_opts.enable_memory_optim()- infer_opts.disable_glog_info()- infer_opts.delete_pass("conv_transpose_eltwiseadd_bn_fuse_pass")- infer_opts.delete_pass("matmul_transpose_reshape_fuse_pass")- infer_opts.switch_use_feed_fetch_ops(False)- infer_opts.switch_ir_optim(True)-- self.predictor = inference.create_predictor(infer_opts)-- def _init_predictor_v2(self, cfg, pdmodel_path, pdiparams_path):- infer_opts = inference.Config(str(pdmodel_path), str(pdiparams_path))-- if cfg.engine_cfg.use_cuda:- gpu_id = self.get_infer_gpuid()- if gpu_id is None:- logger.warning(- "GPU is not found in current device by nvidia-smi. Please check your device or ignore it if run on jetson."- )- device_id = cfg.engine_cfg.gpu_id- infer_opts.enable_use_gpu(cfg.engine_cfg.gpu_mem, device_id)- logger.info(f"Using GPU device with ID: {device_id}")-- elif cfg.engine_cfg.use_npu:- npu_id = cfg.engine_cfg.get("npu_id", 0)- infer_opts.enable_custom_device("npu", npu_id)- logger.info(f"Using NPU device with ID: {npu_id}")-- else: # CPU- infer_opts.disable_gpu()- if hasattr(infer_opts, "disable_mkldnn"):- infer_opts.disable_mkldnn()- logger.info("Using CPU device")-- cpu_nums = os.cpu_count()- infer_num_threads = cfg.engine_cfg.get("cpu_math_library_num_threads", -1)- if infer_num_threads != -1 and 1 <= infer_num_threads <= cpu_nums:- infer_opts.set_cpu_math_library_num_threads(infer_num_threads)- logger.info(f"Set CPU math library threads to: {infer_num_threads}")-- if hasattr(infer_opts, "enable_new_ir"):- infer_opts.enable_new_ir(True)-- if hasattr(infer_opts, "enable_new_executor"):- infer_opts.enable_new_executor()- infer_opts.set_optimization_level(3)-- infer_opts.enable_memory_optim()- self.predictor = inference.create_predictor(infer_opts)-- def __call__(self, img: np.ndarray):- input_tensor = self.get_input_tensors()- output_tensors = self.get_output_tensors()-- input_tensor.copy_from_cpu(img)- self.predictor.run()-- outputs = []- for output_tensor in output_tensors:- output = output_tensor.copy_to_cpu()- outputs.append(output)-- self.predictor.try_shrink_memory()- return outputs[0]-- def get_input_tensors(self):- input_names = self.predictor.get_input_names()- for name in input_names:- input_tensor = self.predictor.get_input_handle(name)- return input_tensor-- def get_output_tensors(self):- output_names = self.predictor.get_output_names()- if self.mode == "rec":- output_name = "softmax_0.tmp_0"- if output_name in output_names:- return [self.predictor.get_output_handle(output_name)]-- output_tensors = []- for output_name in output_names:- output_tensor = self.predictor.get_output_handle(output_name)- output_tensors.append(output_tensor)- return output_tensors-- @staticmethod- def get_infer_gpuid():- sysstr = platform.system()- if sysstr == "Windows":- return 0-- if not paddle.device.is_compiled_with_rocm:- cmd = "env | grep CUDA_VISIBLE_DEVICES"- else:- cmd = "env | grep HIP_VISIBLE_DEVICES"- env_cuda = os.popen(cmd).readlines()-- if len(env_cuda) == 0:- return 0-- gpu_id = env_cuda[0].strip().split("=")[1]- return int(gpu_id[0])-- def have_key(self, key: str = "character") -> bool:- return False---class PaddleInferError(Exception):- passdiff --git a/python/rapidocr/inference_engine/paddle/__init__.py b/python/rapidocr/inference_engine/paddle/__init__.pynew file mode 100644index 0000000..a3a0185--- /dev/null+++ b/python/rapidocr/inference_engine/paddle/__init__.py@@ -0,0 +1,4 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+from .main import PaddleInferError, PaddleInferSessiondiff --git a/python/rapidocr/inference_engine/paddle/device_config.py b/python/rapidocr/inference_engine/paddle/device_config.pynew file mode 100644index 0000000..9acddb5--- /dev/null+++ b/python/rapidocr/inference_engine/paddle/device_config.py@@ -0,0 +1,119 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+import os+import platform++import paddle++from ...utils.log import logger+from ...utils.typings import OCRVersion+++class DeviceConfig:+ def __init__(self, cfg, infer_opts, ocr_version):+ self.use_cuda = cfg.engine_cfg.use_cuda+ self.use_npu = cfg.engine_cfg.use_npu++ self.cfg = cfg+ self.infer_opts = infer_opts+ self.ocr_version = ocr_version++ def setup_device(self):+ if self.use_cuda:+ self.config_cuda(self.cfg.engine_cfg.cuda_ep_cfg)+ return self.infer_opts++ if self.use_npu:+ self.config_npu(self.cfg.engine_cfg.npu_ep_cfg)+ return self.infer_opts++ self.config_cpu()+ return self.infer_opts++ def config_cuda(self, ep_cfg):+ if not self.check_cuda():+ raise DeviceConfigError("CUDA is not available.")++ gpu_id = self.get_infer_gpuid()+ if gpu_id is None:+ raise DeviceConfigError(+ "CUDA is not found in current device by nvidia-smi. Please check your device or ignore it if run on jetson."+ )++ self.infer_opts.enable_use_gpu(ep_cfg.gpu_mem, ep_cfg.device_id)+ logger.info(f"Using CUDA device with ID: {ep_cfg.device_id}")++ def config_npu(self, ep_cfg):+ self.setup_device_envs(ep_cfg.envs)++ npu_id = ep_cfg.device_id+ self.infer_opts.enable_custom_device("npu", npu_id)+ logger.info(f"Using NPU device with ID: {npu_id}")++ def config_cpu(self):+ self.infer_opts.disable_gpu()+ if hasattr(self.infer_opts, "disable_mkldnn"):+ self.infer_opts.disable_mkldnn()+ logger.info("Using CPU device")++ cpu_nums = os.cpu_count()+ infer_num_threads = self.cfg.get("cpu_math_library_num_threads", -1)+ if infer_num_threads != -1 and 1 <= infer_num_threads <= cpu_nums:+ self.infer_opts.set_cpu_math_library_num_threads(infer_num_threads)++ logger.info(f"Set CPU math library threads to: {infer_num_threads}")++ if self.ocr_version == OCRVersion.PPOCRV5:+ if hasattr(self.infer_opts, "enable_new_ir"):+ self.infer_opts.enable_new_ir(True)++ if hasattr(self.infer_opts, "enable_new_executor"):+ self.infer_opts.enable_new_executor()+ self.infer_opts.set_optimization_level(3)++ @staticmethod+ def setup_device_envs(envs):+ for key, val in envs.items():+ os.environ[key] = str(val)+ logger.info(f"{key} has been set to {val}.")++ @staticmethod+ def check_cuda() -> bool:+ if paddle.is_compiled_with_cuda():+ device = paddle.get_device()+ if device.startswith("gpu"):+ logger.info(f"GPU is available. Current device: {device}")+ return True++ logger.warning(+ "PaddlePaddle was compiled with CUDA support, but no GPU is currently available."+ )+ return False++ logger.warning(+ "PaddlePaddle was not compiled with CUDA support (CPU-only version)."+ )+ return False++ @staticmethod+ def get_infer_gpuid() -> int:+ sysstr = platform.system()+ if sysstr == "Windows":+ return 0++ if not paddle.device.is_compiled_with_rocm:+ cmd = "env | grep CUDA_VISIBLE_DEVICES"+ else:+ cmd = "env | grep HIP_VISIBLE_DEVICES"+ env_cuda = os.popen(cmd).readlines()++ if len(env_cuda) == 0:+ return 0++ gpu_id = env_cuda[0].strip().split("=")[1]+ return int(gpu_id[0])+++class DeviceConfigError(Exception):+ passdiff --git a/python/rapidocr/inference_engine/paddle/main.py b/python/rapidocr/inference_engine/paddle/main.pynew file mode 100644index 0000000..e2a04f3--- /dev/null+++ b/python/rapidocr/inference_engine/paddle/main.py@@ -0,0 +1,147 @@+# -*- encoding: utf-8 -*-+# @Author: SWHL+# @Contact: [email protected]+from pathlib import Path+from typing import Optional, Tuple++import numpy as np+from omegaconf.errors import ConfigKeyError+from paddle import inference++from ...utils.download_file import DownloadFile, DownloadFileInput+from ...utils.log import logger+from ...utils.typings import OCRVersion+from ..base import FileInfo, InferSession+from .device_config import DeviceConfig+++class PaddleInferSession(InferSession):+ def __init__(self, cfg, mode: Optional[str] = None) -> None:+ self.mode = mode++ pdmodel_path, pdiparams_path = self.setup_model(cfg)+ infer_opts = inference.Config(str(pdmodel_path), str(pdiparams_path))+ infer_opts = DeviceConfig(cfg, infer_opts, cfg.ocr_version).setup_device()++ self.predictor = self.init_predictor(infer_opts, cfg.ocr_version)++ def __call__(self, img: np.ndarray):+ input_tensor = self.get_input_tensors()+ output_tensors = self.get_output_tensors()++ input_tensor.copy_from_cpu(img)+ self.predictor.run()++ outputs = []+ for output_tensor in output_tensors:+ output = output_tensor.copy_to_cpu()+ outputs.append(output)++ self.predictor.try_shrink_memory()+ return outputs[0]++ def get_input_tensors(self):+ input_names = self.predictor.get_input_names()+ for name in input_names:+ input_tensor = self.predictor.get_input_handle(name)+ return input_tensor++ def get_output_tensors(self):+ output_names = self.predictor.get_output_names()+ if self.mode == "rec":+ output_name = "softmax_0.tmp_0"+ if output_name in output_names:+ return [self.predictor.get_output_handle(output_name)]++ output_tensors = []+ for output_name in output_names:+ output_tensor = self.predictor.get_output_handle(output_name)+ output_tensors.append(output_tensor)+ return output_tensors++ def setup_model(self, cfg) -> Tuple[Path, Path]:+ pdmodel_name = "inference.json"+ pdmodel_name_v2 = "inference.pdmodel"+ pdiparams_name = "inference.pdiparams"++ model_dir = cfg.get("model_dir", None)+ if model_dir is None:+ model_info = self.get_model_url(+ FileInfo(+ engine_type=cfg.engine_type,+ ocr_version=cfg.ocr_version,+ task_type=cfg.task_type,+ lang_type=cfg.lang_type,+ model_type=cfg.model_type,+ )+ )+ default_model_dir = model_info["model_dir"]++ try:+ pdmodel_path = self.download_model(+ model_info, default_model_dir, pdmodel_name+ )+ except ConfigKeyError as e:+ pdmodel_path = self.download_model(+ model_info, default_model_dir, pdmodel_name_v2+ )+ except Exception as e:+ raise PaddleInferError(f"Download model error: {e}") from e++ pdiparams_path = self.download_model(+ model_info, default_model_dir, pdiparams_name+ )++ logger.info(f"Using {pdmodel_path}")+ logger.info(f"Using {pdiparams_path}")+ return pdmodel_path, pdiparams_path++ model_dir = Path(model_dir)+ pdmodel_path = model_dir / pdmodel_name+ pdiparams_path = model_dir / pdiparams_name+ if not pdmodel_path.exists():+ pdmodel_path = model_dir / pdmodel_name_v2+ self._verify_model(pdmodel_path)+ self._verify_model(pdiparams_path)++ logger.info(f"Using {pdmodel_path}")+ logger.info(f"Using {pdiparams_path}")+ return pdmodel_path, pdiparams_path++ def download_model(+ self, model_info, default_model_dir: str, model_file_name: str+ ) -> Path:+ model_file_url = f"{default_model_dir}/{model_file_name}"+ model_file_path = (+ self.DEFAULT_MODEL_PATH / Path(default_model_dir).name / model_file_name+ )+ DownloadFile.run(+ DownloadFileInput(+ file_url=model_file_url,+ sha256=model_info[model_file_name],+ save_path=model_file_path,+ logger=logger,+ )+ )+ return model_file_path++ def init_predictor(self, infer_opts, ocr_version):+ if ocr_version == OCRVersion.PPOCRV5:+ infer_opts.enable_memory_optim()+ return inference.create_predictor(infer_opts)++ infer_opts.enable_memory_optim()+ infer_opts.disable_glog_info()+ infer_opts.delete_pass("conv_transpose_eltwiseadd_bn_fuse_pass")+ infer_opts.delete_pass("matmul_transpose_reshape_fuse_pass")+ infer_opts.switch_use_feed_fetch_ops(False)+ infer_opts.switch_ir_optim(True)++ return inference.create_predictor(infer_opts)++ def have_key(self, key: str = "character") -> bool:+ return False+++class PaddleInferError(Exception):+ passdiff --git a/python/rapidocr/utils/typings.py b/python/rapidocr/utils/typings.pyindex c35d29e..67aa2db 100644--- a/python/rapidocr/utils/typings.py+++ b/python/rapidocr/utils/typings.py@@ -8,6 +8,10 @@ class DeviceType(Enum):CPU = "cpu"CUDA = "cuda"NPU = "npu"+ XPU = "xpu"+ MLU = "mlu"+ DCU = "dcu"+ GCU = "gcu"class LangDet(Enum):diff --git a/python/tests/test_engine.py b/python/tests/test_engine.pyindex 99c0106..3bf8d0f 100644--- a/python/tests/test_engine.py+++ b/python/tests/test_engine.py@@ -68,10 +68,15 @@ def test_engine_openvino():assert result.txts[0] == "正品促销"-def test_engine_paddle():+ "ocr_version,gt",+ [(OCRVersion.PPOCRV4, "正品促销"), (OCRVersion.PPOCRV5, "大桶装更划算")],+)+def test_engine_paddle(ocr_version, gt):engine = RapidOCR(params={"Det.engine_type": EngineType.PADDLE,+ "Det.ocr_version": ocr_version,"Cls.engine_type": EngineType.PADDLE,"Rec.engine_type": EngineType.PADDLE,}@@ -79,7 +84,7 @@ def test_engine_paddle():result = engine(img_path)assert result.txts is not None- assert result.txts[0] == "正品促销"+ assert result.txts[0] == gtdef test_engine_torch():