📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
The torch engine adds support for Ascend NPU (#557)
yuege969eb6c6856163c
diff --git a/python/rapidocr/config.yaml b/python/rapidocr/config.yamlindex b43d622..6779fc3 100644--- a/python/rapidocr/config.yaml+++ b/python/rapidocr/config.yaml@@ -64,6 +64,8 @@ EngineConfig:torch:use_cuda: falsegpu_id: 0+ use_npu: false+ npu_id: 0Det:engine_type: "onnxruntime"diff --git a/python/rapidocr/inference_engine/torch.py b/python/rapidocr/inference_engine/torch.pyindex fc913ff..a2c48dc 100644--- a/python/rapidocr/inference_engine/torch.py+++ b/python/rapidocr/inference_engine/torch.py@@ -53,19 +53,34 @@ class TorchInferSession(InferSession):arch_config = all_arch_config.get(file_name)self.predictor = BaseModel(arch_config)- self.predictor.load_state_dict(torch.load(model_path, weights_only=False))+ self.predictor.load_state_dict(torch.load(model_path, map_location="cpu", weights_only=False))self.predictor.eval()-self.use_gpu = False+ self.use_npu = Falseif cfg.engine_cfg.use_cuda:self.device = torch.device(f"cuda:{cfg.engine_cfg.gpu_id}")self.predictor.to(self.device)self.use_gpu = True+ elif cfg.engine_cfg.use_npu:+ try:+ import torch_npu+ options = {+ # 设定算子编译的磁盘缓存模式,非必要每次重新编译+ "ACL_OP_COMPILER_CACHE_MODE": "enable",+ # 指定缓存目录,确保路径已存在+ "ACL_OP_COMPILER_CACHE_DIR": "./kernel_meta",+ }+ torch_npu.npu.set_option(options)+ except ImportError:+ self.logger.warning("torch_npu is not installed, options with ACL setting failed.")+ self.device = torch.device(f"npu:{cfg.engine_cfg.npu_id}")+ self.predictor.to(self.device)+ self.use_npu = Truedef __call__(self, img: np.ndarray):with torch.no_grad():inp = torch.from_numpy(img)- if self.use_gpu:+ if self.use_gpu or self.use_npu:inp = inp.to(self.device)# 适配跟onnx对齐取值逻辑