PaddlePaddle / Paddle2ONNX

ONNX Model Exporter for PaddlePaddle
Apache License 2.0
713 stars 166 forks source link

我想把一个resnet_vd转为onnx,想请问应该用静态图还是动态图,参数该如何填 #262

Closed cww97 closed 3 months ago

cww97 commented 3 years ago

这个模型 https://github.com/PaddlePaddle/PaddleClas/blob/release/2.1/docs/zh_CN/application/transfer_learning.md 页面里面有个.pdparams文件

根据这个文档 https://github.com/PaddlePaddle/Paddle2ONNX/blob/release/0.6/README_zh.md

两个都试了,都有问题

$ paddle2onnx \
    --model_dir /root/paddle \
    --model_filename /root/paddle/PaddleClas/ppcls/modeling/architectures/resnet_vd.py \
    --params_filename /root/paddle/ResNet50_vd_10w_pretrained.pdparams \
    --save_file onnx_file.onnx

Traceback (most recent call last):
  File "/root/anaconda3/bin/paddle2onnx", line 8, in <module>
    sys.exit(main())
  File "/root/anaconda3/lib/python3.8/site-packages/paddle2onnx/command.py", line 136, in main
    program2onnx(
  File "/root/anaconda3/lib/python3.8/site-packages/paddle2onnx/command.py", line 102, in program2onnx
    [program, feed_var_names, fetch_vars] = fluid.io.load_inference_model(
  File "<decorator-gen-88>", line 2, in load_inference_model
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/wrapped_decorator.py", line 25, in __impl__
    return wrapped_func(*args, **kwargs)
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/framework.py", line 234, in __impl__
    return func(*args, **kwargs)
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/io.py", line 1538, in load_inference_model
    program = Program.parse_from_string(program_desc_str)
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/framework.py", line 5080, in parse_from_string
    p.desc = core.ProgramDesc(binary_str)
ValueError: (InvalidArgument) Failed to parse program_desc from binary string.
  [Hint: Expected desc_.ParseFromString(binary_str) == true, but received desc_.ParseFromString(binary_str):0 != true:1.] (at /paddle/paddle/fluid/framework/program_desc.cc:103)

另一个

import os
import time
import paddle

# 从模型代码中导入模型
from PaddleClas.ppcls.modeling.architectures.resnet_vd import ResNet50_vd

# 实例化模型
model = ResNet50_vd()
# import ipdb; ipdb.set_trace()
# 将模型设置为推理状态
model.eval()

# 定义输入数据
# input_spec = paddle.static.InputSpec(shape=[None, 3, 320, 320], dtype='float32', name='image')

# ONNX模型导出
# enable_onnx_checker设置为True,表示使用官方ONNX工具包来check模型的正确性,需要安装ONNX(pip install onnx)

# paddle.onnx.export(model, 'resnet50', input_spec=[input_spec], opset_version=12, enable_onnx_checker=True)
paddle.onnx.export(model, 'ResNet50_vd.onnx')

得到输出

Inference models that PaddleClas provides are listed as follows:
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|       Series      |                                                                           Name                                                                           |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|      AlexNet      |                                                                         AlexNet                                                                          |
|      DarkNet      |                                                                        DarkNet53                                                                         |
|        DeiT       |     DeiT_base_distilled_patch16_224  DeiT_base_distilled_patch16_384  DeiT_base_patch16_224  DeiT_base_patch16_384  DeiT_small_distilled_patch16_224     |
|                   |                                      DeiT_small_patch16_224  DeiT_tiny_distilled_patch16_224  DeiT_tiny_patch16_224                                      |
|      DenseNet     |                                             DenseNet121  DenseNet161  DenseNet169  DenseNet201  DenseNet264                                              |
|        DPN        |                                                           DPN68  DPN92  DPN98  DPN107  DPN131                                                            |
|    EfficientNet   |   EfficientNetB0  EfficientNetB0_small  EfficientNetB1  EfficientNetB2  EfficientNetB3  EfficientNetB4  EfficientNetB5  EfficientNetB6  EfficientNetB7   |
|      GhostNet     |                                             GhostNet_x0_5  GhostNet_x1_0  GhostNet_x1_3  GhostNet_x1_3_ssld                                              |
|       HRNet       |              HRNet_W18_C  HRNet_W30_C  HRNet_W32_C  HRNet_W40_C  HRNet_W44_C  HRNet_W48_C  HRNet_W64_C  HRNet_W18_C_ssld  HRNet_W48_C_ssld               |
|     Inception     |                                                           GoogLeNet  InceptionV3  InceptionV4                                                            |
|    MobileNetV1    |                                  MobileNetV1_x0_25  MobileNetV1_x0_5  MobileNetV1_x0_75  MobileNetV1  MobileNetV1_ssld                                   |
|    MobileNetV2    |                MobileNetV2_x0_25  MobileNetV2_x0_5  MobileNetV2_x0_75  MobileNetV2  MobileNetV2_x1_5  MobileNetV2_x2_0  MobileNetV2_ssld                 |
|    MobileNetV3    |    MobileNetV3_small_x0_35  MobileNetV3_small_x0_5  MobileNetV3_small_x0_75  MobileNetV3_small_x1_0  MobileNetV3_small_x1_25  MobileNetV3_large_x0_35    |
|                   |              MobileNetV3_large_x0_5  MobileNetV3_large_x0_75  MobileNetV3_large_x1_0  MobileNetV3_large_x1_25  MobileNetV3_small_x1_0_ssld               |
|                   |                                                               MobileNetV3_large_x1_0_ssld                                                                |
|       RegNet      |                                                                       RegNetX_4GF                                                                        |
|      Res2Net      | Res2Net50_14w_8s  Res2Net50_26w_4s  Res2Net50_vd_26w_4s  Res2Net200_vd_26w_4s  Res2Net101_vd_26w_4s  Res2Net50_vd_26w_4s_ssld  Res2Net101_vd_26w_4s_ssld |
|                   |                                                                Res2Net200_vd_26w_4s_ssld                                                                 |
|      ResNeSt      |                                                            ResNeSt50  ResNeSt50_fast_1s1x64d                                                             |
|       ResNet      |    ResNet18  ResNet18_vd  ResNet34  ResNet34_vd  ResNet50  ResNet50_vc  ResNet50_vd  ResNet50_vd_v2  ResNet101  ResNet101_vd  ResNet152  ResNet152_vd    |
|                   |         ResNet200_vd  ResNet34_vd_ssld  ResNet50_vd_ssld  ResNet50_vd_ssld_v2  ResNet101_vd_ssld  Fix_ResNet50_vd_ssld_v2  ResNet50_ACNet_deploy         |
|      ResNeXt      |          ResNeXt50_32x4d  ResNeXt50_vd_32x4d  ResNeXt50_64x4d  ResNeXt50_vd_64x4d  ResNeXt101_32x4d  ResNeXt101_vd_32x4d  ResNeXt101_32x8d_wsl           |
|                   | ResNeXt101_32x16d_wsl  ResNeXt101_32x32d_wsl  ResNeXt101_32x48d_wsl  Fix_ResNeXt101_32x48d_wsl  ResNeXt101_64x4d  ResNeXt101_vd_64x4d  ResNeXt152_32x4d  |
|                   |                                                ResNeXt152_vd_32x4d  ResNeXt152_64x4d  ResNeXt152_vd_64x4d                                                |
|       SENet       |     SENet154_vd  SE_HRNet_W64_C_ssld  SE_ResNet18_vd  SE_ResNet34_vd  SE_ResNet50_vd  SE_ResNeXt50_32x4d  SE_ResNeXt50_vd_32x4d  SE_ResNeXt101_32x4d     |
|    ShuffleNetV2   |          ShuffleNetV2_swish  ShuffleNetV2_x0_25  ShuffleNetV2_x0_33  ShuffleNetV2_x0_5  ShuffleNetV2_x1_0  ShuffleNetV2_x1_5  ShuffleNetV2_x2_0          |
|     SqueezeNet    |                                                               SqueezeNet1_0  SqueezeNet1_1                                                               |
|  SwinTransformer  |   SwinTransformer_large_patch4_window7_224_22kto1k  SwinTransformer_large_patch4_window12_384_22kto1k  SwinTransformer_base_patch4_window7_224_22kto1k   |
|                   |           SwinTransformer_base_patch4_window12_384_22kto1k  SwinTransformer_base_patch4_window12_384  SwinTransformer_base_patch4_window7_224            |
|                   |                                    SwinTransformer_small_patch4_window7_224  SwinTransformer_tiny_patch4_window7_224                                     |
|        VGG        |                                                                VGG11  VGG13  VGG16  VGG19                                                                |
| VisionTransformer |          ViT_base_patch16_224  ViT_base_patch16_384  ViT_base_patch32_384  ViT_large_patch16_224  ViT_large_patch16_384  ViT_large_patch32_384           |
|                   |                                                                  ViT_small_patch16_224                                                                   |
|      Xception     |                                        Xception41  Xception41_deeplab  Xception65  Xception65_deeplab  Xception71                                        |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
W0604 17:43:06.252307  2924 device_context.cc:404] Please NOTE: device: 0, GPU Compute Capability: 7.5, Driver API Version: 11.0, Runtime API Version: 10.2
W0604 17:43:06.255980  2924 device_context.cc:422] device: 0, cuDNN Version: 7.6.
Traceback (most recent call last):
  File "paddle2onnx.py", line 21, in <module>
    paddle.onnx.export(model, 'ResNet50_vd.onnx')
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/onnx/export.py", line 90, in export
    p2o = try_import('paddle2onnx')
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/utils/lazy_import.py", line 32, in try_import
    mod = importlib.import_module(module_name)
  File "/root/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./paddle2onnx.py", line 9, in <module>
    model = ResNet50_vd()
  File "/root/paddle/PaddleClas/ppcls/modeling/architectures/resnet_vd.py", line 338, in ResNet50_vd
    model = ResNet_vd(layers=50, **args)
  File "/root/paddle/PaddleClas/ppcls/modeling/architectures/resnet_vd.py", line 231, in __init__
    self.conv1_1 = ConvBNLayer(
  File "/root/paddle/PaddleClas/ppcls/modeling/architectures/resnet_vd.py", line 50, in __init__
    self._conv = Conv2D(
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/nn/layer/conv.py", line 633, in __init__
    super(Conv2D, self).__init__(
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/nn/layer/conv.py", line 132, in __init__
    self.weight = self.create_parameter(
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/dygraph/layers.py", line 411, in create_parameter
    return self._helper.create_parameter(temp_attr, shape, dtype, is_bias,
  File "/root/anaconda3/lib/python3.8/site-packages/paddle/fluid/layer_helper_base.py", line 364, in create_parameter
    raise ValueError(
ValueError: parameter name [conv1_1_weights] have be been used. In dygraph mode, the name of parameter can't be same.Please check the parameter attr value passed to self.create_parameter or constructor of dygraph Layers

想请问正确的姿势是什么,想直接取这个模型用

jiangjiajun commented 3 years ago

参考下PaddleDetection中的做法 https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/deploy/EXPORT_ONNX_MODEL.md