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.3 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
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": "", "resized_img_shape": None, "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()
Tip!

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

Comments

Loading...