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

MobileNetV2.onnx转换过程报错 #990

Open
masteryi-0018 opened this issue Aug 24, 2023 · 1 comment
Open

MobileNetV2.onnx转换过程报错 #990

masteryi-0018 opened this issue Aug 24, 2023 · 1 comment

Comments

@masteryi-0018
Copy link

问题描述

网络为来自tensorflow官方的预训练的MobileNetV2,首先将tensorflow模型转换为onnx,使用x2paddle将onnx转换为paddle时,发生以下错误:

  • 错误信息
Traceback (most recent call last):
  File "/home/xxx/anaconda3/envs/x2paddle/bin/x2paddle", line 8, in <module>
    sys.exit(main())
  File "/home/xxx/anaconda3/envs/x2paddle/lib/python3.8/site-packages/x2paddle/convert.py", line 489, in main
    onnx2paddle(
  File "/home/xxx/anaconda3/envs/x2paddle/lib/python3.8/site-packages/x2paddle/convert.py", line 311, in onnx2paddle
    mapper.paddle_graph.gen_model(save_dir)
  File "/home/xxx/anaconda3/envs/x2paddle/lib/python3.8/site-packages/x2paddle/core/program.py", line 284, in gen_model
    self.gen_code(save_dir)
  File "/home/xxx/anaconda3/envs/x2paddle/lib/python3.8/site-packages/x2paddle/core/program.py", line 451, in gen_code
    remove_default_attrs(layer.kernel, layer.attrs)
  File "/home/xxx/anaconda3/envs/x2paddle/lib/python3.8/site-packages/x2paddle/core/util.py", line 74, in remove_default_attrs
    if default_v == attrs[default_k]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

具体信息

  • 模型来源
    MobileNetV2:
import tensorflow as tf
model = tf.keras.applications.MobileNetV2(weights='imagenet')

然后将tensorflow转换为onnx:

import tf2onnx
output_path = model.name + ".onnx"
model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, opset=13, output_path=output_path)
  • 版本信息
    PaddlePaddle => :2.5.1:
    X2Paddle => :1.4.1:
    来源框架版本(PyTorch/TF/ONNX/Caffe) => :tf2onnx: 1.15.0,onnx: 1.13.1,tensorflow: 2.13.0

我的尝试

经过简单调试发现在get_default_args函数中进行判断时,打印axis的信息,可以看到attrs[default_k]是一个列表,即[2 3],但是使用数据类型判断是否为list时,结果为False,造成了条件判断错误,使用Netron观察网络,也可以看到在Squeeze算子的地方,出现了[2, 3]的参数

@firedent
Copy link
Contributor

firedent commented Dec 28, 2023

解决方案:从TF转ONNX的时候,将opset设置为<13。

import tf2onnx
output_path = model.name + ".onnx"
model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, opset=12, output_path=output_path)

原因:Squeeze-13的axis参数类型为tensor(int64),Squeeze-11的axis参数类型为list of ints。

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

2 participants