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

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

Comments

Loading...