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

PyTorch转换为Paddle; Paddle不支持 convert failed node:_features_conv0_Conv_output_0, op_type is Conv #1010

Open
zuishenke123 opened this issue Apr 20, 2024 · 0 comments

Comments

@zuishenke123
Copy link

zuishenke123 commented Apr 20, 2024

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

问题描述

将PyTorch官方库中的DenseNet模型转换为Paddle版本,并用于测试基于ONNX的模型转换性能。PyTorch模型转换ONNX成功,但是ONNX转换为PaddlePaddle失败

  • 错误信息
    -Converting node 434 ... Traceback (most recent call last):
    File "D:\anaconda3\envs\LID-CMC\lib\site-packages\x2paddle\op_mapper\onnx2paddle\opset_legacy.py", line 110, in run_mapping
    res = func(*args, **kwargs)
    File "D:\anaconda3\envs\LID-CMC\lib\site-packages\x2paddle\op_mapper\onnx2paddle\opset_legacy.py", line 2304, in Conv
    _rename_or_remove_weight(
    File "D:\anaconda3\envs\LID-CMC\lib\site-packages\x2paddle\op_mapper\onnx2paddle\opset_legacy.py", line 68, in _rename_or_remove_weight
    raise KeyError('{} not a key in {}'.format(origin_name, weights.keys()))
    KeyError: "x2paddle_onnx__Conv_1167 not a key in dict_keys(['x2paddle_features_denseblock1_denselayer1_norm1_weight', 'x2paddle_features_denseblock1_denselayer1_norm1_bias', 'x2paddle_features_denseblock1_denselayer1_conv2_weight', 'x2paddle_features_denseblock1_denselayer2_norm1_weight', 'x2paddle_features_denseblock1_denselayer2_norm1_bias', 'x2paddle_features_denseblock1_denselayer2_conv2_weight', 'x2paddle_features_denseblock1_denselayer3_norm1_weight', 'x2paddle_features_denseblock1_denselayer3_norm1_bias', 'x2paddle_features_denseblock1_denselayer3_conv2_weight', 'x2paddle_features_denseblock1_denselayer4_norm1_weight', 'x2paddle_features_denseblock1_denselayer4_norm1_bias', 'x2paddle_features_denseblock1_denselayer4_conv2_weight', 'x2paddle_features_denseblock1_denselayer5_norm1_weight', 'x2paddle_features_denseblock1_denselayer5_norm1_bias', 'x2paddle_features_denseblock1_denselayer5_conv2_weight', 'x2paddle_features_denseblock1_denselayer6_norm1_weight', 'x2paddle_features_denseblock1_denselayer6_norm1_bias', 'x2paddle_features_denseblock1_denselayer6_conv2_weight', 'x2paddle_features_transition1_norm_weight', 'x2paddle_features_transition1_norm_bias', 'x2paddle_features_transition1_conv_weight', 'x2paddle_features_denseblock2_denselayer1_conv2_weight', 'x2paddle_features_denseblock2_denselayer2_conv2_weight', 'x2paddle_features_denseblock2_denselayer3_conv2_weight', 'x2paddle_features_denseblock2_denselayer4_conv2_weight', 'x2paddle_features_denseblock2_denselayer5_conv2_weight', 'x2paddle_features_denseblock2_denselayer6_norm1_weight', 'x2paddle_features_denseblock2_denselayer6_norm1_bias', 'x2paddle_features_denseblock2_denselayer6_conv2_weight', 'x2paddle_features_denseblock2_denselayer7_norm1_weight', 'x2paddle_features_denseblock2_denselayer7_norm1_bias', 'x2paddle_features_denseblock2_denselayer7_conv2_weight', 'x2paddle_features_denseblock2_denselayer8_norm1_weight', 'x2paddle_features_denseblock2_denselayer8_norm1_bias', 'x2paddle_features_denseblock2_denselayer8_conv2_weight', 'x2paddle_features_denseblock2_denselayer9_norm1_weight', 'x2paddle_features_denseblock2_denselayer9_norm1_bias', 'x2paddle_features_denseblock2_denselayer9_conv2_weight', 'x2paddle_features_denseblock2_denselayer10_norm1_weight', 'x2paddle_features_denseblock2_denselayer10_norm1_bias', 'x2paddle_features_denseblock2_denselayer10_conv2_weight', 'x2paddle_features_denseblock2_denselayer11_norm1_weight', 'x2paddle_features_denseblock2_denselayer11_norm1_bias', 'x2paddle_features_denseblock2_denselayer11_conv2_weight', 'x2paddle_features_denseblock2_denselayer12_norm1_weight', 'x2paddle_features_denseblock2_denselayer12_norm1_bias', 'x2paddle_features_denseblock2_denselayer12_conv2_weight', 'x2paddle_features_transition2_norm_weight', 'x2paddle_features_transition2_norm_bias', 'x2paddle_features_transition2_conv_weight', 'x2paddle_features_denseblock3_denselayer1_conv2_weight', 'x2paddle_features_denseblock3_denselayer2_conv2_weight', 'x2paddle_features_denseblock3_denselayer3_conv2_weight', 'x2paddle_features_denseblock3_denselayer4_conv2_weight', 'x2paddle_features_denseblock3_denselayer5_conv2_weight', 'x2paddle_features_denseblock3_denselayer6_conv2_weight', 'x2paddle_features_denseblock3_denselayer7_conv2_weight', 'x2paddle_features_denseblock3_denselayer8_conv2_weight', 'x2paddle_features_denseblock3_denselayer9_conv2_weight', 'x2paddle_features_denseblock3_denselayer10_norm1_weight', 'x2paddle_features_denseblock3_denselayer10_norm1_bias', 'x2paddle_features_denseblock3_denselayer10_conv2_weight', 'x2paddle_features_denseblock3_denselayer11_norm1_weight', 'x2paddle_features_denseblock3_denselayer11_norm1_bias', 'x2paddle_features_denseblock3_denselayer11_conv2_weight', 'x2paddle_features_denseblock3_denselayer12_norm1_weight', 'x2paddle_features_denseblock3_denselayer12_norm1_bias', 'x2paddle_features_denseblock3_denselayer12_conv2_weight', 'x2paddle_features_denseblock3_denselayer13_norm1_weight', 'x2paddle_features_denseblock3_denselayer13_norm1_bias', 'x2paddle_features_denseblock3_denselayer13_conv2_weight', 'x2paddle_features_denseblock3_denselayer14_norm1_weight', 'x2paddle_features_denseblock3_denselayer14_norm1_bias', 'x2paddle_features_denseblock3_denselayer14_conv2_weight', 'x2paddle_features_denseblock3_denselayer15_norm1_weight', 'x2paddle_features_denseblock3_denselayer15_norm1_bias', 'x2paddle_features_denseblock3_denselayer15_conv2_weight', 'x2paddle_features_denseblock3_denselayer16_norm1_weight', 'x2paddle_features_denseblock3_denselayer16_norm1_bias', 'x2paddle_features_denseblock3_denselayer16_conv2_weight', 'x2paddle_features_denseblock3_denselayer17_norm1_weight', 'x2paddle_features_denseblock3_denselayer17_norm1_bias', 'x2paddle_features_denseblock3_denselayer17_conv2_weight', 'x2paddle_features_denseblock3_denselayer18_norm1_weight', 'x2paddle_features_denseblock3_denselayer18_norm1_bias', 'x2paddle_features_denseblock3_denselayer18_conv2_weight', 'x2paddle_features_denseblock3_denselayer19_norm1_weight', 'x2paddle_features_denseblock3_denselayer19_norm1_bias', 'x2paddle_features_denseblock3_denselayer19_conv2_weight', 'x2paddle_features_denseblock3_denselayer20_norm1_weight', 'x2paddle_features_denseblock3_denselayer20_norm1_bias', 'x2paddle_features_denseblock3_denselayer20_conv2_weight', 'x2paddle_features_denseblock3_denselayer21_norm1_weight', 'x2paddle_features_denseblock3_denselayer21_norm1_bias', 'x2paddle_features_denseblock3_denselayer21_conv2_weight', 'x2paddle_features_denseblock3_denselayer22_norm1_weight', 'x2paddle_features_denseblock3_denselayer22_norm1_bias', 'x2paddle_features_denseblock3_denselayer22_conv2_weight', 'x2paddle_features_denseblock3_denselayer23_norm1_weight', 'x2paddle_features_denseblock3_denselayer23_norm1_bias', 'x2paddle_features_denseblock3_denselayer23_conv2_weight', 'x2paddle_features_denseblock3_denselayer24_norm1_weight', 'x2paddle_features_denseblock3_denselayer24_norm1_bias', 'x2paddle_features_denseblock3_denselayer24_conv2_weight', 'x2paddle_features_transition3_norm_weight', 'x2paddle_features_transition3_norm_bias', 'x2paddle_features_transition3_conv_weight', 'x2paddle_features_denseblock4_denselayer1_conv2_weight', 'x2paddle_features_denseblock4_denselayer2_conv2_weight', 'x2paddle_features_denseblock4_denselayer3_conv2_weight', 'x2paddle_features_denseblock4_denselayer4_conv2_weight', 'x2paddle_features_denseblock4_denselayer5_conv2_weight', 'x2paddle_features_denseblock4_denselayer6_conv2_weight', 'x2paddle_features_denseblock4_denselayer7_conv2_weight', 'x2paddle_features_denseblock4_denselayer8_conv2_weight', 'x2paddle_features_denseblock4_denselayer9_conv2_weight', 'x2paddle_features_denseblock4_denselayer10_conv2_weight', 'x2paddle_features_denseblock4_denselayer11_conv2_weight', 'x2paddle_features_denseblock4_denselayer12_conv2_weight', 'x2paddle_features_denseblock4_denselayer13_conv2_weight', 'x2paddle_features_denseblock4_denselayer14_conv2_weight', 'x2paddle_features_denseblock4_denselayer15_conv2_weight', 'x2paddle_features_denseblock4_denselayer16_conv2_weight', 'x2paddle_classifier_weight', 'x2paddle_classifier_bias', 'x2paddle_onnx__Conv_1166', 'x2paddle_onnx__Conv_1169', 'x2paddle_onnx__Conv_1172', 'x2paddle_onnx__Conv_1175', 'x2paddle_onnx__Conv_1178', 'x2paddle_onnx__Conv_1181', 'x2paddle_onnx__Conv_1184', 'x2paddle_onnx__Conv_1187', 'x2paddle_onnx__Conv_1190', 'x2paddle_onnx__Conv_1193', 'x2paddle_onnx__Conv_1196', 'x2paddle_onnx__Conv_1199', 'x2paddle_onnx__Conv_1202', 'x2paddle_onnx__Conv_1205', 'x2paddle_onnx__Conv_1208', 'x2paddle_onnx__Conv_1211', 'x2paddle_onnx__Conv_1214', 'x2paddle_onnx__Conv_1217', 'x2paddle_onnx__Conv_1220', 'x2paddle_onnx__Conv_1223', 'x2paddle_onnx__Conv_1226', 'x2paddle_onnx__Conv_1229', 'x2paddle_onnx__Conv_1232', 'x2paddle_onnx__Conv_1235', 'x2paddle_onnx__Conv_1238', 'x2paddle_onnx__Conv_1241', 'x2paddle_onnx__Conv_1244', 'x2paddle_onnx__Conv_1247', 'x2paddle_onnx__Conv_1250', 'x2paddle_onnx__Conv_1253', 'x2paddle_onnx__Conv_1256', 'x2paddle_onnx__Conv_1259', 'x2paddle_onnx__Conv_1262', 'x2paddle_onnx__Conv_1265', 'x2paddle_onnx__Conv_1268', 'x2paddle_onnx__Conv_1271', 'x2paddle_onnx__Conv_1274', 'x2paddle_onnx__Conv_1277', 'x2paddle_onnx__Conv_1280', 'x2paddle_onnx__Conv_1283', 'x2paddle_onnx__Conv_1286', 'x2paddle_onnx__Conv_1289', 'x2paddle_onnx__Conv_1292', 'x2paddle_onnx__Conv_1295', 'x2paddle_onnx__Conv_1298', 'x2paddle_onnx__Conv_1301', 'x2paddle_onnx__Conv_1304', 'x2paddle_onnx__Conv_1307', 'x2paddle_onnx__Conv_1310', 'x2paddle_onnx__Conv_1313', 'x2paddle_onnx__Conv_1316', 'x2paddle_onnx__Conv_1319', 'x2paddle_onnx__Conv_1322', 'x2paddle_onnx__Conv_1325', 'x2paddle_onnx__Conv_1328', 'x2paddle_onnx__Conv_1331', 'x2paddle_onnx__Conv_1334', 'x2paddle_onnx__Conv_1337', 'x2paddle_onnx__Conv_1340', 'x2paddle__features_transition1_pool_Constant_output_0', 'x2paddle__features_transition2_pool_Constant_output_0', 'x2paddle__features_transition3_pool_Constant_output_0', 'conv0.weight'])"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "D:\anaconda3\envs\LID-CMC\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "D:\anaconda3\envs\LID-CMC\lib\runpy.py", line 87, in run_code
exec(code, run_globals)
File "D:\anaconda3\envs\LID-CMC\Scripts\x2paddle.exe_main
.py", line 7, in
sys.exit(main())
File "D:\anaconda3\envs\LID-CMC\lib\site-packages\x2paddle\convert.py", line 489, in main
onnx2paddle(
File "D:\anaconda3\envs\LID-CMC\lib\site-packages\x2paddle\convert.py", line 304, in onnx2paddle
mapper = ONNXOpMapper(model)
File "D:\anaconda3\envs\LID-CMC\lib\site-packages\x2paddle\op_mapper\onnx2paddle\onnx_op_mapper.py", line 52, in init
func(node)
File "D:\anaconda3\envs\LID-CMC\lib\site-packages\x2paddle\op_mapper\onnx2paddle\opset_legacy.py", line 112, in run_mapping
raise Exception("convert failed node:{}, op_type is {}".format(
Exception: convert failed node:_features_conv0_Conv_output_0, op_type is Conv

  • 错误截图
    db1dbe0ff4aeab9fa9b91af2fb44cfb

具体信息

详细代码

import torch,os
from torchvision.models.densenet import DenseNet121_Weights, DenseNet

# Initialize Result Path
input_tensor = torch.randn(64, 3, 224, 224)
output_folder = os.path.join(os.getcwd())
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

weights = DenseNet121_Weights.verify(None)
model = DenseNet(32, (6, 12, 24, 16), num_classes=100)
model_name = "densenet121"
if weights is not None:
     model.load_state_dict(weights.get_state_dict(progress=True))
model.eval()

# 指定存储文件夹的名称
if not os.path.exists(os.path.join(output_folder, f"{model_name}_IR_and_pd")):
    os.makedirs(os.path.join(output_folder, f"{model_name}_IR_and_pd"))

try:
    import torch
except Exception as e:
    raise ValueError(f"import torch failed:{e}")

onnx_file = os.path.join(output_folder, f"{model_name}_IR_and_pd", f"testTorch{model_name}.onnx")
try:
   torch.onnx.export(model, input_tensor, onnx_file, verbose=True)
except Exception as e:
   raise ValueError(f"Failed to export ONNX model: {e}")

pd_model_dir = os.path.join(output_folder, f"{model_name}_IR_and_pd")

# 使用 x2paddle 转换 ONNX 模型到 PaddlePaddle 格式
try:
   conversion_command = f"x2paddle --framework=onnx --model={onnx_file} --save_dir={pd_model_dir}"
   conversion_result = os.system(conversion_command)
   print(f"x2paddle conversion command returned: {conversion_result}")
except Exception as e:
   raise ValueError(f"Failed to convert ONNX model with x2paddle: {e}")

print(f"Model conversion completed. ONNX and PaddlePaddle models are saved in '{output_folder}'.")
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