torch转onnx遇到的坑(二)

anaconda3/lib/python3.9/site-packages/mmcv/ops/point_sample.py:76: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
  y1 = torch.where(y1 > padded_h - 1, torch.tensor(padded_h - 1).to(device), y1)

之前的博文里讲了用open-mmcv中的mmcv.ops.point_sample.bilinear_grid_sample解决onnx不支持torch.nn.functional.grid_sample的问题

但是近期在onnxruntime推理过程中又遇到了mmcv中grid_sample不支持动态输入的问题,转onnx模型时有上述警告,一开始没有当回事,在静态输入时也完全没有问题,直到动态推理时遇到了axis不匹配的问题。

最终找到了解决方案:

使用onnxsim将onnx模型简化,对复杂模块封装,这意外的解决了我的问题

  1. 安装onnxsim
pip install onnxsim
  1. 简化你原本的onnx模型
onnxsim inp.onnx out.onnx
  1. 加载新的onnx模型进行动态推理
    顺利解决问题

原onnx模型:
在这里插入图片描述
问题主要在红框圈出来的部分 造成了最下方的concat维度不匹配

经过onnxsim优化后的模型在这里插入图片描述
可以看出 输出的结果成功的变为动态维度了

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值