ONNX 不支持 operator adaptive_avg_pool2d 的导出

作者: statiqueplasma创建于 2026年2月10日更新于 2026年2月10日

Context

After training my model using the Lightning Module, I tried exporting the resulting model to Pytorch and ONNX. I followed the training structure used in the git repo, then exported the model to pytorch, and tried to load it and export it to ONNX (the code is below). I used a LightningModule and Dataset class structures that are identical to the ones described in the repo notebook. And my library versions are as follows:

  • PyTorch: 2.4.0a0+f70bd71a48.nv24.06
  • Torch CUDA available: True
  • PyTorch Lightning: 2.6.1
  • Segmentation Models PyTorch: 0.5.0
  • NumPy: 1.26.4
  • OpenCV: 4.9.0

Error

The training, export to pytorch, and loading are handled fine, no issue in these steps, I can even run inferences using the loaded smp.PSPNet model. But when trying to export the pytorch smp.PSPNet model to ONNX, I run into a SymbolicValueError saying:

Unsupported: ONNX export of operator adaptive_avg_pool2d, output size that are not factor of input size. Please feel free to request support or submit a Pull Request on PyTorch GitHub: https://GitHub.com/pytorch/pytorch/issues  [Caused by the value '469 defined in (%469 : Long(2, strides=[1], device=cpu) = onnx::Constant[value= 3  3 [ CPULongType{2} ]]())' (type 'Tensor') in the TorchScript graph. The containing node has kind 'onnx::Constant'.] 
Input:
Empty
Output:
#0: 469 defined in (%469 : Long(2, strides=[1], device=cpu) = onnx::Constant[value= 3  3 [ CPULongType{2} ]]())
(type 'Tensor')
# Discussion
The input used in the ONNX export is the exact input used in the training (which is `128x128`) since I used the same `Dataloader`, so I find it very weird that the error is showing a completely different number and that it leads to a problem with the ONNX export.

内容来源: qubvel-org/segmentation_models.pytorch