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

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

Comments

Loading...