pytorch转换成onnx模型的坑:Attribute 'strides' is expected to have field 'ints'

训练完pytorch模型后,将其转换成onnx模型:

torch.onnx.export(model,               # model being run
                  x,                         # model input (or a tuple for multiple inputs)
                  "resnet18_float.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=10,          # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  dynamic_axes={'input' : {0 : '1'},    # variable lenght axes
                                'output' : {0 : '1'}}

成功转换。
但是如果网络结构中有F.avg_pool2d()即平均池化层时可能会遇到一些问题。特别是这样使用:

out = F.avg_pool2d(out, 4)

这样转换的onnx模型的平均池化层中strides是空的(可以用netron查看)。
如果用onnxruntime进行推理,则会报错:
Error in Node: : Attribute ‘strides’ is expected to have field ‘ints’
解决办法:指定stride

out = F.avg_pool2d(out, 4, stride=4)

似乎最新版本的pytorch已经解决这个问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值