-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ONNX exportability compatibity test and fix (#275)
fix: Fix for ONNX export of Maxblurpool layer and performance optimization by registering kernel as a buffer so that it doesn't need to be copied to the GPU over and over again. ### Description - **What**: Converting the pretrained models to ONNX format gives error in the Maxpool layer used in the N2V2 architecture.This is mainly because the convolution kernel is dynamically expanded to a size matching the number of channels in the input in the Maxblurpool layer. But the number of channels should be constant within the model. - **Why**: Users can convert the pytorch models to ONNX for inference in thier platforms - **How**: -- instead of using the symbolic variable x.size(1), explicitly cast it to an integer and make it a constant. -- make the kernel as a buffer to avoid the copying to GPU overhead. -- add tests for ONNX exportability ### Changes Made - **Added**: -- onnx as a test dependency in pyproject.toml -- 'test_lightning_module_onnx_exportability.py' - **Modified**: Maxblurpool module in 'layers.py' **Please ensure your PR meets the following requirements:** - [x] Code builds and passes tests locally, including doctests - [x] New tests have been added (for bug fixes/features) - [x] Pre-commit passes - [ ] PR to the documentation exists (for bug fixes / features) Co-authored-by: Joran Deschamps <[email protected]>
- Loading branch information
1 parent
0e0bc28
commit f0fcc89
Showing
3 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ dev = [ | |
"pre-commit", | ||
"pytest", | ||
"pytest-cov", | ||
"onnx", | ||
"sybil", # doctesting | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/lightning/test_lightning_module_onnx_exportability.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import pytest | ||
import torch | ||
from onnx import checker | ||
|
||
from careamics.config import FCNAlgorithmConfig | ||
from careamics.lightning.lightning_module import FCNModule | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"algorithm, architecture, conv_dim, n2v2, loss, shape", | ||
[ | ||
("n2n", "UNet", 2, False, "mae", (16, 16)), # n2n 2D model | ||
("n2n", "UNet", 3, False, "mae", (8, 16, 16)), # n2n 3D model | ||
("n2v", "UNet", 2, False, "n2v", (16, 16)), # n2v 2D model | ||
("n2v", "UNet", 3, False, "n2v", (8, 16, 16)), # n2v 3D model | ||
("n2v", "UNet", 2, True, "n2v", (16, 16)), # n2v2 2D model | ||
("n2v", "UNet", 3, True, "n2v", (8, 16, 16)), # n2v2 3D model | ||
], | ||
) | ||
def test_onnx_export(tmp_path, algorithm, architecture, conv_dim, n2v2, loss, shape): | ||
"""Test model exportability to ONNX.""" | ||
|
||
algo_config = { | ||
"algorithm": algorithm, | ||
"model": { | ||
"architecture": architecture, | ||
"conv_dims": conv_dim, | ||
"in_channels": 1, | ||
"num_classes": 1, | ||
"depth": 3, | ||
"n2v2": n2v2, | ||
}, | ||
"loss": loss, | ||
} | ||
algo_config = FCNAlgorithmConfig(**algo_config) | ||
|
||
# instantiate CAREamicsKiln | ||
model = FCNModule(algo_config) | ||
# set model to evaluation mode to avoid batch dimension error | ||
model.model.eval() | ||
# create a sample input of BC(Z)XY | ||
x = torch.rand((1, 1, *shape)) | ||
|
||
# create dynamic axes from the shape of the x | ||
dynamic_axes = {"input": {}, "output": {}} | ||
for i in range(len(x.shape)): | ||
dynamic_axes["input"][i] = f"dim_{i}" | ||
dynamic_axes["output"][i] = f"dim_{i}" | ||
|
||
torch.onnx.export( | ||
model, | ||
x, | ||
f"{tmp_path}/test_model.onnx", | ||
input_names=["input"], # the model's input names | ||
output_names=["output"], # the model's output names | ||
dynamic_axes=dynamic_axes, # variable length axes, | ||
) | ||
|
||
checker.check_model(f"{tmp_path}/test_model.onnx") |