From f6bb96ca1d4eb57232e65b42771be21e64ffcc5d Mon Sep 17 00:00:00 2001 From: SWHL Date: Tue, 12 Apr 2022 09:22:09 +0800 Subject: [PATCH] Update python openvino --- python/README.md | 5 ++-- .../ch_ppocr_mobile_v2_cls/text_cls.py | 2 +- python/onnxruntime_infer/requirements.txt | 7 +++-- python/openvino_infer/README.md | 22 ++++++++-------- .../ch_ppocr_mobile_v2_cls/text_cls.py | 21 ++++++++++----- python/openvino_infer/requirements.txt | 7 +++++ python/openvino_infer/test_demo.py | 2 +- python/openvino_infer/test_demo.sh | 16 ++++++++++++ python/openvino_infer/test_openvino.py | 26 ++++++++++++------- 9 files changed, 73 insertions(+), 35 deletions(-) create mode 100644 python/openvino_infer/requirements.txt create mode 100644 python/openvino_infer/test_demo.sh diff --git a/python/README.md b/python/README.md index 356e479..c95a823 100755 --- a/python/README.md +++ b/python/README.md @@ -4,8 +4,9 @@ ### TODO - [ ] 模型转INT8,正在尝试 -- [ ] 其他推理模型代码整理 -- [ ] 模型转换代码整理 +- [ ] OpenVINO推理方向分类模型有误,已经提了[issue](https://github.com/openvinotoolkit/openvino/issues/11501) +- [x] 其他推理模型代码整理 +- [x] 模型转换代码整理 ### 关于OpenVINO - OpenVINO可以直接推理IR、ONNX和PaddlePaddle模型,具体如下(图来源:[link](https://docs.openvino.ai/latest/openvino_docs_OV_UG_OV_Runtime_User_Guide.html#doxid-openvino-docs-o-v-u-g-o-v-runtime-user-guide)): diff --git a/python/onnxruntime_infer/ch_ppocr_mobile_v2_cls/text_cls.py b/python/onnxruntime_infer/ch_ppocr_mobile_v2_cls/text_cls.py index e66b84f..6c706cd 100644 --- a/python/onnxruntime_infer/ch_ppocr_mobile_v2_cls/text_cls.py +++ b/python/onnxruntime_infer/ch_ppocr_mobile_v2_cls/text_cls.py @@ -106,9 +106,9 @@ class TextClassifier(object): norm_img = norm_img[np.newaxis, :] norm_img_batch.append(norm_img) norm_img_batch = np.concatenate(norm_img_batch) - onnx_inputs = {self.session.get_inputs()[0].name: norm_img_batch} starttime = time.time() + onnx_inputs = {self.session.get_inputs()[0].name: norm_img_batch} prob_out = self.session.run(None, onnx_inputs)[0] cls_result = self.postprocess_op(prob_out) diff --git a/python/onnxruntime_infer/requirements.txt b/python/onnxruntime_infer/requirements.txt index 3bccfbb..fe8d176 100644 --- a/python/onnxruntime_infer/requirements.txt +++ b/python/onnxruntime_infer/requirements.txt @@ -1,8 +1,7 @@ pyclipper==1.2.0 Shapely==1.7.1 -onnxruntime==1.7.0 +onnxruntime>=1.7.0 opencv_python==4.5.1.48 six==1.15.0 -numpy==1.19.2 -Pillow==8.3.0 -openvino==2022.1.0 \ No newline at end of file +numpy>=1.19.2 +Pillow==8.3.0 \ No newline at end of file diff --git a/python/openvino_infer/README.md b/python/openvino_infer/README.md index 7310bd2..cc8d650 100644 --- a/python/openvino_infer/README.md +++ b/python/openvino_infer/README.md @@ -7,16 +7,16 @@ pip install openvino==2022.1.0 pip install openvino-dev==2022.1.0 ``` -#### 模型转换为IR -```bash -# 转动态模型 推理时占用内存平均为3.5G左右 -mo --input_model models/ch_PP-OCRv2_det_infer.onnx --output_dir models/IR/ +#### 模型问题 +- 因为OpenVINO可以直接推理ONNX模型,故这里暂时不作转换,直接推理之前ONNX模型即可 +- 这里仍然给出转换的代码,用作参考: + ```bash + mo --input_model models/ch_PP-OCRv2_det_infer.onnx --output_dir models/IR/ -# 转静态模型 推理时占用内存平均为1G左右 -mo --input_model models/ch_PP-OCRv2_det_infer.onnx \ - --output_dir models/IR/static \ - --input_shape "[1,3,12128,800]" -``` + mo --input_model models/ch_PP-OCRv2_det_infer.onnx \ + --output_dir models/IR/static \ + --input_shape "[1,3,12128,800]" + ``` -#### 测试图像下载 -- [long1.jpg](https://drive.google.com/file/d/1iJcGvOVIdUlyOS52bBdvO8uzx8QORo5M/view?usp=sharing) +#### 使用步骤 +同[onnxruntime_infer](../onnxruntime_infer/README.md) \ No newline at end of file diff --git a/python/openvino_infer/ch_ppocr_mobile_v2_cls/text_cls.py b/python/openvino_infer/ch_ppocr_mobile_v2_cls/text_cls.py index e882814..b5f02e0 100644 --- a/python/openvino_infer/ch_ppocr_mobile_v2_cls/text_cls.py +++ b/python/openvino_infer/ch_ppocr_mobile_v2_cls/text_cls.py @@ -18,6 +18,7 @@ import time import cv2 import numpy as np +import onnxruntime as ort from openvino.runtime import Core try: @@ -33,10 +34,15 @@ class TextClassifier(object): self.cls_thresh = 0.9 self.postprocess_op = ClsPostProcess(label_list=['0', '180']) - ie = Core() - model_onnx = ie.read_model(cls_model_path) - compile_model = ie.compile_model(model=model_onnx, device_name='CPU') - self.session = compile_model.create_infer_request() + # ie = Core() + # model_onnx = ie.read_model(cls_model_path) + # compile_model = ie.compile_model(model=model_onnx, device_name='CPU') + # self.session = compile_model.create_infer_request() + + sess_opt = ort.SessionOptions() + sess_opt.log_severity_level = 4 + sess_opt.enable_cpu_mem_arena = False + self.session = ort.InferenceSession(cls_model_path, sess_opt) def resize_norm_img(self, img): imgC, imgH, imgW = self.cls_image_shape @@ -110,8 +116,11 @@ class TextClassifier(object): norm_img_batch = np.concatenate(norm_img_batch) starttime = time.time() - self.session.infer(inputs=[norm_img_batch]) - prob_out = self.session.get_output_tensor().data + onnx_inputs = {self.session.get_inputs()[0].name: norm_img_batch} + prob_out = self.session.run(None, onnx_inputs)[0] + + # self.session.infer(inputs=[norm_img_batch]) + # prob_out = self.session.get_output_tensor().data cls_result = self.postprocess_op(prob_out) elapse += time.time() - starttime diff --git a/python/openvino_infer/requirements.txt b/python/openvino_infer/requirements.txt new file mode 100644 index 0000000..b65da24 --- /dev/null +++ b/python/openvino_infer/requirements.txt @@ -0,0 +1,7 @@ +pyclipper==1.2.0 +Shapely==1.7.1 +opencv_python==4.5.1.48 +six==1.15.0 +numpy>=1.19.2 +Pillow==8.3.0 +openvino==2022.1.0 \ No newline at end of file diff --git a/python/openvino_infer/test_demo.py b/python/openvino_infer/test_demo.py index 70cf959..2f33cee 100644 --- a/python/openvino_infer/test_demo.py +++ b/python/openvino_infer/test_demo.py @@ -16,7 +16,7 @@ text_sys = TextSystem(det_model_path, cls_model_path=cls_model_path, keys_path=keys_path) -image_path = r'test_images/det_images/ch_en_num.jpg' +image_path = r'test_images/ch_en_num.jpg' dt_boxes, rec_res = text_sys(image_path) visualize(image_path, dt_boxes, rec_res) diff --git a/python/openvino_infer/test_demo.sh b/python/openvino_infer/test_demo.sh new file mode 100644 index 0000000..e905249 --- /dev/null +++ b/python/openvino_infer/test_demo.sh @@ -0,0 +1,16 @@ +#! /bin/bash + +det_model_path="models/ch_PP-OCRv2_det_infer.onnx" +cls_model_path="models/ch_ppocr_mobile_v2.0_cls_infer.onnx" + +# 这里可以按照自己需求,做相关更改 +rec_model_path="models/ch_ppocr_mobile_v2.0_rec_infer.onnx" +keys_path="ch_ppocr_mobile_v2_rec/ppocr_keys_v1.txt" + +img_path="test_images/det_images/ch_en_num.jpg" + +python rapid_ocr_api.py --det_model_path ${det_model_path} \ + --cls_model_path ${cls_model_path} \ + --rec_model_path ${rec_model_path} \ + --image_path ${img_path} \ + --keys_path ${keys_path} diff --git a/python/openvino_infer/test_openvino.py b/python/openvino_infer/test_openvino.py index bcb6725..0b78a60 100644 --- a/python/openvino_infer/test_openvino.py +++ b/python/openvino_infer/test_openvino.py @@ -4,6 +4,13 @@ # @Author: SWHL # @Contact: liekkaskono@163.com import numpy as np +from openvino.runtime import Core + +cls_model_path = r'models/ch_ppocr_mobile_v2.0_cls_infer.onnx' +ie = Core() +model_onnx = ie.read_model(cls_model_path) +compile_model = ie.compile_model(model_onnx, device_name='CPU') +pass # Infer ONNXRuntime # import cv2 @@ -32,17 +39,17 @@ import numpy as np # dt_boxes, elapse = text_detect_vino(img) # print(f'openvino ONNX: {elapse}\tdt_boxes:{np.mean(dt_boxes)}') -# # Infer IR -import cv2 -from ch_ppocr_mobile_v2_det_openvino_ir import TextDetector as TextDetectorIR +# # # Infer IR +# import cv2 +# from ch_ppocr_mobile_v2_det_openvino_ir import TextDetector as TextDetectorIR -det_model_path = 'models/IR/static/ch_PP-OCRv2_det_infer.xml' -img_path = 'test_images/long1.jpg' -img = cv2.imread(img_path) +# det_model_path = 'models/IR/static/ch_PP-OCRv2_det_infer.xml' +# img_path = 'test_images/long1.jpg' +# img = cv2.imread(img_path) -text_detect_ir = TextDetectorIR(det_model_path) -dt_boxes, elapse = text_detect_ir(img) -print(f'FP16 openvino IR: {elapse}\tdt_boxes:{np.mean(dt_boxes)}') +# text_detect_ir = TextDetectorIR(det_model_path) +# dt_boxes, elapse = text_detect_ir(img) +# print(f'FP16 openvino IR: {elapse}\tdt_boxes:{np.mean(dt_boxes)}') # Infer paddle 有误 # RuntimeError: Can't SetBlob with name: x, because model input (shape={?,3,960,960}) and blob (shape=(1.3.12128.800)) are incompatible @@ -56,4 +63,3 @@ print(f'FP16 openvino IR: {elapse}\tdt_boxes:{np.mean(dt_boxes)}') # text_detect_ir = TextDetectorPaddle(det_model_path) # dt_boxes, elapse = text_detect_ir(img) # print(f'openvino IR: {elapse}') -