报错
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
- # if self.use_onnx:
- # img_h, img_w = self.input_tensor.shape[2:]
- # if img_h is not None and img_w is not None and img_h > 0 and img_w > 0:
- # pre_process_list[0] = {
- # 'DetResizeForTest': {
- # 'image_shape': [img_h, img_w]
- # }
- # }
predict_rec.py line174
- # if self.use_onnx:
- # w = self.input_tensor.shape[3:][0]
- # if w is not None and w > 0:
- # imgW = w
对以上报错的部分代码进行注释掉,再运行就正常运行了。
2、将动态输入维度转固定输入维度
在转onnx之前对原paddle模型进行处理,处理成固定输入, 原输入是动态维度,只要运行paddle_infer_shape.py,固定模型输入之后,再转onnx并且进行推理就不会出错了。
具体自己操作一下吧,这两种办法我都试过,都可以。
看到有人问我这个脚本paddle_infer_shape.py找不到,可能不知道这个脚本是个链接,不知道点,所以我还是把这个代码复制过来吧,直接复制下面的脚本,就是paddle_infer_shape.py代码。
- import argparse
-
- def process_old_ops_desc(program):
- for i in range(len(program.blocks[0].ops)):
- if program.blocks[0].ops[i].type == "matmul":
- if not program.blocks[0].ops[i].has_attr("head_number"):
- program.blocks[0].ops[i]._set_attr("head_number", 1)
-
- def infer_shape(program, input_shape_dict):
- import paddle
- paddle.enable_static()
- import paddle.fluid as fluid
-
- OP_WITHOUT_KERNEL_SET = {
- 'feed', 'fetch', 'recurrent', 'go', 'rnn_memory_helper_grad',
- 'conditional_block', 'while', 'send', 'recv', 'listen_and_serv',
- 'fl_listen_and_serv', 'ncclInit', 'select', 'checkpoint_notify',
- 'gen_bkcl_id', 'c_gen_bkcl_id', 'gen_nccl_id', 'c_gen_nccl_id',
- 'c_comm_init', 'c_sync_calc_stream', 'c_sync_comm_stream',
- 'queue_generator', 'dequeue', 'enqueue', 'heter_listen_and_serv',
- 'c_wait_comm', 'c_wait_compute', 'c_gen_hccl_id', 'c_comm_init_hccl',
- 'copy_cross_scope'
- }
- model_version = program.desc._version()
- paddle_version = paddle.__version__
- major_ver = model_version // 1000000
- minor_ver = (model_version - major_ver * 1000000) // 1000
- patch_ver = model_version - major_ver * 1000000 - minor_ver * 1000
- model_version = "{}.{}.{}".format(major_ver, minor_ver, patch_ver)
- if model_version != paddle_version:
- 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))
- for k, v in input_shape_dict.items():
- program.blocks[0].var(k).desc.set_shape(v)
- for i in range(len(program.blocks)):
- for j in range(len(program.blocks[0].ops)):
- if program.blocks[i].ops[j].type in OP_WITHOUT_KERNEL_SET:
- continue
- program.blocks[i].ops[j].desc.infer_shape(program.blocks[i].desc)
-
- def parse_arguments():
- parser = argparse.ArgumentParser()
- parser.add_argument('--model_dir', required=True, help='Path of directory saved the input model.')
- parser.add_argument('--model_filename', required=True, help='The input model file name.')
- parser.add_argument('--params_filename', required=True, help='The parameters file name.')
- parser.add_argument('--save_dir', required=True,
- help='Path of directory to save the new exported model.')
- parser.add_argument('--input_shape_dict', required=True, help="The new shape information.")
- return parser.parse_args()
-
- if __name__ == '__main__':
- args = parse_arguments()
- import paddle
- paddle.enable_static()
- import paddle.fluid as fluid
- input_shape_dict_str = args.input_shape_dict
- input_shape_dict = eval(input_shape_dict_str)
- print("Start to load paddle model...")
- exe = fluid.Executor(fluid.CPUPlace())
- [prog, ipts, outs] = fluid.io.load_inference_model(args.model_dir, exe, model_filename=args.model_filename, params_filename=args.params_filename)
- process_old_ops_desc(prog)
- infer_shape(prog, input_shape_dict)
- fluid.io.save_inference_model(args.save_dir, ipts, outs, exe, prog, model_filename=args.model_filename, params_filename=args.params_filename)
真是为了小白们煞费苦心啊,要面面俱到