MCPcopy Index your code
hub / github.com/Project-MONAI/MONAI / test_onnx_save

Function test_onnx_save

tests/test_utils.py:809–830  ·  view source on GitHub ↗

Test the ability to save `net` in ONNX format, reload it and validate with runtime. The value `inputs` is forward-passed through the `net` without gradient accumulation to do onnx export and PyTorch inference. PyTorch model inference is performed with CUDA if available, else CPU.

(net, *inputs, device=None, rtol=1e-4, atol=0.0)

Source from the content-addressed store, hash-verified

807
808
809def test_onnx_save(net, *inputs, device=None, rtol=1e-4, atol=0.0):
810 """
811 Test the ability to save `net` in ONNX format, reload it and validate with runtime.
812 The value `inputs` is forward-passed through the `net` without gradient accumulation
813 to do onnx export and PyTorch inference.
814 PyTorch model inference is performed with CUDA if available, else CPU.
815 Saved ONNX model is validated with onnxruntime, if available, else ONNX native implementation.
816 """
817 # TODO: would be nice to use GPU if available, but it currently causes CI failures.
818 device = "cpu"
819 _, has_onnxruntime = optional_import("onnxruntime")
820 with tempfile.TemporaryDirectory() as tempdir:
821 convert_to_onnx(
822 model=net,
823 filename=os.path.join(tempdir, "model.onnx"),
824 verify=True,
825 inputs=inputs,
826 device=device,
827 use_ort=has_onnxruntime,
828 rtol=rtol,
829 atol=atol,
830 )
831
832
833def download_url_or_skip_test(*args, **kwargs):

Callers 1

test_onnxMethod · 0.90

Calls 2

optional_importFunction · 0.90
convert_to_onnxFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…