Description
Describe the bug
When trying to export the SwinUNETR model from MONAI, I get the error:
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.
In a different issue, I read that this issue might get fixed by changing x_shape = x.size()
to x_shape = [int(s) for s in x.size()]
in the problematic code -- I found out that problem manifests at proj_out()
. Doing this, though, results in a different error:
RuntimeError: Unsupported: ONNX export of instance_norm for unknown channel size.
Making this change in all places where I find x_shape = x.size()
results in a floating point exception!
To Reproduce
Here is a minimal example demonstrating the issue:
from monai.networks.nets import SwinUNETR
import torch
if __name__ == '__main__':
model = SwinUNETR(img_size=(96, 96, 96),
in_channels=1,
out_channels=5,
feature_size=48,
drop_rate=0.0,
attn_drop_rate=0.0,
dropout_path_rate=0.0,
use_checkpoint=True,
)
inputs = [torch.randn([1,1,96,96,96])]
input_names = ['input']
output_names = ['output']
torch.onnx.export(
model,
tuple(inputs), 'model.onnx',
verbose=False,
input_names=input_names,
output_names=output_names,
dynamic_axes=None,
opset_version=11,
)
Environment
================================
MONAI version: 0.9.1
Numpy version: 1.23.2
Pytorch version: 1.12.1.post200
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 356d2d2
MONAI file: .../envs/temp_env/lib/python3.10/site-packages/monai/init.py
Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: NOT INSTALLED or UNKNOWN VERSION.
scikit-image version: NOT INSTALLED or UNKNOWN VERSION.
Pillow version: NOT INSTALLED or UNKNOWN VERSION.
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: NOT INSTALLED or UNKNOWN VERSION.
tqdm version: NOT INSTALLED or UNKNOWN VERSION.
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: NOT INSTALLED or UNKNOWN VERSION.
pandas version: NOT INSTALLED or UNKNOWN VERSION.
einops version: 0.4.1
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.
For details about installing the optional dependencies, please visit:
https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies
================================
Printing system config...
psutil
required for print_system_info
================================
Printing GPU config...
Num GPUs: 1
Has CUDA: True
CUDA version: 11.2
cuDNN enabled: True
cuDNN version: 8401
Current device: 0
Library compiled for CUDA architectures: ['sm_35', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'compute_86']
GPU 0 Name: Tesla T4
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 40
GPU 0 Total memory (GB): 14.6
GPU 0 CUDA capability (maj.min): 7.5
Additional context
I have also filed an issue with Pytorch as I'm not certain on which side should the bug be resolved.