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 15 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  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
  11. from super_gradients.training.transforms.utils import (
  12. _rescale_image,
  13. _rescale_bboxes,
  14. _pad_image,
  15. _shift_bboxes,
  16. _rescale_and_pad_to_size,
  17. _rescale_xyxy_bboxes,
  18. _get_center_padding_coordinates,
  19. _get_bottom_right_padding_coordinates,
  20. PaddingCoordinates,
  21. )
  22. class TestTransforms(unittest.TestCase):
  23. def test_keypoints_random_affine(self):
  24. image = np.random.rand(640, 480, 3)
  25. mask = np.random.rand(640, 480)
  26. # Cover all image pixels with keypoints. This would guarantee test coverate of all possible keypoint locations
  27. # without relying on randomly generated keypoints.
  28. x = np.arange(image.shape[1])
  29. y = np.arange(image.shape[0])
  30. xv, yv = np.meshgrid(x, y, indexing="xy")
  31. joints = np.stack([xv.flatten(), yv.flatten(), np.ones_like(yv.flatten())], axis=-1) # [N, 3]
  32. joints = joints.reshape((-1, 1, 3)).repeat(17, axis=1) # [N, 17, 3]
  33. 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)
  34. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  35. joints_outside_image = (
  36. (aug_joints[:, :, 0] < 0) | (aug_joints[:, :, 1] < 0) | (aug_joints[:, :, 0] >= aug_image.shape[1]) | (aug_joints[:, :, 1] >= aug_image.shape[0])
  37. )
  38. # Ensure that keypoints outside the image are not visible
  39. self.assertTrue((aug_joints[joints_outside_image, 2] == 0).all(), msg=f"{aug_joints[joints_outside_image]}")
  40. # Ensure that all keypoints with visible status are inside the image
  41. # (There is no intersection of two sets: keypoints outside the image and keypoints with visible status)
  42. self.assertFalse((joints_outside_image & (aug_joints[:, :, 2] == 1)).any())
  43. def test_keypoints_horizontal_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 = KeypointsRandomHorizontalFlip(flip_index=[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], 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(image.shape[1] - aug_joints[:, ::-1, 0] - 1, joints[..., 0])
  52. np.testing.assert_array_equal(aug_joints[:, ::-1, 1], joints[..., 1])
  53. np.testing.assert_array_equal(aug_joints[:, ::-1, 2], joints[..., 2])
  54. def test_keypoints_vertical_flip(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 = KeypointsRandomVerticalFlip(prob=1)
  59. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  60. np.testing.assert_array_equal(aug_image, image[::-1, :, :])
  61. np.testing.assert_array_equal(aug_mask, mask[::-1, :])
  62. np.testing.assert_array_equal(aug_joints[..., 0], joints[..., 0])
  63. np.testing.assert_array_equal(image.shape[0] - aug_joints[..., 1] - 1, joints[..., 1])
  64. np.testing.assert_array_equal(aug_joints[..., 2], joints[..., 2])
  65. def test_keypoints_pad_if_needed(self):
  66. image = np.random.rand(640, 480, 3)
  67. mask = np.random.rand(640, 480)
  68. joints = np.random.randint(0, 100, size=(1, 17, 3))
  69. aug = KeypointsPadIfNeeded(min_width=768, min_height=768, image_pad_value=0, mask_pad_value=0)
  70. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  71. self.assertEqual(aug_image.shape, (768, 768, 3))
  72. self.assertEqual(aug_mask.shape, (768, 768))
  73. np.testing.assert_array_equal(aug_joints, joints)
  74. def test_keypoints_longest_max_size(self):
  75. image = np.random.rand(640, 480, 3)
  76. mask = np.random.rand(640, 480)
  77. joints = np.random.randint(0, 480, size=(1, 17, 3))
  78. aug = KeypointsLongestMaxSize(max_height=512, max_width=512)
  79. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  80. self.assertEqual(aug_image.shape[:2], aug_mask.shape[:2])
  81. self.assertLessEqual(aug_image.shape[0], 512)
  82. self.assertLessEqual(aug_image.shape[1], 512)
  83. self.assertTrue((aug_joints[..., 0] < aug_image.shape[1]).all())
  84. self.assertTrue((aug_joints[..., 1] < aug_image.shape[0]).all())
  85. def test_detection_image_permute(self):
  86. aug = DetectionImagePermute(dims=(2, 1, 0))
  87. image = np.random.rand(640, 480, 3)
  88. sample = {"image": image}
  89. output = aug(sample)
  90. self.assertEqual(output["image"].shape, (3, 480, 640))
  91. def test_detection_pad_to_size(self):
  92. aug = DetectionPadToSize(output_size=(640, 640), pad_value=123)
  93. image = np.ones((512, 480, 3))
  94. # Boxes in format (x1, y1, x2, y2, class_id)
  95. boxes = np.array([[0, 0, 100, 100, 0], [100, 100, 200, 200, 1]])
  96. sample = {"image": image, "target": boxes}
  97. output = aug(sample)
  98. shift_x = (640 - 480) // 2
  99. shift_y = (640 - 512) // 2
  100. expected_boxes = np.array(
  101. [[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]]
  102. )
  103. self.assertEqual(output["image"].shape, (640, 640, 3))
  104. np.testing.assert_array_equal(output["target"], expected_boxes)
  105. def test_rescale_image(self):
  106. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  107. target_shape = (320, 240)
  108. rescaled_image = _rescale_image(image, target_shape)
  109. # Check if the rescaled image has the correct target shape
  110. self.assertEqual(rescaled_image.shape[:2], target_shape)
  111. def test_rescale_bboxes(self):
  112. sy, sx = (2.0, 0.5)
  113. # Empty bboxes
  114. bboxes = np.zeros((0, 4))
  115. expected_bboxes = np.zeros((0, 4))
  116. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  117. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  118. # Not empty bboxes
  119. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  120. expected_bboxes = np.array([[5.0, 40.0, 25.0, 120.0, 1.0], [15.0, 80.0, 40.0, 180.0, 2.0]], dtype=np.float32)
  121. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  122. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  123. def test_pad_image(self):
  124. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  125. padding_coordinates = PaddingCoordinates(top=80, bottom=80, left=60, right=60)
  126. pad_value = 0
  127. shifted_image = _pad_image(image, padding_coordinates, pad_value)
  128. # Check if the shifted image has the correct shape
  129. self.assertEqual(shifted_image.shape, (800, 600, 3))
  130. # Check if the padding values are correct
  131. self.assertTrue((shifted_image[: padding_coordinates.top, :, :] == pad_value).all())
  132. self.assertTrue((shifted_image[-padding_coordinates.bottom :, :, :] == pad_value).all())
  133. self.assertTrue((shifted_image[:, : padding_coordinates.left, :] == pad_value).all())
  134. self.assertTrue((shifted_image[:, -padding_coordinates.right :, :] == pad_value).all())
  135. def test_shift_bboxes(self):
  136. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  137. shift_w, shift_h = 60, 80
  138. shifted_bboxes = _shift_bboxes(bboxes, shift_w, shift_h)
  139. # Check if the shifted bboxes have the correct values
  140. expected_bboxes = np.array([[70, 100, 110, 140, 1], [90, 120, 140, 170, 2]], dtype=np.float32)
  141. np.testing.assert_array_equal(shifted_bboxes, expected_bboxes)
  142. def test_rescale_xyxy_bboxes(self):
  143. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  144. r = 0.5
  145. rescaled_bboxes = _rescale_xyxy_bboxes(bboxes, r)
  146. # Check if the rescaled bboxes have the correct values
  147. expected_bboxes = np.array([[5.0, 10.0, 25.0, 30.0, 1.0], [15.0, 20.0, 40.0, 45.0, 2.0]], dtype=np.float32)
  148. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  149. def test_padding(self):
  150. # Test Case 1: Padding needed
  151. image = np.array([[1, 2], [3, 4]])
  152. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  153. expected_padded_image = np.array([[1, 2, 114, 114], [3, 4, 114, 114], [114, 114, 114, 114]])
  154. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=114)
  155. np.testing.assert_array_equal(padded_image, expected_padded_image)
  156. # Test Case 2: No padding needed
  157. image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  158. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=0, right=0)
  159. expected_padded_image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  160. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=114)
  161. np.testing.assert_array_equal(padded_image, expected_padded_image)
  162. # Test Case 3: Image with channel dimension
  163. image = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
  164. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  165. expected_padded_image = np.array(
  166. [
  167. [[1, 2, 3], [4, 5, 6], [0, 0, 0], [0, 0, 0]],
  168. [[7, 8, 9], [10, 11, 12], [0, 0, 0], [0, 0, 0]],
  169. [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
  170. ],
  171. )
  172. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=0)
  173. np.testing.assert_array_equal(padded_image, expected_padded_image)
  174. def test_get_padding_coordinates(self):
  175. # Test Case 1: Width padding required
  176. image = np.zeros((640, 480))
  177. output_size = (640, 640)
  178. expected_center_padding = PaddingCoordinates(top=0, bottom=0, left=80, right=80)
  179. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=0, left=0, right=160)
  180. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  181. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  182. self.assertEqual(center_padding_coordinates, expected_center_padding)
  183. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  184. # Test Case 2: Height padding required
  185. image = np.zeros((480, 640))
  186. output_size = (640, 640)
  187. expected_center_padding = PaddingCoordinates(top=80, bottom=80, left=0, right=0)
  188. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=160, left=0, right=0)
  189. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  190. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  191. self.assertEqual(center_padding_coordinates, expected_center_padding)
  192. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  193. # Test Case 3: Width and Height padding required
  194. image = np.zeros((480, 640))
  195. output_size = (800, 800)
  196. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  197. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  198. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  199. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  200. self.assertEqual(center_padding_coordinates, expected_center_padding)
  201. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  202. # Test Case 4: Image shape is bigger than output shape
  203. image = np.zeros((800, 800))
  204. output_size = (640, 640)
  205. expected_center_padding = PaddingCoordinates(top=-80, bottom=-80, left=-80, right=-80)
  206. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=-160, left=0, right=-160)
  207. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  208. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  209. self.assertEqual(center_padding_coordinates, expected_center_padding)
  210. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  211. # Test Case 5: Width and Height padding required with an image of 3 channels
  212. image = np.zeros((480, 640, 3))
  213. output_size = (800, 800)
  214. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  215. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  216. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  217. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  218. self.assertEqual(center_padding_coordinates, expected_center_padding)
  219. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  220. def test_rescale_and_pad_to_size(self):
  221. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  222. output_size = (800, 500)
  223. pad_val = 114
  224. rescaled_padded_image, r = _rescale_and_pad_to_size(image, output_size, pad_val=pad_val)
  225. # Check if the rescaled and padded image has the correct shape
  226. self.assertEqual(rescaled_padded_image.shape, (3, *output_size))
  227. # Check if the image is rescaled with the correct ratio
  228. resized_image_shape = (int(image.shape[0] * r), int(image.shape[1] * r))
  229. # Check if the padding is correctly applied
  230. padded_area = rescaled_padded_image[:, resized_image_shape[0] :, :] # Right padding area
  231. self.assertTrue((padded_area == pad_val).all())
  232. padded_area = rescaled_padded_image[:, :, resized_image_shape[1] :] # Bottom padding area
  233. self.assertTrue((padded_area == pad_val).all())
  234. if __name__ == "__main__":
  235. unittest.main()
Tip!

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

Comments

Loading...