关于pytorch 转 onnx ,TracerWarning解决

已遇到情况举例:
1 不能有if…else
如:

if nx == na:  
    x = x + a

因为pytorch过于灵活,个人感觉 onnx计算图,需要固定的计算公式,逻辑判断语句会出问题,
如上面语句,我debug了下,在我需要的yolov3情况下,发现nx==na一直成立,所以直接去掉“ if nx == na: ”,就OK了

2 参数覆盖(原地修改/计算)
如:

p[:,0:2] = torch.sigmoid(p[:,0:2])

下面是问题描述:

TracerWarning: There are 3 live references to the data region being modified when tracing in-place operator copy_ (possibly due to an assignment). This might cause the trace to be incorrect, because all other views that also reference this data will not reflect this change in the trace! On the other hand, if all other views use the same memory chunk, but are disjoint (e.g. are outputs of torch.split), this might still be safe. p[:, 0:2] = torch.sigmoid(p[:, 0:2]) + grid  # x, y

大概意思是占用相同内存块。使得映射计算过程出了问题,
个人理解:一些计算参数 相同内存地址的覆盖,onnx 转换 需要整个网络参数都有独立的内存地址。
p[:,0:2] = torch.sigmoid(p[:,0:2])
公式的意思是,对矩阵p对应位置进行sigmoid运算,onnx不支持在原位置运算操作的,所以需要在声明一个临时或新的变量(如tem),来承载计算后的结果。

所以修改方式如下,

tem = torch.sigmoid(p[:,0:2])

就是再重新定义一个参量,反正最后return,得到结果就行了,无非是多占用了些内存

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值