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

transforms_test.py 7.1 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  1. import unittest
  2. import numpy as np
  3. from super_gradients.training.transforms.keypoint_transforms import (
  4. KeypointsRandomHorizontalFlip,
  5. KeypointsRandomVerticalFlip,
  6. KeypointsRandomAffineTransform,
  7. KeypointsPadIfNeeded,
  8. KeypointsLongestMaxSize,
  9. )
  10. from super_gradients.training.transforms.transforms import DetectionImagePermute, DetectionPadToSize, DetectionRescale
  11. class TestTransforms(unittest.TestCase):
  12. def test_keypoints_random_affine(self):
  13. image = np.random.rand(640, 480, 3)
  14. mask = np.random.rand(640, 480)
  15. # Cover all image pixels with keypoints. This would guarantee test coverate of all possible keypoint locations
  16. # without relying on randomly generated keypoints.
  17. x = np.arange(image.shape[1])
  18. y = np.arange(image.shape[0])
  19. xv, yv = np.meshgrid(x, y, indexing="xy")
  20. joints = np.stack([xv.flatten(), yv.flatten(), np.ones_like(yv.flatten())], axis=-1) # [N, 3]
  21. joints = joints.reshape((-1, 1, 3)).repeat(17, axis=1) # [N, 17, 3]
  22. aug = KeypointsRandomAffineTransform(min_scale=0.8, max_scale=1.2, max_rotation=30, max_translate=0.5, prob=1, image_pad_value=0, mask_pad_value=0)
  23. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  24. joints_outside_image = (
  25. (aug_joints[:, :, 0] < 0) | (aug_joints[:, :, 1] < 0) | (aug_joints[:, :, 0] >= aug_image.shape[1]) | (aug_joints[:, :, 1] >= aug_image.shape[0])
  26. )
  27. # Ensure that keypoints outside the image are not visible
  28. self.assertTrue((aug_joints[joints_outside_image, 2] == 0).all(), msg=f"{aug_joints[joints_outside_image]}")
  29. # Ensure that all keypoints with visible status are inside the image
  30. # (There is no intersection of two sets: keypoints outside the image and keypoints with visible status)
  31. self.assertFalse((joints_outside_image & (aug_joints[:, :, 2] == 1)).any())
  32. def test_keypoints_horizontal_flip(self):
  33. image = np.random.rand(640, 480, 3)
  34. mask = np.random.rand(640, 480)
  35. joints = np.random.randint(0, 100, size=(1, 17, 3))
  36. aug = KeypointsRandomHorizontalFlip(flip_index=[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], prob=1)
  37. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  38. np.testing.assert_array_equal(aug_image, image[:, ::-1, :])
  39. np.testing.assert_array_equal(aug_mask, mask[:, ::-1])
  40. np.testing.assert_array_equal(image.shape[1] - aug_joints[:, ::-1, 0] - 1, joints[..., 0])
  41. np.testing.assert_array_equal(aug_joints[:, ::-1, 1], joints[..., 1])
  42. np.testing.assert_array_equal(aug_joints[:, ::-1, 2], joints[..., 2])
  43. def test_keypoints_vertical_flip(self):
  44. image = np.random.rand(640, 480, 3)
  45. mask = np.random.rand(640, 480)
  46. joints = np.random.randint(0, 100, size=(1, 17, 3))
  47. aug = KeypointsRandomVerticalFlip(prob=1)
  48. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  49. np.testing.assert_array_equal(aug_image, image[::-1, :, :])
  50. np.testing.assert_array_equal(aug_mask, mask[::-1, :])
  51. np.testing.assert_array_equal(aug_joints[..., 0], joints[..., 0])
  52. np.testing.assert_array_equal(image.shape[0] - aug_joints[..., 1] - 1, joints[..., 1])
  53. np.testing.assert_array_equal(aug_joints[..., 2], joints[..., 2])
  54. def test_keypoints_pad_if_needed(self):
  55. image = np.random.rand(640, 480, 3)
  56. mask = np.random.rand(640, 480)
  57. joints = np.random.randint(0, 100, size=(1, 17, 3))
  58. aug = KeypointsPadIfNeeded(min_width=768, min_height=768, image_pad_value=0, mask_pad_value=0)
  59. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  60. self.assertEqual(aug_image.shape, (768, 768, 3))
  61. self.assertEqual(aug_mask.shape, (768, 768))
  62. np.testing.assert_array_equal(aug_joints, joints)
  63. def test_keypoints_longest_max_size(self):
  64. image = np.random.rand(640, 480, 3)
  65. mask = np.random.rand(640, 480)
  66. joints = np.random.randint(0, 480, size=(1, 17, 3))
  67. aug = KeypointsLongestMaxSize(max_height=512, max_width=512)
  68. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  69. self.assertEqual(aug_image.shape[:2], aug_mask.shape[:2])
  70. self.assertLessEqual(aug_image.shape[0], 512)
  71. self.assertLessEqual(aug_image.shape[1], 512)
  72. self.assertTrue((aug_joints[..., 0] < aug_image.shape[1]).all())
  73. self.assertTrue((aug_joints[..., 1] < aug_image.shape[0]).all())
  74. def test_detection_image_permute(self):
  75. aug = DetectionImagePermute(dims=(2, 1, 0))
  76. image = np.random.rand(640, 480, 3)
  77. sample = {"image": image}
  78. output = aug(sample)
  79. self.assertEqual(output["image"].shape, (3, 480, 640))
  80. def test_detection_pad_to_size(self):
  81. aug = DetectionPadToSize(output_size=(640, 640), pad_value=123)
  82. image = np.ones((512, 480, 3))
  83. # Boxes in format (x1, y1, x2, y2, class_id)
  84. boxes = np.array([[0, 0, 100, 100, 0], [100, 100, 200, 200, 1]])
  85. sample = {"image": image, "target": boxes}
  86. output = aug(sample)
  87. shift_x = (640 - 480) // 2
  88. shift_y = (640 - 512) // 2
  89. expected_boxes = np.array(
  90. [[0 + shift_x, 0 + shift_y, 100 + shift_x, 100 + shift_y, 0], [100 + shift_x, 100 + shift_y, 200 + shift_x, 200 + shift_y, 1]]
  91. )
  92. self.assertEqual(output["image"].shape, (640, 640, 3))
  93. np.testing.assert_array_equal(output["target"], expected_boxes)
  94. self.assertEqual(aug.apply_reverse_to_image(output["image"]).shape, image.shape)
  95. np.testing.assert_array_equal(aug.apply_reverse_to_targets(output["target"]), boxes)
  96. def test_detection_rescale(self):
  97. # Test initialization
  98. rescale = DetectionRescale((300, 300))
  99. # Test __call__
  100. img = np.random.randint(0, 256, size=(100, 200, 3), dtype=np.uint8)
  101. targets = np.array([[10, 20, 30, 40, 0], [50, 60, 70, 80, 1]], dtype=np.float32)
  102. sample = {"image": img, "target": targets}
  103. ratio_x = 300 / 200
  104. ratio_y = 300 / 100
  105. expected_boxes = np.array([[10 * ratio_x, 20 * ratio_y, 30 * ratio_x, 40 * ratio_y, 0], [50 * ratio_x, 60 * ratio_y, 70 * ratio_x, 80 * ratio_y, 1]])
  106. transformed_sample = rescale(sample)
  107. transformed_img = transformed_sample["image"]
  108. transformed_targets = transformed_sample["target"]
  109. self.assertEqual(transformed_img.shape, (300, 300, 3))
  110. self.assertEqual(transformed_targets.shape, (2, 5))
  111. np.testing.assert_array_equal(transformed_targets, expected_boxes)
  112. # Test apply_reverse_to_targets
  113. reversed_targets = rescale.apply_reverse_to_targets(transformed_targets)
  114. self.assertEqual(reversed_targets.shape, (2, 5))
  115. np.testing.assert_array_equal(reversed_targets, targets)
  116. # Test apply_reverse_to_image
  117. reversed_img = rescale.apply_reverse_to_image(transformed_img)
  118. self.assertEqual(reversed_img.shape, img.shape)
  119. if __name__ == "__main__":
  120. unittest.main()
Tip!

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

Comments

Loading...