Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix object detection onnx (#2555)

Edgar Riba8809110fea62
diff --git a/test/contrib/models/test_rt_detr.py b/test/contrib/models/test_rt_detr.py
index 3d4fc8450..7c74b5cb4 100644
--- a/test/contrib/models/test_rt_detr.py
+++ b/test/contrib/models/test_rt_detr.py
@@ -9,7 +9,6 @@ from kornia.contrib.models.rt_detr.architecture.resnet_d import ResNetD
from kornia.contrib.models.rt_detr.architecture.rtdetr_head import RTDETRHead
from kornia.contrib.models.rt_detr.model import RTDETR, RTDETRConfig
from kornia.testing import BaseTester, assert_close
-from kornia.utils._compat import torch_version
@pytest.mark.parametrize(
@@ -120,17 +119,3 @@ class TestRTDETR(BaseTester):
actual = model_optimized(img)
self.assert_close(actual, expected)
-
- @pytest.mark.skipif(
- torch_version() in ("2.0.0", "2.0.1"),
- reason="aten::scaled_dot_product_attention cannot be exported to ONNX. See https://github.com/pytorch/pytorch/issues/97262",
- ) # remove this once the fix is released to stable
- @pytest.mark.skipif(not hasattr(torch.onnx, "symbolic_opset16"), reason="F.grid_sample() requires ONNX opset 16")
- @pytest.mark.parametrize("variant", ("resnet50d", "hgnetv2_l"))
- def test_onnx(self, variant, tmp_path, dtype):
- # NOTE: correctness check is not included
- model = RTDETR.from_config(RTDETRConfig(variant, 80)).to(dtype).eval()
- img = torch.rand(1, 3, 224, 256, dtype=dtype)
- onnx_path = str(tmp_path / "rtdetr.onnx")
-
- torch.onnx.export(model, img, onnx_path, opset_version=16)
diff --git a/test/contrib/test_contrib.py b/test/contrib/test_contrib.py
index 0be010abc..f3a3c6b3a 100644
--- a/test/contrib/test_contrib.py
+++ b/test/contrib/test_contrib.py
@@ -10,6 +10,7 @@ import kornia.testing as utils # test utils
from kornia.contrib.face_detection import FaceKeypoint
from kornia.contrib.models.rt_detr import RTDETR, DETRPostProcessor, RTDETRConfig
from kornia.testing import assert_close
+from kornia.utils._compat import torch_version_lt
class TestDiamondSquare:
@@ -761,14 +762,16 @@ class TestObjectDetector:
assert torch.all(dets[:, 0].int() == dets[:, 0])
assert torch.all(dets[:, 1] >= 0.3)
- def test_onnx(self, tmp_path: Path):
- config = RTDETRConfig("resnet18d", 80)
- model = RTDETR.from_config(config)
+ @pytest.mark.skipif(torch_version_lt(2, 0, 0), reason="Unsupported ONNX opset version: 16")
+ @pytest.mark.parametrize("variant", ("resnet50d", "hgnetv2_l"))
+ def test_onnx(self, device, dtype, tmp_path: Path, variant: str):
+ config = RTDETRConfig(variant, 1)
+ model = RTDETR.from_config(config).to(device=device, dtype=dtype).eval()
pre_processor = kornia.contrib.object_detection.ResizePreProcessor(640)
post_processor = DETRPostProcessor(0.3)
detector = kornia.contrib.ObjectDetector(model, pre_processor, post_processor)
- data = torch.rand(3, 400, 640)
+ data = torch.rand(3, 400, 640, device=device, dtype=dtype)
model_path = tmp_path / "rtdetr.onnx"