Open justcodew opened 11 months ago
将PULC模型库中的文字行方向分类模型转换成onnx,使用链接中的推理模型。
转换onnx脚本:
python tools/export_model.py -c ./ppcls/configs/PULC/textline_orientation/PPLCNet_x1_0.yaml -o Global.checkpoints=./textline_orientation_infer/inference paddle2onnx --model_dir .inference --model_filename inference.pdmodel --params_filename inference.pdiparams --save_file textline_orientation.onnx --opset_version 11 --enable_onnx_checker True
推理代码:
import onnxruntime as ort import numpy as np import cv2 import glob import os angle_list = ['0_degree', '180_degree'] def preproc(img, input_size): img = img[:, :, ::-1] #BGR to RGB img = cv2.resize(img, input_size,interpolation=1) #unified resize mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] mean = np.array(mean).reshape((1, 1, 3)).astype('float32') #broadcast std = np.array(std).reshape((1, 1, 3)).astype('float32') #broadcast img = (img.astype('float32') * np.float32(1.0/255.0) - mean) / std #normalize scale:1.0/255.0 img = img.transpose(2, 0, 1).astype('float32') #whc to chw return img def get_angle(ort_sess,img_path): x = cv2.imdecode(np.fromfile(img_path,dtype=np.uint8),1) x = preproc(x, (160,80)) #w,h x = x.reshape(1,3,80,160) #batch1 outputs = ort_sess.run(None, {'x': x}) res = outputs[0][0] angle = angle_list[np.argmax(res)] print('res,angle ',res,angle) return np.argmax(res) if __name__ == "__main__": onnx_path = r'textline_orientation.onnx' ort_sess = ort.InferenceSession(onnx_path,providers=[ 'CPUExecutionProvider']) src_dir = r'./data/ort_line_data' files = glob.glob(src_dir+'/*.jpg') + glob.glob(src_dir+'/*.png') for i in range(0,len(files)): img_path = files[i] angle = get_angle(ort_sess,img_path)
测试数据: PULC 快速体验数据
4张图 onnx 输出结果
res,angle [0.07300492 0.92699504] 180_degree res,angle [0.0833722 0.91662776] 180_degree res,angle [0.09312499 0.90687495] 180_degree res,angle [0.03547611 0.9645239 ] 180_degree
而paddle直接推理的结果是正确的: paddleclas --model_name=textline_orientation --infer_imgs=./data ppcls INFO: class_ids: [1], scores: [1.0], label_names: ['180_degree'] ppcls INFO: class_ids: [0], scores: [1.0], label_names: ['0_degree'] ppcls INFO: class_ids: [1], scores: [1.0], label_names: ['180_degree'] ppcls INFO: class_ids: [0], scores: [1.0], label_names: ['0_degree']
这个感觉比较奇怪呢,我们定位下问题
请问这个问题解决了吗?
将PULC模型库中的文字行方向分类模型转换成onnx,使用链接中的推理模型。
转换onnx脚本:
推理代码:
测试数据: PULC 快速体验数据
4张图 onnx 输出结果
res,angle [0.07300492 0.92699504] 180_degree res,angle [0.0833722 0.91662776] 180_degree res,angle [0.09312499 0.90687495] 180_degree res,angle [0.03547611 0.9645239 ] 180_degree
而paddle直接推理的结果是正确的: paddleclas --model_name=textline_orientation --infer_imgs=./data ppcls INFO: class_ids: [1], scores: [1.0], label_names: ['180_degree'] ppcls INFO: class_ids: [0], scores: [1.0], label_names: ['0_degree'] ppcls INFO: class_ids: [1], scores: [1.0], label_names: ['180_degree'] ppcls INFO: class_ids: [0], scores: [1.0], label_names: ['0_degree']