Update python openvino

This commit is contained in:
SWHL committed 2022-04-12 09:22:09 +08:00
1 parent 172f306171
commit f6bb96ca1d
9 files changed
+73 -35

No files matched your search

+3 -2
View File
@@ -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)):
@@ -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)
+3 -4
View File
@@ -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
numpy>=1.19.2
Pillow==8.3.0
+11 -11
View File
@@ -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)
@@ -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
+7
View File
@@ -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
+1 -1
View File
@@ -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)
+16
View File
@@ -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}
+16 -10
View File
@@ -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}')