Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yolov5 onnx 转 infer 报错 Scale's shape must be 2 or 1, but got shape = 0 #1102

Open
2 tasks
maiquanshen opened this issue Dec 4, 2024 · 1 comment
Open
2 tasks

Comments

@maiquanshen
Copy link

感谢您参与 X2Paddle 社区! 问题模版为了 X2Paddle 能更好的迭代,例如新功能发布、 RoadMaps 和错误跟踪. 😸

问题描述

  • 错误信息
  • (paddle_env) iotmp@iotmp-PR4024ZY:/media/sdb3/PaddleOCR$ x2paddle --framework=onnx --model=/media/sdb3/rknn_model_zoo/models/vision/object_detection/yolov5-pytorch/yolov5/runs/capchat_20241202/exp4/weights/best.onnx --save_dir=./capchat/capchat_20241203_infer --enable_onnx_checker=true --input_shape_dict="{'image':[1, 3, 640, 640]}"
    /home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/setuptools/command/easy_install.py:41: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
    import pkg_resources
    /home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/pkg_resources/init.py:3154: DeprecationWarning: Deprecated call to pkg_resources.declare_namespace('mpl_toolkits').
    Implementing implicit namespace packages (as specified in PEP 420) is preferred to pkg_resources.declare_namespace. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
    declare_namespace(pkg)
    /home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/pkg_resources/init.py:3154: DeprecationWarning: Deprecated call to pkg_resources.declare_namespace('google').
    Implementing implicit namespace packages (as specified in PEP 420) is preferred to pkg_resources.declare_namespace. See https://setuptools.pypa.io/en/latest/references/keywords.html#keyword-namespace-packages
    declare_namespace(pkg)
    INFO:root:paddle.version = 2.4.0
    INFO:root:Now translating model from onnx to paddle.
    model ir_version: 4, op version: 9
    Shape inferencing ...
    Stopping at incomplete shape inference at Concat: x2paddle__model_12_Concat_output_0
    node inputs:
    name: "x2paddle__model_11_Upsample_output_0"
    type {
    tensor_type {
    elem_type: 1
    shape {
    dim {
    dim_param: "unk__0"
    }
    dim {
    dim_param: "unk__1"
    }
    dim {
    dim_param: "unk__2"
    }
    dim {
    dim_param: "unk__3"
    }
    }
    }
    }

name: "x2paddle__model_6_cv3_act_Relu_output_0"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 256
}
dim {
dim_value: 40
}
dim {
dim_value: 40
}
}
}
}

node outputs:
name: "x2paddle__model_12_Concat_output_0"
type {
tensor_type {
elem_type: 1
shape {
dim {
}
dim {
dim_param: "unk__1 + 256"
}
dim {
}
dim {
}
}
}
}

!!!!!!!!!!
Shape inferenced.
/home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/x2paddle/decoder/onnx_decoder.py:374: DeprecationWarning: mapping.TENSOR_TYPE_TO_NP_TYPE is now deprecated and will be removed in a future release.To silence this warning, please use helper.tensor_dtype_to_np_dtype instead.
TENSOR_TYPE_TO_NP_TYPE[item.type.tensor_type.elem_type],
Now, onnx2paddle support convert onnx model opset_verison [7, 8, 9, 10, 11, 12, 13, 14, 15], opset_verison of your onnx model is 9.
Total nodes: 150
Nodes converting ...
Converting node 1 ... /home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/x2paddle/decoder/onnx_decoder.py:172: DeprecationWarning: mapping.TENSOR_TYPE_TO_NP_TYPE is now deprecated and will be removed in a future release.To silence this warning, please use helper.tensor_dtype_to_np_dtype instead.
return TENSOR_TYPE_TO_NP_TYPE[dtype]
Converting node 2 ... /home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/x2paddle/decoder/onnx_decoder.py:175: DeprecationWarning: mapping.TENSOR_TYPE_TO_NP_TYPE is now deprecated and will be removed in a future release.To silence this warning, please use helper.tensor_dtype_to_np_dtype instead.
return TENSOR_TYPE_TO_NP_TYPE[dtype]
Converting node 276 ...
Nodes converted.
INFO:root:Model optimizing ...
INFO:root:Model optimized.
Exporting inference model from python code ('/media/sdb3/PaddleOCR/capchat/capchat_20241203_infer/x2paddle_code.py')...

In transformed code:

File "/media/sdb3/PaddleOCR/./capchat/capchat_20241203_infer/x2paddle_code.py", line 247, in forward
    x2paddle__model_14_act_Relu_output_0 = self.relu39(x2paddle__model_14_conv_Conv_output_0)
    x2paddle__model_11_Constant_output_0 = paddle.slice(input=x2paddle__model_11_Constant_output_0, axes=[0], starts=[2], ends=[4])
    x2paddle__model_15_Upsample_output_0 = paddle.nn.functional.interpolate(x=x2paddle__model_14_act_Relu_output_0, scale_factor=x2paddle__model_11_Constant_output_0, mode='nearest', align_mode=1)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
    x2paddle__model_16_Concat_output_0 = paddle.concat(x=[x2paddle__model_15_Upsample_output_0, x2paddle__model_4_cv3_act_Relu_output_0], axis=1)
    x2paddle__model_17_cv1_conv_Conv_output_0 = self.conv44(x2paddle__model_16_Concat_output_0)

File "/home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/paddle/nn/functional/common.py", line 698, in interpolate
    helper.append_op(
File "/home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/paddle/fluid/layer_helper.py", line 45, in append_op
    return self.main_program.current_block().append_op(*args, **kwargs)
File "/home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/paddle/fluid/framework.py", line 4017, in append_op
    op = Operator(
File "/home/iotmp/anaconda3/envs/paddle_env/lib/python3.8/site-packages/paddle/fluid/framework.py", line 3016, in __init__
    self.desc.infer_shape(self.block.desc)

ValueError: (InvalidArgument) Scale's shape must be 2 or 1, but got shape = 0 .

[Hint: Expected scale_tensor_dim[0] == 2 || scale_tensor_dim[0] == 1 == true, but received scale_tensor_dim[0] == 2 || scale_tensor_dim[0] == 1:0 != true:1.] (at /paddle/paddle/phi/infermeta/multiary.cc:1514)
[operator < nearest_interp_v2 > error]

  • 错误截图
    image

具体信息

onnx模型已经是去掉sacle_factor以及后处理部分,在转的是还会因为sacle_factor这个分支没有而报错,为什么呢,如何去掉呢

  • 转换模型后用处
    • [ *] 使用 Paddle 框架/ PaddleInference 推理预测
    • 使用 Paddle-Lite 做移动端推理
    • 转换预训练参数,再使用 Paddle 进行模型开发
  • 模型来源
    YOLOX:yolov5
  • 应用场景
    边缘端的检测业务
  • 版本信息
    PaddlePaddle => ❔ 2.4.2
    X2Paddle => ❔ 1.5.0
    来源框架版本(PyTorch/TF/ONNX/Caffe) => ❔
  • 您的联系方式(邮箱/微信/电话)
    [email protected]
@maiquanshen
Copy link
Author

用了一个笨的方法,自己解决了,修改生成后的x2paddle_code.py代码,将sacle_factor的输入去掉,然后手动给一个默认值。最后修改x2paddle的源码,让它用修改后的x2paddle_code去保存权重文件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant