π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(onnx): recognize per-image tuple detector outputs (#6192)
Huashuo Lei4ae74adc76e9
diff --git a/changelog.d/6192.fixed.md b/changelog.d/6192.fixed.mdnew file mode 100644index 000000000..af84ccf8b--- /dev/null+++ b/changelog.d/6192.fixed.md@@ -0,0 +1 @@+`ObjectDetector.to_onnx` exports a detector whose post-processor returns a tuple with one tensor per image for a batch of one image, as it does for a list, and raises a `ValueError` when a `dynamic_axes` or `dynamic_shapes` argument makes that batch dynamic. It used to declare a dynamic batch: the TorchScript exporter's graph then returned only the traced images' detections at a larger batch, and the dynamo exporter of torch 2.9 and later failed with an error that did not name the cause. A tuple of batched outputs, such as `(boxes, scores)`, still exports a dynamic batch.diff --git a/kornia/contrib/object_detection.py b/kornia/contrib/object_detection.pyindex 277621cb2..3c274865b 100644--- a/kornia/contrib/object_detection.py+++ b/kornia/contrib/object_detection.py@@ -135,10 +135,11 @@ def results_from_detections(def _returns_detections_per_image(detector: nn.Module, images: torch.Tensor) -> bool:- """Return whether ``detector`` returns a list of per-image detections for ``images`` rather than one tensor.+ """Return whether ``detector`` returns a sequence of per-image detections rather than batched tensors.- The detector runs once, in eval mode and without gradients, on ``images`` moved to the dtype and device of its first- floating tensor, as the export's dummy input is. Its output decides, so any post-processor is covered. The+ The detector runs in eval mode and without gradients, on ``images`` moved to the dtype and device of its first+ floating tensor, as the export's dummy input is. A tuple is checked at a second batch size: its length must+ follow the batch, rather than name a fixed set of batched outputs such as ``(boxes, scores)``. Theattributes that ``to_onnx`` keeps, such as an image module's output cache, hold what they held before, in everymodule inside the detector, and so does the global random state."""@@ -148,7 +149,14 @@ def _returns_detections_per_image(detector: nn.Module, images: torch.Tensor) -># The run must not leave its output or parameters in a module inside the detector, nor consume the global random# state, as the export's own run does not.with _eval_mode(detector), _preserved_call_state(detector), _preserved_rng(), torch.no_grad():- return isinstance(detector(images), list)+ output = detector(images)+ if isinstance(output, list):+ return True+ if not isinstance(output, tuple) or len(output) != images.shape[0]:+ return False+ other_batch = 2 if images.shape[0] == 1 else 1+ other_output = detector(images[:1].expand(other_batch, *images.shape[1:]).contiguous())+ return isinstance(other_output, tuple) and len(other_output) == other_batchclass ObjectDetector(ModelBase, ONNXExportMixin):@@ -279,10 +287,11 @@ class ObjectDetector(ModelBase, ONNXExportMixin):kwargs: Additional keyword arguments of :func:`torch.onnx.export`.Note:- With a post-processor that returns a list of per-image detections, as a+ With a post-processor that returns a list or tuple of per-image detections, as a``kornia.models.rt_detr.DETRPostProcessor`` that filters by confidence does, the model isexported for a batch of one image: no ONNX output holds a list as long as the batch. Which kind of output- the detector returns is read from one forward pass at the export size, in eval mode and without gradients.+ the detector returns is read at the export size, in eval mode and without gradients. A tuple requires+ two forward passes with different batch sizes to distinguish per-image results from batched outputs.Build an RT-DETR detector with ``confidence_filtering=False`` to export a batch of any size. A``dynamic_axes`` or ``dynamic_shapes`` argument that makes the batch of such an export dynamic raises a``ValueError`` after the export, before anything is saved.@@ -295,7 +304,7 @@ class ObjectDetector(ModelBase, ONNXExportMixin):if onnx_name is None:onnx_name = f"kornia_{self.name}_{image_size}.onnx"pseudo_shape = [1, 3, image_size or 352, image_size or 352]- # Exported for a dynamic batch, a per-image list comes out as the traced batch's detections, or the export+ # Exported for a dynamic batch, a per-image sequence comes out as the traced batch's detections, or the export# fails: such a detector is exported for one image.batch = -1# The probe's input draws from the global generator, as the export's dummy does, and gives back what it drew.diff --git a/tests/onnx/test_object_detector_output.py b/tests/onnx/test_object_detector_output.pynew file mode 100644index 000000000..d251b0a1b--- /dev/null+++ b/tests/onnx/test_object_detector_output.py@@ -0,0 +1,183 @@+# LICENSE HEADER MANAGED BY add-license-header+#+# Copyright 2018 Kornia Team+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++import numpy as np+import pytest+import torch+from torch import nn++from kornia.contrib.object_detection import ObjectDetector, _returns_detections_per_image+from kornia.core import ImageModule+from kornia.core._compat import torch_version_lt+from kornia.models.processors import ResizePreProcessor++from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, dynamo_is_available+++class _Scored(nn.Module):+ def forward(self, images):+ signal = images.mean((-2, -1))+ return signal[:, None, :2], torch.cat((signal, signal[:, :1]), dim=-1)[:, None]+++class _Post(nn.Module):+ def __init__(self, output_kind):+ super().__init__()+ self.output_kind = output_kind++ def forward(self, logits, boxes, sizes):+ if self.output_kind == "batched_pair":+ return logits, boxes+ detections = torch.cat((logits, boxes), dim=-1)+ if self.output_kind == "one_tuple":+ return (detections,)+ if self.output_kind == "list":+ return [detections[i] for i in range(detections.shape[0])]+ return tuple(detections[i] for i in range(detections.shape[0]))+++def _detector(output_kind):+ return ObjectDetector(_Scored(), ResizePreProcessor(16, 16), _Post(output_kind)).eval()+++def _session(exported):+ ort = pytest.importorskip("onnxruntime")+ options = ort.SessionOptions()+ options.intra_op_num_threads = 1+ options.inter_op_num_threads = 1+ return ort.InferenceSession(exported.SerializeToString(), options, providers=["CPUExecutionProvider"])+++class TestObjectDetectorOutputSequence(BaseTester):+ @pytest.mark.parametrize("batch", [1, 2])+ # A 1-tuple of batched detections has the length of a batch of one: only the other batch size tells it apart.+ @pytest.mark.parametrize(+ "output_kind, expected", [("list", True), ("tuple", True), ("batched_pair", False), ("one_tuple", False)]+ )+ def test_probe(self, batch, output_kind, expected):+ detector = _detector(output_kind)+ assert _returns_detections_per_image(detector, torch.rand(batch, 3, 16, 16)) is expected++ @pytest.mark.parametrize("dynamo", [False, True])+ @pytest.mark.parametrize("output_kind", ["list", "tuple"])+ def test_fixed_batch_export(self, dynamo, output_kind):+ pytest.importorskip("onnx")+ if dynamo:+ pytest.importorskip("onnxscript")+ detector = _detector(output_kind)+ exported = detector.to_onnx(image_size=16, save=False, dynamo=dynamo)+ session = _session(exported)+ images = torch.rand(1, 3, 16, 16)++ assert exported.graph.input[0].type.tensor_type.shape.dim[0].dim_value == 1+ outputs = session.run(None, {"input": images.numpy()})+ assert len(outputs) == 1+ np.testing.assert_allclose(outputs[0], detector(images)[0].numpy(), rtol=1e-5, atol=1e-6)++ ort = pytest.importorskip("onnxruntime")+ with pytest.raises(ort.capi.onnxruntime_pybind11_state.InvalidArgument):+ session.run(None, {"input": torch.rand(3, 3, 16, 16).numpy()})++ @pytest.mark.parametrize(+ "export_kwargs",+ [+ pytest.param({"dynamo": False, "dynamic_axes": {"input": {0: "batch"}}}, id="torchscript"),+ pytest.param(+ {"dynamo": True, "dynamic_shapes": ({0: "batch"},)},+ # conftest.py deselects IDs containing "dynamo" unless KORNIA_TEST_OPTIMIZER is set.+ id="torch_export",+ marks=[+ pytest.mark.skipif(not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON),+ pytest.mark.skipif(torch_version_lt(2, 9, 0), reason="to_onnx uses dynamic_shapes from torch 2.9"),+ ],+ ),+ ],+ )+ @pytest.mark.parametrize("output_kind", ["list", "tuple"])+ def test_dynamic_per_image_batch_rejected(self, output_kind, export_kwargs, tmp_path):+ pytest.importorskip("onnx")+ if export_kwargs["dynamo"]:+ pytest.importorskip("onnxscript")+ output_path = tmp_path / "detector.onnx"+ with pytest.raises(ValueError, match="post-processor that returns one tensor"):+ _detector(output_kind).to_onnx(onnx_name=str(output_path), image_size=16, **export_kwargs)+ assert not output_path.exists()++ @pytest.mark.parametrize("dynamo", [False, True])+ def test_batched_pair_stays_dynamic(self, dynamo):+ pytest.importorskip("onnx")+ if dynamo:+ pytest.importorskip("onnxscript")+ detector = _detector("batched_pair")+ # Bound the dynamo dimension: torch 2.5 otherwise falls back to a static TorchScript graph.+ batch_options = (+ {"dynamic_shapes": ({0: torch.export.Dim("batch", min=1, max=8)},)}+ if dynamo+ else {"dynamic_axes": {"input": {0: "batch"}}}+ )+ exported = detector.to_onnx(+ image_size=16,+ save=False,+ dynamo=dynamo,+ output_names=["logits", "boxes"],+ **batch_options,+ )+ session = _session(exported)+ assert exported.graph.input[0].type.tensor_type.shape.dim[0].dim_param+ for batch in (1, 3, 5):+ images = torch.rand(batch, 3, 16, 16)+ outputs = session.run(None, {"input": images.numpy()})+ assert len(outputs) == 2+ for actual, expected in zip(outputs, detector(images)):+ np.testing.assert_allclose(actual, expected.numpy(), rtol=1e-5, atol=1e-6)++ def test_tuple_probe_preserves_rng_and_training_mode(self):+ class RandomScored(_Scored):+ def forward(self, images):+ logits, boxes = super().forward(images)+ return logits + torch.rand_like(logits), boxes++ detector = ObjectDetector(RandomScored(), ResizePreProcessor(16, 16), _Post("tuple")).train()+ images = torch.rand(1, 3, 16, 16)+ state = torch.get_rng_state().clone()+ assert _returns_detections_per_image(detector, images)+ assert torch.equal(state, torch.get_rng_state())+ assert all(module.training for module in detector.modules())++ def test_tuple_probe_preserves_image_module_cache(self):+ class CachedPre(ImageModule):+ def forward(self, images):+ return images, images.new_ones((images.shape[0], 2))++ pre = CachedPre()+ detector = ObjectDetector(_Scored(), pre, _Post("tuple"))+ pre(torch.rand(1, 3, 8, 8))+ cache = pre.__dict__["_output_image"]++ assert _returns_detections_per_image(detector, torch.rand(1, 3, 16, 16))+ assert pre.__dict__["_output_image"] is cache++ def test_tuple_probe_accepts_contiguous_only_preprocessor(self):+ class FlattenPre(nn.Module):+ def forward(self, images):+ images = images.view(-1).mul(0.5).view_as(images)+ return images, images.new_ones((images.shape[0], 2))++ detector = ObjectDetector(_Scored(), FlattenPre(), _Post("tuple"))+ images = torch.rand(1, 3, 16, 16)+ assert len(detector(images)) == 1+ assert _returns_detections_per_image(detector, images)