📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
Update python/ort-gpu docs
SWHLcf00f769deb1
diff --git a/ocrweb/api.py b/ocrweb/api.pyindex bdbea45..e9a9009 100644--- a/ocrweb/api.py+++ b/ocrweb/api.py@@ -3,7 +3,6 @@# @Contact: [email protected]import base64from wsgiref.simple_server import make_server-import jsonimport cv2import numpy as npdiff --git a/ocrweb/config.yaml b/ocrweb/config.yamlindex 9f40a0d..5b84273 100644--- a/ocrweb/config.yaml+++ b/ocrweb/config.yaml@@ -37,6 +37,7 @@ Det:max_candidates: 1000unclip_ratio: 1.6use_dilation: true+ score_mode: fastCls:module_name: ch_ppocr_v2_clsdiff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.pyindex 8ee0fa5..93f09ba 100644--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py+++ b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py@@ -15,7 +15,8 @@ import warningsimport yamlfrom onnxruntime import (get_available_providers, get_device,- SessionOptions, InferenceSession)+ SessionOptions, InferenceSession,+ GraphOptimizationLevel)class OrtInferSession(object):@@ -23,15 +24,19 @@ class OrtInferSession(object):sess_opt = SessionOptions()sess_opt.log_severity_level = 4sess_opt.enable_cpu_mem_arena = False+ sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALLcuda_ep = 'CUDAExecutionProvider'cpu_ep = 'CPUExecutionProvider'+ cpu_provider_options = {+ "arena_extend_strategy": "kSameAsRequested",+ }EP_list = []if config['use_cuda'] and get_device() == 'GPU' \and cuda_ep in get_available_providers():EP_list = [(cuda_ep, config[cuda_ep])]- EP_list.append(cpu_ep)+ EP_list.append((cpu_ep, cpu_provider_options))self.session = InferenceSession(config['model_path'],sess_options=sess_opt,diff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.pyindex 674ed8b..75e7bb7 100644--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py+++ b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py@@ -26,7 +26,8 @@ import siximport yamlfrom shapely.geometry import Polygonfrom onnxruntime import (get_available_providers, get_device,- SessionOptions, InferenceSession)+ SessionOptions, InferenceSession,+ GraphOptimizationLevel)class OrtInferSession(object):@@ -34,15 +35,19 @@ class OrtInferSession(object):sess_opt = SessionOptions()sess_opt.log_severity_level = 4sess_opt.enable_cpu_mem_arena = False+ sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALLcuda_ep = 'CUDAExecutionProvider'cpu_ep = 'CPUExecutionProvider'+ cpu_provider_options = {+ "arena_extend_strategy": "kSameAsRequested",+ }EP_list = []if config['use_cuda'] and get_device() == 'GPU' \and cuda_ep in get_available_providers():EP_list = [(cuda_ep, config[cuda_ep])]- EP_list.append(cpu_ep)+ EP_list.append((cpu_ep, cpu_provider_options))self.session = InferenceSession(config['model_path'],sess_options=sess_opt,@@ -152,19 +157,19 @@ class DetResizeForTest(object):self.image_shape = kwargs['image_shape']self.resize_type = 1elif 'limit_side_len' in kwargs:- self.limit_side_len = kwargs['limit_side_len']+ self.limit_side_len = kwargs.get('limit_side_len', 736)self.limit_type = kwargs.get('limit_type', 'min')if 'resize_long' in kwargs:self.resize_type = 2self.resize_long = kwargs.get('resize_long', 960)else:- self.limit_side_len = 736- self.limit_type = 'min'+ self.limit_side_len = kwargs.get('limit_side_len', 736)+ self.limit_type = kwargs.get('limit_type', 'min')def __call__(self, data):img = data['image']- src_h, src_w, _ = img.shape+ src_h, src_w = img.shape[:2]if self.resize_type == 0:# img, shape = self.resize_image_type0(img)@@ -196,7 +201,7 @@ class DetResizeForTest(object):img, (ratio_h, ratio_w)"""limit_side_len = self.limit_side_len- h, w, _ = img.shape+ h, w = img.shape[:2]# limit the max sideif self.limit_type == 'max':@@ -233,7 +238,7 @@ class DetResizeForTest(object):return img, [ratio_h, ratio_w]def resize_image_type2(self, img):- h, w, _ = img.shape+ h, w = img.shape[:2]resize_w = wresize_h = h@@ -299,12 +304,14 @@ class DBPostProcess(object):box_thresh=0.7,max_candidates=1000,unclip_ratio=2.0,+ score_mode="fast",use_dilation=False):self.thresh = threshself.box_thresh = box_threshself.max_candidates = max_candidatesself.unclip_ratio = unclip_ratioself.min_size = 3+ self.score_mode = score_modeif use_dilation:self.dilation_kernel = np.array([[1, 1], [1, 1]])@@ -337,7 +344,10 @@ class DBPostProcess(object):if sside < self.min_size:continuepoints = np.array(points)- score = self.box_score_fast(pred, points.reshape(-1, 2))+ if self.score_mode == "fast":+ score = self.box_score_fast(pred, points.reshape(-1, 2))+ else:+ score = self.box_score_slow(pred, contour)if self.box_thresh > score:continue@@ -390,10 +400,10 @@ class DBPostProcess(object):def box_score_fast(self, bitmap, _box):h, w = bitmap.shape[:2]box = _box.copy()- xmin = np.clip(np.floor(box[:, 0].min()).astype(np.int), 0, w - 1)- xmax = np.clip(np.ceil(box[:, 0].max()).astype(np.int), 0, w - 1)- ymin = np.clip(np.floor(box[:, 1].min()).astype(np.int), 0, h - 1)- ymax = np.clip(np.ceil(box[:, 1].max()).astype(np.int), 0, h - 1)+ xmin = np.clip(np.floor(box[:, 0].min()).astype(np.int32), 0, w - 1)+ xmax = np.clip(np.ceil(box[:, 0].max()).astype(np.int32), 0, w - 1)+ ymin = np.clip(np.floor(box[:, 1].min()).astype(np.int32), 0, h - 1)+ ymax = np.clip(np.ceil(box[:, 1].max()).astype(np.int32), 0, h - 1)mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8)box[:, 0] = box[:, 0] - xmin@@ -401,6 +411,27 @@ class DBPostProcess(object):cv2.fillPoly(mask, box.reshape(1, -1, 2).astype(np.int32), 1)return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]+ def box_score_slow(self, bitmap, contour):+ '''+ box_score_slow: use polyon mean score as the mean score+ '''+ h, w = bitmap.shape[:2]+ contour = contour.copy()+ contour = np.reshape(contour, (-1, 2))++ xmin = np.clip(np.min(contour[:, 0]), 0, w - 1)+ xmax = np.clip(np.max(contour[:, 0]), 0, w - 1)+ ymin = np.clip(np.min(contour[:, 1]), 0, h - 1)+ ymax = np.clip(np.max(contour[:, 1]), 0, h - 1)++ mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8)++ contour[:, 0] = contour[:, 0] - xmin+ contour[:, 1] = contour[:, 1] - ymin++ cv2.fillPoly(mask, contour.reshape(1, -1, 2).astype(np.int32), 1)+ return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]+def __call__(self, pred, shape_list):pred = pred[:, 0, :, :]segmentation = pred > self.threshdiff --git a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.pyindex 8f697c1..7576fcf 100644--- a/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py+++ b/ocrweb/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py@@ -6,7 +6,8 @@ import warningsimport numpy as npimport yamlfrom onnxruntime import (get_available_providers, get_device,- SessionOptions, InferenceSession)+ SessionOptions, InferenceSession,+ GraphOptimizationLevel)class OrtInferSession(object):@@ -14,16 +15,19 @@ class OrtInferSession(object):sess_opt = SessionOptions()sess_opt.log_severity_level = 4sess_opt.enable_cpu_mem_arena = False+ sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALLcuda_ep = 'CUDAExecutionProvider'cpu_ep = 'CPUExecutionProvider'+ cpu_provider_options = {+ "arena_extend_strategy": "kSameAsRequested",+ }EP_list = []if config['use_cuda'] and get_device() == 'GPU' \and cuda_ep in get_available_providers():EP_list = [(cuda_ep, config[cuda_ep])]-- EP_list.append(cpu_ep)+ EP_list.append((cpu_ep, cpu_provider_options))self.session = InferenceSession(config['model_path'],sess_options=sess_opt,@@ -120,5 +124,5 @@ class CTCLabelDecode(object):else:conf_list.append(1)text = ''.join(char_list)- result_list.append((text, np.mean(conf_list + [1e-20])))+ result_list.append((text, np.mean(conf_list + [1e-10] )))return result_listdiff --git a/python/README.md b/python/README.mdindex ce4e24f..5caaf64 100755--- a/python/README.md+++ b/python/README.md@@ -277,29 +277,16 @@ print(rec_res)python tests/benchmark/benchmark.py --yaml_path config_gpu.yaml```3. 运行相关信息汇总:(以下仅为个人测试情况,具体情况请自行测试)-- - 来自[zhsunlight](https://github.com/zhsunlight)的测试,感谢- - 设备型号:宏碁(Acer) 暗影骑士·威N50-N93游戏台式机- - OS: Windows- - CPU型号:十代i5-10400F 16G 512G SSD- - GPU型号:NVIDIA GeForce GTX 1660Super 6G- - onnxruntime-gpu: 1.11.0- - 耗时情况:- |设备|总耗时(s)|平均耗时(s/img)|- |:---:|:---:|:---:|- |CPU|296.8841|1.18282|- |GPU|646.14667|2.57429|- - 来自[SWHL](https://github.com/SWHL)的测试- - 设备型号:Docker- - OS: Linux Ubuntu- - CPU型号:-- - GPU型号:NVIDIA V100S 16G- - onnxruntime-gpu: 1.7.0- - 耗时情况:- |设备|总耗时(s)|平均耗时(s/img)|- |:---:|:---:|:---:|- |CPU|1079.4726|4.3001|- |GPU|525.8244|2.0989|+ - 环境+ |测试者|设备|OS|CPU|GPU|onnxruntime-gpu|+ |:--|:--|:--|:--|:--|:--|+ |[1][zhsunlight](https://github.com/zhsunlight)|宏碁(Acer) 暗影骑士·威N50-N93游戏台式机|Windows|十代i5-10400F 16G 512G SSD|NVIDIA GeForce GTX 1660Super 6G|1.11.0|+ |[2][SWHL](https://github.com/SWHL)|服务器|Linux|-|NVIDIA GeForce RTX 3090|1.12.1|+ - 耗时+ |对应上面序号|CPU总耗时(s)|CPU平均耗时(s/img)|GPU总耗时(s)|GPU平均耗时(s/img)||+ |:---:|:---:|:---:|:---:|:---:|:---:|+ |[1]|296.8841|1.18282|646.14667|2.57429|+ |[2]|149.35427|0.50504|250.81760|0.99927|### OpenVINO GPU推理配置- 官方参考文档:[docs](https://docs.openvino.ai/latest/api/ie_python_api/_autosummary/openvino.runtime.Core.html?highlight=compile_model#openvino.runtime.Core.compile_model)diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.pyindex d44c08b..93f09ba 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v2_cls/utils.py@@ -15,7 +15,8 @@ import warningsimport yamlfrom onnxruntime import (get_available_providers, get_device,- SessionOptions, InferenceSession)+ SessionOptions, InferenceSession,+ GraphOptimizationLevel)class OrtInferSession(object):@@ -23,6 +24,7 @@ class OrtInferSession(object):sess_opt = SessionOptions()sess_opt.log_severity_level = 4sess_opt.enable_cpu_mem_arena = False+ sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALLcuda_ep = 'CUDAExecutionProvider'cpu_ep = 'CPUExecutionProvider'diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.pyindex c108a97..75e7bb7 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_det/utils.py@@ -26,7 +26,8 @@ import siximport yamlfrom shapely.geometry import Polygonfrom onnxruntime import (get_available_providers, get_device,- SessionOptions, InferenceSession)+ SessionOptions, InferenceSession,+ GraphOptimizationLevel)class OrtInferSession(object):@@ -34,6 +35,7 @@ class OrtInferSession(object):sess_opt = SessionOptions()sess_opt.log_severity_level = 4sess_opt.enable_cpu_mem_arena = False+ sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALLcuda_ep = 'CUDAExecutionProvider'cpu_ep = 'CPUExecutionProvider'diff --git a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.pyindex 392e659..7576fcf 100644--- a/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py+++ b/python/rapidocr_onnxruntime/ch_ppocr_v3_rec/utils.py@@ -6,7 +6,8 @@ import warningsimport numpy as npimport yamlfrom onnxruntime import (get_available_providers, get_device,- SessionOptions, InferenceSession)+ SessionOptions, InferenceSession,+ GraphOptimizationLevel)class OrtInferSession(object):@@ -14,6 +15,7 @@ class OrtInferSession(object):sess_opt = SessionOptions()sess_opt.log_severity_level = 4sess_opt.enable_cpu_mem_arena = False+ sess_opt.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALLcuda_ep = 'CUDAExecutionProvider'cpu_ep = 'CPUExecutionProvider'