• 在使用paddleocr转onnx后,推理遇到的问题解决


    报错

    if img_h is not None and img_w is not None and img_h > 0 and img_w > 0:
    TypeError: '>' not supported between instances of 'str' and 'int'

    定位一下问题可以看到

    import onnxruntime as ort
    sess = ort.InferenceSession("inference/det_onnx/model.onnx")
    sess.get_inputs()[0].shape
    获取的输入维度如下,不是固定的输入
    ['p2o.DynamicDimension.0', 3, 'p2o.DynamicDimension.1', 'p2o.DynamicDimension.2']

    解决办法:

    1、删掉代码

    predict_det.py line144

    1. # if self.use_onnx:
    2. # img_h, img_w = self.input_tensor.shape[2:]
    3. # if img_h is not None and img_w is not None and img_h > 0 and img_w > 0:
    4. # pre_process_list[0] = {
    5. # 'DetResizeForTest': {
    6. # 'image_shape': [img_h, img_w]
    7. # }
    8. # }

    predict_rec.py line174

    1. # if self.use_onnx:
    2. # w = self.input_tensor.shape[3:][0]
    3. # if w is not None and w > 0:
    4. # imgW = w

    对以上报错的部分代码进行注释掉,再运行就正常运行了。

    2、将动态输入维度转固定输入维度

    在转onnx之前对原paddle模型进行处理,处理成固定输入, 原输入是动态维度,只要运行paddle_infer_shape.py,固定模型输入之后,再转onnx并且进行推理就不会出错了。

    具体自己操作一下吧,这两种办法我都试过,都可以。

    看到有人问我这个脚本paddle_infer_shape.py找不到,可能不知道这个脚本是个链接,不知道点,所以我还是把这个代码复制过来吧,直接复制下面的脚本,就是paddle_infer_shape.py代码。

    1. import argparse
    2. def process_old_ops_desc(program):
    3. for i in range(len(program.blocks[0].ops)):
    4. if program.blocks[0].ops[i].type == "matmul":
    5. if not program.blocks[0].ops[i].has_attr("head_number"):
    6. program.blocks[0].ops[i]._set_attr("head_number", 1)
    7. def infer_shape(program, input_shape_dict):
    8. import paddle
    9. paddle.enable_static()
    10. import paddle.fluid as fluid
    11. OP_WITHOUT_KERNEL_SET = {
    12. 'feed', 'fetch', 'recurrent', 'go', 'rnn_memory_helper_grad',
    13. 'conditional_block', 'while', 'send', 'recv', 'listen_and_serv',
    14. 'fl_listen_and_serv', 'ncclInit', 'select', 'checkpoint_notify',
    15. 'gen_bkcl_id', 'c_gen_bkcl_id', 'gen_nccl_id', 'c_gen_nccl_id',
    16. 'c_comm_init', 'c_sync_calc_stream', 'c_sync_comm_stream',
    17. 'queue_generator', 'dequeue', 'enqueue', 'heter_listen_and_serv',
    18. 'c_wait_comm', 'c_wait_compute', 'c_gen_hccl_id', 'c_comm_init_hccl',
    19. 'copy_cross_scope'
    20. }
    21. model_version = program.desc._version()
    22. paddle_version = paddle.__version__
    23. major_ver = model_version // 1000000
    24. minor_ver = (model_version - major_ver * 1000000) // 1000
    25. patch_ver = model_version - major_ver * 1000000 - minor_ver * 1000
    26. model_version = "{}.{}.{}".format(major_ver, minor_ver, patch_ver)
    27. if model_version != paddle_version:
    28. print("[WARNING] The model is saved by paddlepaddle v{}, but now your paddlepaddle is version of {}, this difference may cause error, it is recommend you reinstall a same version of paddlepaddle for this model".format(model_version, paddle_version))
    29. for k, v in input_shape_dict.items():
    30. program.blocks[0].var(k).desc.set_shape(v)
    31. for i in range(len(program.blocks)):
    32. for j in range(len(program.blocks[0].ops)):
    33. if program.blocks[i].ops[j].type in OP_WITHOUT_KERNEL_SET:
    34. continue
    35. program.blocks[i].ops[j].desc.infer_shape(program.blocks[i].desc)
    36. def parse_arguments():
    37. parser = argparse.ArgumentParser()
    38. parser.add_argument('--model_dir', required=True, help='Path of directory saved the input model.')
    39. parser.add_argument('--model_filename', required=True, help='The input model file name.')
    40. parser.add_argument('--params_filename', required=True, help='The parameters file name.')
    41. parser.add_argument('--save_dir', required=True,
    42. help='Path of directory to save the new exported model.')
    43. parser.add_argument('--input_shape_dict', required=True, help="The new shape information.")
    44. return parser.parse_args()
    45. if __name__ == '__main__':
    46. args = parse_arguments()
    47. import paddle
    48. paddle.enable_static()
    49. import paddle.fluid as fluid
    50. input_shape_dict_str = args.input_shape_dict
    51. input_shape_dict = eval(input_shape_dict_str)
    52. print("Start to load paddle model...")
    53. exe = fluid.Executor(fluid.CPUPlace())
    54. [prog, ipts, outs] = fluid.io.load_inference_model(args.model_dir, exe, model_filename=args.model_filename, params_filename=args.params_filename)
    55. process_old_ops_desc(prog)
    56. infer_shape(prog, input_shape_dict)
    57. fluid.io.save_inference_model(args.save_dir, ipts, outs, exe, prog, model_filename=args.model_filename, params_filename=args.params_filename)

    真是为了小白们煞费苦心啊,要面面俱到

  • 相关阅读:
    c# 使用UdpClient发送接收数据
    ArcGIS基础:二维点转三维点 二维线转三维线
    入行测试6年了,从月薪3000到30000,浅谈我的自动化测试进阶之路...
    Backbone 网络-ResNet v2 详解
    应对数字化挑战职场人真的要学python吗?
    [StartingPoint][Tier1]Crocodile
    三角网格库Triangle的使用
    竞赛 多目标跟踪算法 实时检测 - opencv 深度学习 机器视觉
    思考总结:领域知识图谱平台构建与业务应用
    Python面向对象三大特征
  • 原文地址:https://blog.csdn.net/wangmengmeng99/article/details/133376065