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

#278 Adding new version of detection dataset, PascalVOC and PascalVOC dataset interface

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-171-DetectionDatasetV2_with_PascalVOC
@@ -1,12 +1,71 @@
 import unittest
 import unittest
-from super_gradients.training.datasets import Cifar10DatasetInterface
+
+from super_gradients.training.datasets.dataset_interfaces.dataset_interface import PascalVOCUnifiedDetectionDatasetInterface
+from super_gradients.training.transforms.transforms import DetectionPaddedRescale, DetectionTargetsFormatTransform, DetectionMosaic, DetectionRandomAffine,\
+    DetectionHSV
+from super_gradients.training.utils.detection_utils import DetectionTargetsFormat
+from super_gradients.training.utils.detection_utils import DetectionCollateFN
+from super_gradients.training.utils import sg_model_utils
+from super_gradients.training import utils as core_utils
 
 
 
 
 class TestDatasetInterface(unittest.TestCase):
 class TestDatasetInterface(unittest.TestCase):
-    def test_cifar(self):
-        test_dataset_interface = Cifar10DatasetInterface()
-        cifar_dataset_sample = test_dataset_interface.get_test_sample()
-        self.assertListEqual([3, 32, 32], list(cifar_dataset_sample[0].shape))
+    def setUp(self) -> None:
+        self.root_dir = "/home/data/"
+        self.train_batch_size, self.val_batch_size = 16, 32
+        self.train_image_size, self.val_image_size = 640, 640
+        self.train_input_dim = (self.train_image_size, self.train_image_size)
+        self.val_input_dim = (self.val_image_size, self.val_image_size)
+        self.train_max_num_samples = 100
+        self.val_max_num_samples = 90
+
+    def setup_pascal_voc_interface(self):
+        """setup PascalVOCUnifiedDetectionDataSetInterfaceV2 and return dataloaders"""
+        dataset_params = {
+            "data_dir": self.root_dir + "pascal_unified_coco_format/",
+            "cache_dir": self.root_dir + "pascal_unified_coco_format/",
+            "batch_size": self.train_batch_size,
+            "val_batch_size": self.val_batch_size,
+            "train_image_size": self.train_image_size,
+            "val_image_size": self.val_image_size,
+            "train_max_num_samples": self.train_max_num_samples,
+            "val_max_num_samples": self.val_max_num_samples,
+            "train_transforms": [
+                DetectionMosaic(input_dim=self.train_input_dim, prob=1),
+                DetectionRandomAffine(degrees=0.373, translate=0.245, scales=0.898, shear=0.602, target_size=self.train_input_dim),
+                DetectionHSV(prob=1, hgain=0.0138, sgain=0.664, vgain=0.464),
+                DetectionPaddedRescale(input_dim=self.train_input_dim, max_targets=100),
+                DetectionTargetsFormatTransform(input_format=DetectionTargetsFormat.XYXY_LABEL,
+                                                output_format=DetectionTargetsFormat.LABEL_CXCYWH)],
+            "val_transforms": [
+                DetectionPaddedRescale(input_dim=self.val_input_dim),
+                DetectionTargetsFormatTransform(input_format=DetectionTargetsFormat.XYXY_LABEL,
+                                                output_format=DetectionTargetsFormat.LABEL_CXCYWH)],
+            "train_collate_fn": DetectionCollateFN(),
+            "val_collate_fn": DetectionCollateFN(),
+            "download": False,
+            "cache_train_images": False,
+            "cache_val_images": False,
+            "class_inclusion_list": ["person"]
+        }
+        dataset_interface = PascalVOCUnifiedDetectionDatasetInterface(dataset_params=dataset_params)
+        train_loader, valid_loader, _test_loader, _classes = dataset_interface.get_data_loaders()
+        return train_loader, valid_loader
+
+    def test_pascal_voc(self):
+        """Check that the dataset interface is correctly instantiated, and that the batch items are of expected size"""
+        train_loader, valid_loader = self.setup_pascal_voc_interface()
+
+        for loader, batch_size, image_size, max_num_samples in [(train_loader, self.train_batch_size, self.train_image_size, self.train_max_num_samples),
+                                                                (valid_loader, self.val_batch_size, self.val_image_size, self.val_max_num_samples)]:
+            # The dataset is at most of length max_num_samples, but can be smaller if not enough samples
+            self.assertGreaterEqual(max_num_samples, len(loader.dataset))
+
+            batch_items = next(iter(loader))
+            batch_items = core_utils.tensor_container_to_device(batch_items, 'cuda', non_blocking=True)
+
+            inputs, targets, additional_batch_items = sg_model_utils.unpack_batch_items(batch_items)
+            self.assertListEqual([batch_size, 3, image_size, image_size], list(inputs.shape))
 
 
 
 
 if __name__ == '__main__':
 if __name__ == '__main__':
Discard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  1. import unittest
  2. import numpy as np
  3. from super_gradients.training.datasets import DetectionDataset
  4. from super_gradients.training.utils.detection_utils import DetectionTargetsFormat
  5. from super_gradients.training.exceptions.dataset_exceptions import EmptyDatasetException
  6. class DummyDetectionDataset(DetectionDataset):
  7. def __init__(self, input_dim, *args, **kwargs):
  8. """Dummy Dataset testing subclassing, designed with no annotation that includes class_2."""
  9. self.dummy_targets = [np.array([[0, 0, 10, 10, 0],
  10. [0, 5, 10, 15, 0],
  11. [0, 5, 15, 20, 0]]),
  12. np.array([[0, 0, 10, 10, 0],
  13. [0, 5, 10, 15, 0],
  14. [0, 15, 55, 20, 1]])]
  15. self.image_size = input_dim
  16. kwargs['all_classes_list'] = ["class_0", "class_1", "class_2"]
  17. kwargs['original_target_format'] = DetectionTargetsFormat.XYXY_LABEL
  18. super().__init__(data_dir='', input_dim=input_dim, *args, **kwargs)
  19. def _setup_data_source(self):
  20. return len(self.dummy_targets)
  21. def _load_annotation(self, sample_id: int) -> dict:
  22. """Load 2 different annotations.
  23. - Annotation 0 is made of: 3 targets of class 0, 0 of class_1 and 0 of class_2
  24. - Annotation 1 is made of: 2 targets of class_0, 1 of class_1 and 0 of class_2
  25. """
  26. return {"img_path": "", "target": self.dummy_targets[sample_id]}
  27. # DetectionDatasetV2 will call _load_image but since we don't have any image we patch this method with
  28. # tensor of image shape
  29. def _load_image(self, index: int) -> np.ndarray:
  30. return np.random.random(self.image_size)
  31. class TestDetectionDatasetSubclassing(unittest.TestCase):
  32. def setUp(self) -> None:
  33. self.config_keep_empty_annotation = [
  34. {
  35. "class_inclusion_list": ["class_0", "class_1", "class_2"],
  36. "expected_n_targets_after_subclass": [3, 3]
  37. },
  38. {
  39. "class_inclusion_list": ["class_0"],
  40. "expected_n_targets_after_subclass": [3, 2]
  41. },
  42. {
  43. "class_inclusion_list": ["class_1"],
  44. "expected_n_targets_after_subclass": [0, 1]
  45. },
  46. {
  47. "class_inclusion_list": ["class_2"],
  48. "expected_n_targets_after_subclass": [0, 0]
  49. },
  50. ]
  51. self.config_ignore_empty_annotation = [
  52. {
  53. "class_inclusion_list": ["class_0", "class_1", "class_2"],
  54. "expected_n_targets_after_subclass": [3, 3]
  55. },
  56. {
  57. "class_inclusion_list": ["class_0"],
  58. "expected_n_targets_after_subclass": [3, 2]
  59. },
  60. {
  61. "class_inclusion_list": ["class_1"],
  62. "expected_n_targets_after_subclass": [1]
  63. }
  64. ]
  65. def test_subclass_keep_empty(self):
  66. """Check that subclassing only keeps annotations of wanted class"""
  67. for config in self.config_keep_empty_annotation:
  68. test_dataset = DummyDetectionDataset(input_dim=(640, 512), ignore_empty_annotations=False,
  69. class_inclusion_list=config["class_inclusion_list"])
  70. n_targets_after_subclass = _count_targets_after_subclass_per_index(test_dataset)
  71. self.assertListEqual(config["expected_n_targets_after_subclass"], n_targets_after_subclass)
  72. def test_subclass_drop_empty(self):
  73. """Check that empty annotations are not indexed (i.e. ignored) when ignore_empty_annotations=True"""
  74. for config in self.config_ignore_empty_annotation:
  75. test_dataset = DummyDetectionDataset(input_dim=(640, 512), ignore_empty_annotations=True,
  76. class_inclusion_list=config["class_inclusion_list"])
  77. n_targets_after_subclass = _count_targets_after_subclass_per_index(test_dataset)
  78. self.assertListEqual(config["expected_n_targets_after_subclass"], n_targets_after_subclass)
  79. # Check last case when class_2, which should raise EmptyDatasetException because not a single image has
  80. # a target in class_inclusion_list
  81. with self.assertRaises(EmptyDatasetException):
  82. DummyDetectionDataset(input_dim=(640, 512), ignore_empty_annotations=True,
  83. class_inclusion_list=["class_2"])
  84. def test_wrong_subclass(self):
  85. """Check that ValueError is raised when class_inclusion_list includes a class that does not exist."""
  86. with self.assertRaises(ValueError):
  87. DummyDetectionDataset(input_dim=(640, 512), class_inclusion_list=["non_existing_class"])
  88. with self.assertRaises(ValueError):
  89. DummyDetectionDataset(input_dim=(640, 512), class_inclusion_list=["class_0", "non_existing_class"])
  90. def _count_targets_after_subclass_per_index(test_dataset: DummyDetectionDataset):
  91. """Iterate through every index of the dataset and count the associated number of targets per index"""
  92. dataset_target_len = []
  93. for index in range(len(test_dataset)):
  94. _img, targets = test_dataset[index]
  95. dataset_target_len.append(len(targets))
  96. return dataset_target_len
  97. if __name__ == '__main__':
  98. unittest.main()
Discard
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  1. import unittest
  2. import numpy as np
  3. import torch
  4. from super_gradients.training.datasets import DetectionDataset
  5. from super_gradients.training.utils.detection_utils import DetectionTargetsFormat
  6. class DummyDetectionDataset(DetectionDataset):
  7. def __init__(self, dataset_size, input_dim, *args, **kwargs):
  8. """Dummy Dataset testing subsampling."""
  9. self.dataset_size = dataset_size
  10. self.image_size = input_dim
  11. kwargs['all_classes_list'] = ["class_0", "class_1", "class_2"]
  12. kwargs['original_target_format'] = DetectionTargetsFormat.XYXY_LABEL
  13. super().__init__(data_dir='', input_dim=input_dim, *args, **kwargs)
  14. def _setup_data_source(self):
  15. return self.dataset_size
  16. def _load_annotation(self, sample_id: int) -> dict:
  17. """Load dummy annotation"""
  18. return {"img_path": "", "target": torch.zeros(10, 6)}
  19. # DetectionDatasetV2 will call _load_image but since we don't have any image we patch this method with
  20. # tensor of image shape
  21. def _load_image(self, index: int) -> np.ndarray:
  22. return np.random.random(self.image_size)
  23. class TestDetectionDatasetSubsampling(unittest.TestCase):
  24. def test_subsampling(self):
  25. """Check that subsampling works"""
  26. for max_num_samples in [1, 1_000, 1_000_000]:
  27. test_dataset = DummyDetectionDataset(dataset_size=100_000, input_dim=(640, 512), max_num_samples=max_num_samples)
  28. self.assertEqual(len(test_dataset), min(max_num_samples, 100_000))
  29. if __name__ == '__main__':
  30. unittest.main()
Discard