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

detection_sub_classing_test.py 5.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. from super_gradients.common.exceptions.dataset_exceptions import EmptyDatasetException, DatasetValidationException
  2. import unittest
  3. import numpy as np
  4. from super_gradients.training.datasets import DetectionDataset
  5. from super_gradients.training.datasets.data_formats.default_formats import XYXY_LABEL
  6. from super_gradients.training.datasets.data_formats.formats import ConcatenatedTensorFormat
  7. class DummyDetectionDataset(DetectionDataset):
  8. def __init__(self, input_dim, target_format: ConcatenatedTensorFormat, *args, **kwargs):
  9. """Dummy Dataset testing subclassing, designed with no annotation that includes class_2."""
  10. self.dummy_targets = [
  11. np.array([[0, 0, 10, 10, 0], [0, 5, 10, 15, 0], [0, 5, 15, 20, 0]]),
  12. np.array([[0, 0, 10, 10, 0], [0, 5, 10, 15, 0], [0, 15, 55, 20, 1]]),
  13. ]
  14. self.image_size = input_dim
  15. kwargs["all_classes_list"] = ["class_0", "class_1", "class_2"]
  16. kwargs["original_target_format"] = target_format
  17. super().__init__(data_dir="", input_dim=input_dim, *args, **kwargs)
  18. def _setup_data_source(self):
  19. return len(self.dummy_targets)
  20. def _load_annotation(self, sample_id: int) -> dict:
  21. """Load 2 different annotations.
  22. - Annotation 0 is made of: 3 targets of class 0, 0 of class_1 and 0 of class_2
  23. - Annotation 1 is made of: 2 targets of class_0, 1 of class_1 and 0 of class_2
  24. """
  25. return {"img_path": "", "resized_img_shape": None, "target": self.dummy_targets[sample_id]}
  26. # DetectionDatasetV2 will call _load_image but since we don't have any image we patch this method with
  27. # tensor of image shape
  28. def _load_image(self, image_path: str) -> np.ndarray:
  29. return np.random.random(self.image_size)
  30. class TestDetectionDatasetSubclassing(unittest.TestCase):
  31. def setUp(self) -> None:
  32. self.config_keep_empty_annotation = [
  33. {"class_inclusion_list": ["class_0", "class_1", "class_2"], "expected_n_targets_after_subclass": [3, 3]},
  34. {"class_inclusion_list": ["class_0"], "expected_n_targets_after_subclass": [3, 2]},
  35. {"class_inclusion_list": ["class_1"], "expected_n_targets_after_subclass": [0, 1]},
  36. {"class_inclusion_list": ["class_2"], "expected_n_targets_after_subclass": [0, 0]},
  37. ]
  38. self.config_ignore_empty_annotation = [
  39. {"class_inclusion_list": ["class_0", "class_1", "class_2"], "expected_n_targets_after_subclass": [3, 3]},
  40. {"class_inclusion_list": ["class_0"], "expected_n_targets_after_subclass": [3, 2]},
  41. {"class_inclusion_list": ["class_1"], "expected_n_targets_after_subclass": [1]},
  42. ]
  43. def test_subclass_keep_empty(self):
  44. """Check that subclassing only keeps annotations of wanted class"""
  45. for config in self.config_keep_empty_annotation:
  46. test_dataset = DummyDetectionDataset(
  47. input_dim=(640, 512), ignore_empty_annotations=False, class_inclusion_list=config["class_inclusion_list"], target_format=XYXY_LABEL
  48. )
  49. n_targets_after_subclass = _count_targets_after_subclass_per_index(test_dataset)
  50. self.assertListEqual(config["expected_n_targets_after_subclass"], n_targets_after_subclass)
  51. def test_subclass_drop_empty(self):
  52. """Check that empty annotations are not indexed (i.e. ignored) when ignore_empty_annotations=True"""
  53. for config in self.config_ignore_empty_annotation:
  54. test_dataset = DummyDetectionDataset(
  55. input_dim=(640, 512), ignore_empty_annotations=True, class_inclusion_list=config["class_inclusion_list"], target_format=XYXY_LABEL
  56. )
  57. n_targets_after_subclass = _count_targets_after_subclass_per_index(test_dataset)
  58. self.assertListEqual(config["expected_n_targets_after_subclass"], n_targets_after_subclass)
  59. # Check last case when class_2, which should raise EmptyDatasetException because not a single image has
  60. # a target in class_inclusion_list
  61. with self.assertRaises(EmptyDatasetException):
  62. DummyDetectionDataset(input_dim=(640, 512), ignore_empty_annotations=True, class_inclusion_list=["class_2"], target_format=XYXY_LABEL)
  63. def test_wrong_subclass(self):
  64. """Check that ValueError is raised when class_inclusion_list includes a class that does not exist."""
  65. with self.assertRaises(DatasetValidationException):
  66. DummyDetectionDataset(input_dim=(640, 512), class_inclusion_list=["non_existing_class"], target_format=XYXY_LABEL)
  67. with self.assertRaises(DatasetValidationException):
  68. DummyDetectionDataset(input_dim=(640, 512), class_inclusion_list=["class_0", "non_existing_class"], target_format=XYXY_LABEL)
  69. def test_legacy_detection_targets_format(self):
  70. """Check that ValueError is raised when class_inclusion_list includes a class that does not exist."""
  71. for config in self.config_keep_empty_annotation:
  72. test_dataset = DummyDetectionDataset(
  73. input_dim=(640, 512),
  74. ignore_empty_annotations=False,
  75. class_inclusion_list=config["class_inclusion_list"],
  76. target_format=XYXY_LABEL,
  77. )
  78. n_targets_after_subclass = _count_targets_after_subclass_per_index(test_dataset)
  79. self.assertListEqual(config["expected_n_targets_after_subclass"], n_targets_after_subclass)
  80. def _count_targets_after_subclass_per_index(test_dataset: DummyDetectionDataset):
  81. """Iterate through every index of the dataset and count the associated number of targets per index"""
  82. dataset_target_len = []
  83. for index in range(len(test_dataset)):
  84. _img, targets = test_dataset[index]
  85. dataset_target_len.append(len(targets))
  86. return dataset_target_len
  87. if __name__ == "__main__":
  88. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...