Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#561 Feature/sg 193 extend output formator

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-193-extend_detection_target_transform
@@ -6,8 +6,9 @@ import unittest
 import numpy as np
 import numpy as np
 import torch
 import torch
 
 
+
 from super_gradients.common.factories.bbox_format_factory import BBoxFormatFactory
 from super_gradients.common.factories.bbox_format_factory import BBoxFormatFactory
-from super_gradients.training.utils.bbox_formats import (
+from super_gradients.training.datasets.data_formats.bbox_formats import (
     CXCYWHCoordinateFormat,
     CXCYWHCoordinateFormat,
     NormalizedXYXYCoordinateFormat,
     NormalizedXYXYCoordinateFormat,
     NormalizedXYWHCoordinateFormat,
     NormalizedXYWHCoordinateFormat,
@@ -19,21 +20,21 @@ from super_gradients.training.utils.bbox_formats import (
     BBOX_FORMATS,
     BBOX_FORMATS,
     BoundingBoxFormat,
     BoundingBoxFormat,
 )
 )
-from super_gradients.training.utils.bbox_formats.normalized_cxcywh import (
+from super_gradients.training.datasets.data_formats.bbox_formats.normalized_cxcywh import (
     normalized_cxcywh_to_xyxy_inplace,
     normalized_cxcywh_to_xyxy_inplace,
     xyxy_to_normalized_cxcywh_inplace,
     xyxy_to_normalized_cxcywh_inplace,
     xyxy_to_normalized_cxcywh,
     xyxy_to_normalized_cxcywh,
     normalized_cxcywh_to_xyxy,
     normalized_cxcywh_to_xyxy,
 )
 )
-from super_gradients.training.utils.bbox_formats.normalized_xywh import (
+from super_gradients.training.datasets.data_formats.bbox_formats.normalized_xywh import (
     xyxy_to_normalized_xywh_inplace,
     xyxy_to_normalized_xywh_inplace,
     xyxy_to_normalized_xywh,
     xyxy_to_normalized_xywh,
     normalized_xywh_to_xyxy_inplace,
     normalized_xywh_to_xyxy_inplace,
     normalized_xywh_to_xyxy,
     normalized_xywh_to_xyxy,
 )
 )
-from super_gradients.training.utils.bbox_formats.xywh import xyxy_to_xywh, xywh_to_xyxy, xywh_to_xyxy_inplace, xyxy_to_xywh_inplace
-from super_gradients.training.utils.bbox_formats.yxyx import xyxy_to_yxyx, xyxy_to_yxyx_inplace
-from super_gradients.training.utils.output_adapters.detection_adapter import ConvertBoundingBoxes
+from super_gradients.training.datasets.data_formats.bbox_formats.xywh import xyxy_to_xywh, xywh_to_xyxy, xywh_to_xyxy_inplace, xyxy_to_xywh_inplace
+from super_gradients.training.datasets.data_formats.bbox_formats.yxyx import xyxy_to_yxyx, xyxy_to_yxyx_inplace
+from super_gradients.training.datasets.data_formats.output_adapters.detection_adapter import ConvertBoundingBoxes
 
 
 
 
 class BBoxFormatsTest(unittest.TestCase):
 class BBoxFormatsTest(unittest.TestCase):
@@ -255,7 +256,7 @@ class BBoxFormatsTest(unittest.TestCase):
                 with tempfile.TemporaryDirectory() as tmpdirname:
                 with tempfile.TemporaryDirectory() as tmpdirname:
                     adapter_fname = os.path.join(tmpdirname, "adapter.onnx")
                     adapter_fname = os.path.join(tmpdirname, "adapter.onnx")
                     # Just test that export works, we test the correctness in the detection_output_adapter_test.py
                     # Just test that export works, we test the correctness in the detection_output_adapter_test.py
-                    torch.onnx.export(module, gt_bboxes.clone(), adapter_fname)
+                    torch.onnx.export(module, gt_bboxes.clone(), adapter_fname, opset_version=11)
 
 
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
Discard
@@ -7,8 +7,13 @@ import onnx
 import onnxruntime as ort
 import onnxruntime as ort
 import torch.jit
 import torch.jit
 
 
-from super_gradients.training.utils.bbox_formats import NormalizedXYWHCoordinateFormat, CXCYWHCoordinateFormat, YXYXCoordinateFormat
-from super_gradients.training.utils.output_adapters import DetectionOutputAdapter, ConcatenatedTensorFormat, BoundingBoxesTensorSliceItem, TensorSliceItem
+from super_gradients.training.datasets.data_formats.bbox_formats import NormalizedXYWHCoordinateFormat, CXCYWHCoordinateFormat, YXYXCoordinateFormat
+from super_gradients.training.datasets.data_formats.output_adapters.detection_adapter import DetectionOutputAdapter
+from super_gradients.training.datasets.data_formats import (
+    ConcatenatedTensorFormat,
+    BoundingBoxesTensorSliceItem,
+    TensorSliceItem,
+)
 
 
 NORMALIZED_XYWH_SCORES_LABELS = ConcatenatedTensorFormat(
 NORMALIZED_XYWH_SCORES_LABELS = ConcatenatedTensorFormat(
     layout=(
     layout=(
@@ -119,7 +124,7 @@ class TestDetectionOutputAdapter(unittest.TestCase):
 
 
             with tempfile.TemporaryDirectory() as tmpdirname:
             with tempfile.TemporaryDirectory() as tmpdirname:
                 adapter_fname = os.path.join(tmpdirname, "adapter.onnx")
                 adapter_fname = os.path.join(tmpdirname, "adapter.onnx")
-                torch.onnx.export(adapter, inp, f=adapter_fname, input_names=["predictions"], output_names=["output_predictions"])
+                torch.onnx.export(adapter, inp, f=adapter_fname, input_names=["predictions"], output_names=["output_predictions"], opset_version=11)
 
 
                 onnx_model = onnx.load(adapter_fname)
                 onnx_model = onnx.load(adapter_fname)
                 onnx.checker.check_model(onnx_model)
                 onnx.checker.check_model(onnx_model)
Discard
@@ -2,7 +2,14 @@ import numpy as np
 import unittest
 import unittest
 
 
 from super_gradients.training.transforms.transforms import DetectionTargetsFormatTransform
 from super_gradients.training.transforms.transforms import DetectionTargetsFormatTransform
-from super_gradients.training.utils.detection_utils import DetectionTargetsFormat
+
+from super_gradients.training.datasets.data_formats.default_formats import (
+    XYXY_LABEL,
+    LABEL_XYXY,
+    LABEL_CXCYWH,
+    LABEL_NORMALIZED_XYXY,
+    LABEL_NORMALIZED_CXCYWH,
+)
 
 
 
 
 class DetectionTargetsTransformTest(unittest.TestCase):
 class DetectionTargetsTransformTest(unittest.TestCase):
@@ -12,110 +19,115 @@ class DetectionTargetsTransformTest(unittest.TestCase):
     def test_label_first_2_label_last(self):
     def test_label_first_2_label_last(self):
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         output = np.array([[50, 10, 20, 30, 40]], dtype=np.float32)
         output = np.array([[50, 10, 20, 30, 40]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.XYXY_LABEL,
-                                                    output_format=DetectionTargetsFormat.LABEL_XYXY)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
-        self.assertTrue(np.array_equal(transform(sample)["target"], output))
+
+        transform = DetectionTargetsFormatTransform(image_shape=self.image.shape[1:], max_targets=1, input_format=XYXY_LABEL, output_format=LABEL_XYXY)
+        t_output = transform(sample)["target"]
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_xyxy_2_normalized_xyxy(self):
     def test_xyxy_2_normalized_xyxy(self):
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_XYXY,
-                                                    output_format=DetectionTargetsFormat.LABEL_NORMALIZED_XYXY)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(
+            image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_XYXY, output_format=LABEL_NORMALIZED_XYXY
+        )
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.array_equal(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_xyxy_2_cxcywh(self):
     def test_xyxy_2_cxcywh(self):
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         output = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
         output = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_XYXY,
-                                                    output_format=DetectionTargetsFormat.LABEL_CXCYWH)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_XYXY, output_format=LABEL_CXCYWH)
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.array_equal(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_xyxy_2_normalized_cxcywh(self):
     def test_xyxy_2_normalized_cxcywh(self):
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         output = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
         output = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_XYXY,
-                                                    output_format=DetectionTargetsFormat.LABEL_NORMALIZED_CXCYWH)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(
+            image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_XYXY, output_format=LABEL_NORMALIZED_CXCYWH
+        )
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.array_equal(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_normalized_xyxy_2_cxcywh(self):
     def test_normalized_xyxy_2_cxcywh(self):
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         output = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
         output = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_NORMALIZED_XYXY,
-                                                    output_format=DetectionTargetsFormat.LABEL_CXCYWH)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(
+            image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_XYXY, output_format=LABEL_CXCYWH
+        )
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.allclose(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_normalized_xyxy_2_normalized_cxcywh(self):
     def test_normalized_xyxy_2_normalized_cxcywh(self):
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         output = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
         output = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_NORMALIZED_XYXY,
-                                                    output_format=DetectionTargetsFormat.LABEL_NORMALIZED_CXCYWH)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(
+            image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_XYXY, output_format=LABEL_NORMALIZED_CXCYWH
+        )
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.allclose(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_cxcywh_2_xyxy(self):
     def test_cxcywh_2_xyxy(self):
         output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
         input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_CXCYWH,
-                                                    output_format=DetectionTargetsFormat.LABEL_XYXY)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_CXCYWH, output_format=LABEL_XYXY)
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.array_equal(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_cxcywh_2_normalized_xyxy(self):
     def test_cxcywh_2_normalized_xyxy(self):
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
         input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_CXCYWH,
-                                                    output_format=DetectionTargetsFormat.LABEL_NORMALIZED_XYXY)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(
+            image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_CXCYWH, output_format=LABEL_NORMALIZED_XYXY
+        )
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.array_equal(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_normalized_cxcywh_2_xyxy(self):
     def test_normalized_cxcywh_2_xyxy(self):
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
         input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
         output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
         output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_NORMALIZED_CXCYWH,
-                                                    output_format=DetectionTargetsFormat.LABEL_XYXY)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(
+            image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_CXCYWH, output_format=LABEL_XYXY
+        )
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.allclose(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
     def test_normalized_cxcywh_2_normalized_xyxy(self):
     def test_normalized_cxcywh_2_normalized_xyxy(self):
         _, h, w = self.image.shape
         _, h, w = self.image.shape
         output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
         input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
         input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
-        transform = DetectionTargetsFormatTransform(max_targets=1,
-                                                    input_format=DetectionTargetsFormat.LABEL_NORMALIZED_CXCYWH,
-                                                    output_format=DetectionTargetsFormat.LABEL_NORMALIZED_XYXY)
         sample = {"image": self.image, "target": input}
         sample = {"image": self.image, "target": input}
+
+        transform = DetectionTargetsFormatTransform(
+            image_shape=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_CXCYWH, output_format=LABEL_NORMALIZED_XYXY
+        )
         t_output = transform(sample)["target"]
         t_output = transform(sample)["target"]
-        self.assertTrue(np.allclose(output, t_output))
+        self.assertTrue(np.allclose(output, t_output, atol=1e-6))
 
 
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     unittest.main()
     unittest.main()
Discard