最近在把pytorch模型转换onnx格式模型的时候,遇到了如下错误:
RuntimeError: Failed to export an ONNX attribute 'onnx::Gather', since it's not constant, please try to make things (e.g., kernel size) static if possible
在网上查了下,这个连接是跟我的问题相关的:
https://github.com/onnx/tutorials/issues/137
修改了下pytorch的版本,还是未能解决,我的torch版本已经是比较高的了1.7.0.
后来仔细看这个连接的时候,发现下面这段话:
I think it's a general problem when the input to the functional layer is dynamic.
I had a situation where functional avg_pool3d that depended on the shape of the previous layer's outputs. One has to either make the kernel constant or switch to non-functional pytorch's api.
将代码中的avg_poo2d替换成了adaptive_avg_pool2d,再从新导出,就可以正常导出onnx模型了。