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 17 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
  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 (
  11. DetectionImagePermute,
  12. DetectionPadToSize,
  13. DetectionHorizontalFlip,
  14. DetectionVerticalFlip,
  15. )
  16. from super_gradients.training.transforms.utils import (
  17. _rescale_image,
  18. _rescale_bboxes,
  19. _pad_image,
  20. _shift_bboxes,
  21. _rescale_and_pad_to_size,
  22. _rescale_xyxy_bboxes,
  23. _get_center_padding_coordinates,
  24. _get_bottom_right_padding_coordinates,
  25. PaddingCoordinates,
  26. )
  27. class TestTransforms(unittest.TestCase):
  28. def test_keypoints_random_affine(self):
  29. image = np.random.rand(640, 480, 3)
  30. mask = np.random.rand(640, 480)
  31. # Cover all image pixels with keypoints. This would guarantee test coverate of all possible keypoint locations
  32. # without relying on randomly generated keypoints.
  33. x = np.arange(image.shape[1])
  34. y = np.arange(image.shape[0])
  35. xv, yv = np.meshgrid(x, y, indexing="xy")
  36. joints = np.stack([xv.flatten(), yv.flatten(), np.ones_like(yv.flatten())], axis=-1) # [N, 3]
  37. joints = joints.reshape((-1, 1, 3)).repeat(17, axis=1) # [N, 17, 3]
  38. 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)
  39. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  40. joints_outside_image = (
  41. (aug_joints[:, :, 0] < 0) | (aug_joints[:, :, 1] < 0) | (aug_joints[:, :, 0] >= aug_image.shape[1]) | (aug_joints[:, :, 1] >= aug_image.shape[0])
  42. )
  43. # Ensure that keypoints outside the image are not visible
  44. self.assertTrue((aug_joints[joints_outside_image, 2] == 0).all(), msg=f"{aug_joints[joints_outside_image]}")
  45. # Ensure that all keypoints with visible status are inside the image
  46. # (There is no intersection of two sets: keypoints outside the image and keypoints with visible status)
  47. self.assertFalse((joints_outside_image & (aug_joints[:, :, 2] == 1)).any())
  48. def test_keypoints_horizontal_flip(self):
  49. image = np.random.rand(640, 480, 3)
  50. mask = np.random.rand(640, 480)
  51. joints = np.random.randint(0, 100, size=(1, 17, 3))
  52. aug = KeypointsRandomHorizontalFlip(flip_index=[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], prob=1)
  53. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  54. np.testing.assert_array_equal(aug_image, image[:, ::-1, :])
  55. np.testing.assert_array_equal(aug_mask, mask[:, ::-1])
  56. np.testing.assert_array_equal(image.shape[1] - aug_joints[:, ::-1, 0] - 1, joints[..., 0])
  57. np.testing.assert_array_equal(aug_joints[:, ::-1, 1], joints[..., 1])
  58. np.testing.assert_array_equal(aug_joints[:, ::-1, 2], joints[..., 2])
  59. def test_keypoints_vertical_flip(self):
  60. image = np.random.rand(640, 480, 3)
  61. mask = np.random.rand(640, 480)
  62. joints = np.random.randint(0, 100, size=(1, 17, 3))
  63. aug = KeypointsRandomVerticalFlip(prob=1)
  64. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  65. np.testing.assert_array_equal(aug_image, image[::-1, :, :])
  66. np.testing.assert_array_equal(aug_mask, mask[::-1, :])
  67. np.testing.assert_array_equal(aug_joints[..., 0], joints[..., 0])
  68. np.testing.assert_array_equal(image.shape[0] - aug_joints[..., 1] - 1, joints[..., 1])
  69. np.testing.assert_array_equal(aug_joints[..., 2], joints[..., 2])
  70. def test_keypoints_pad_if_needed(self):
  71. image = np.random.rand(640, 480, 3)
  72. mask = np.random.rand(640, 480)
  73. joints = np.random.randint(0, 100, size=(1, 17, 3))
  74. aug = KeypointsPadIfNeeded(min_width=768, min_height=768, image_pad_value=0, mask_pad_value=0)
  75. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  76. self.assertEqual(aug_image.shape, (768, 768, 3))
  77. self.assertEqual(aug_mask.shape, (768, 768))
  78. np.testing.assert_array_equal(aug_joints, joints)
  79. def test_keypoints_longest_max_size(self):
  80. image = np.random.rand(640, 480, 3)
  81. mask = np.random.rand(640, 480)
  82. joints = np.random.randint(0, 480, size=(1, 17, 3))
  83. aug = KeypointsLongestMaxSize(max_height=512, max_width=512)
  84. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  85. self.assertEqual(aug_image.shape[:2], aug_mask.shape[:2])
  86. self.assertLessEqual(aug_image.shape[0], 512)
  87. self.assertLessEqual(aug_image.shape[1], 512)
  88. self.assertTrue((aug_joints[..., 0] < aug_image.shape[1]).all())
  89. self.assertTrue((aug_joints[..., 1] < aug_image.shape[0]).all())
  90. def test_detection_image_permute(self):
  91. aug = DetectionImagePermute(dims=(2, 1, 0))
  92. image = np.random.rand(640, 480, 3)
  93. sample = {"image": image}
  94. output = aug(sample)
  95. self.assertEqual(output["image"].shape, (3, 480, 640))
  96. def test_detection_pad_to_size(self):
  97. aug = DetectionPadToSize(output_size=(640, 640), pad_value=123)
  98. image = np.ones((512, 480, 3))
  99. # Boxes in format (x1, y1, x2, y2, class_id)
  100. boxes = np.array([[0, 0, 100, 100, 0], [100, 100, 200, 200, 1]])
  101. sample = {"image": image, "target": boxes}
  102. output = aug(sample)
  103. shift_x = (640 - 480) // 2
  104. shift_y = (640 - 512) // 2
  105. expected_boxes = np.array(
  106. [[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]]
  107. )
  108. self.assertEqual(output["image"].shape, (640, 640, 3))
  109. np.testing.assert_array_equal(output["target"], expected_boxes)
  110. def test_rescale_image(self):
  111. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  112. target_shape = (320, 240)
  113. rescaled_image = _rescale_image(image, target_shape)
  114. # Check if the rescaled image has the correct target shape
  115. self.assertEqual(rescaled_image.shape[:2], target_shape)
  116. def test_detection_horizontal_flip(self):
  117. aug = DetectionHorizontalFlip(prob=1)
  118. image = np.random.rand(100, 100, 3)
  119. image_original = image.copy()
  120. # [x0, y0, x1, y1]
  121. bboxes = np.array(
  122. (
  123. (10, 10, 20, 20),
  124. (90, 90, 100, 100),
  125. )
  126. )
  127. bboxes_expected = np.array(
  128. (
  129. (80, 10, 90, 20),
  130. (0, 90, 10, 100),
  131. )
  132. )
  133. # run transform
  134. sample = {"image": image}
  135. sample["target"] = bboxes
  136. sample["crowd_targets"] = bboxes.copy()
  137. output = aug(sample)
  138. image = output["image"]
  139. target = output["target"]
  140. crowd_targets = output["crowd_targets"]
  141. # check image hasn't changed shape
  142. self.assertEqual(image.shape, image_original.shape)
  143. # check the first two cols of original image
  144. # match last two rows of flipped image
  145. self.assertTrue(np.array_equal(image_original[:, 0], image[:, -1]))
  146. self.assertTrue(np.array_equal(image_original[:, 1], image[:, -2]))
  147. # check bboxes as expected
  148. self.assertTrue(np.array_equal(target, bboxes_expected))
  149. self.assertTrue(np.array_equal(crowd_targets, bboxes_expected))
  150. def test_detection_vertical_flip(self):
  151. aug = DetectionVerticalFlip(prob=1)
  152. image = np.random.rand(100, 100, 3)
  153. image_original = image.copy()
  154. # [x0, y0, x1, y1]
  155. bboxes = np.array(
  156. (
  157. (10, 10, 20, 20),
  158. (90, 90, 100, 100),
  159. )
  160. )
  161. bboxes_expected = np.array(
  162. (
  163. (10, 80, 20, 90),
  164. (90, 0, 100, 10),
  165. )
  166. )
  167. # run transform
  168. sample = {"image": image}
  169. sample["target"] = bboxes
  170. sample["crowd_targets"] = bboxes.copy()
  171. output = aug(sample)
  172. image = output["image"]
  173. target = output["target"]
  174. crowd_targets = output["crowd_targets"]
  175. # check image hasn't changed shape
  176. self.assertEqual(image.shape, image_original.shape)
  177. # check top two rows of original image
  178. # matches bottom rows of flipped image
  179. self.assertTrue(np.array_equal(image_original[0], image[-1]))
  180. self.assertTrue(np.array_equal(image_original[1], image[-2]))
  181. # check bboxes as expected
  182. self.assertTrue(np.array_equal(target, bboxes_expected))
  183. self.assertTrue(np.array_equal(crowd_targets, bboxes_expected))
  184. def test_rescale_bboxes(self):
  185. sy, sx = (2.0, 0.5)
  186. # Empty bboxes
  187. bboxes = np.zeros((0, 4))
  188. expected_bboxes = np.zeros((0, 4))
  189. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  190. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  191. # Not empty bboxes
  192. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  193. 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)
  194. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  195. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  196. def test_pad_image(self):
  197. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  198. padding_coordinates = PaddingCoordinates(top=80, bottom=80, left=60, right=60)
  199. pad_value = 0
  200. shifted_image = _pad_image(image, padding_coordinates, pad_value)
  201. # Check if the shifted image has the correct shape
  202. self.assertEqual(shifted_image.shape, (800, 600, 3))
  203. # Check if the padding values are correct
  204. self.assertTrue((shifted_image[: padding_coordinates.top, :, :] == pad_value).all())
  205. self.assertTrue((shifted_image[-padding_coordinates.bottom :, :, :] == pad_value).all())
  206. self.assertTrue((shifted_image[:, : padding_coordinates.left, :] == pad_value).all())
  207. self.assertTrue((shifted_image[:, -padding_coordinates.right :, :] == pad_value).all())
  208. def test_shift_bboxes(self):
  209. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  210. shift_w, shift_h = 60, 80
  211. shifted_bboxes = _shift_bboxes(bboxes, shift_w, shift_h)
  212. # Check if the shifted bboxes have the correct values
  213. expected_bboxes = np.array([[70, 100, 110, 140, 1], [90, 120, 140, 170, 2]], dtype=np.float32)
  214. np.testing.assert_array_equal(shifted_bboxes, expected_bboxes)
  215. def test_rescale_xyxy_bboxes(self):
  216. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  217. r = 0.5
  218. rescaled_bboxes = _rescale_xyxy_bboxes(bboxes, r)
  219. # Check if the rescaled bboxes have the correct values
  220. 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)
  221. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  222. def test_padding(self):
  223. # Test Case 1: Padding needed
  224. image = np.array([[1, 2], [3, 4]])
  225. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  226. expected_padded_image = np.array([[1, 2, 114, 114], [3, 4, 114, 114], [114, 114, 114, 114]])
  227. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=114)
  228. np.testing.assert_array_equal(padded_image, expected_padded_image)
  229. # Test Case 2: No padding needed
  230. image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  231. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=0, right=0)
  232. expected_padded_image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  233. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=114)
  234. np.testing.assert_array_equal(padded_image, expected_padded_image)
  235. # Test Case 3: Image with channel dimension
  236. image = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
  237. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  238. expected_padded_image = np.array(
  239. [
  240. [[1, 2, 3], [4, 5, 6], [0, 0, 0], [0, 0, 0]],
  241. [[7, 8, 9], [10, 11, 12], [0, 0, 0], [0, 0, 0]],
  242. [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
  243. ],
  244. )
  245. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=0)
  246. np.testing.assert_array_equal(padded_image, expected_padded_image)
  247. def test_get_padding_coordinates(self):
  248. # Test Case 1: Width padding required
  249. image = np.zeros((640, 480))
  250. output_size = (640, 640)
  251. expected_center_padding = PaddingCoordinates(top=0, bottom=0, left=80, right=80)
  252. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=0, left=0, right=160)
  253. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  254. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  255. self.assertEqual(center_padding_coordinates, expected_center_padding)
  256. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  257. # Test Case 2: Height padding required
  258. image = np.zeros((480, 640))
  259. output_size = (640, 640)
  260. expected_center_padding = PaddingCoordinates(top=80, bottom=80, left=0, right=0)
  261. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=160, left=0, right=0)
  262. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  263. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  264. self.assertEqual(center_padding_coordinates, expected_center_padding)
  265. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  266. # Test Case 3: Width and Height padding required
  267. image = np.zeros((480, 640))
  268. output_size = (800, 800)
  269. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  270. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  271. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  272. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  273. self.assertEqual(center_padding_coordinates, expected_center_padding)
  274. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  275. # Test Case 4: Image shape is bigger than output shape
  276. image = np.zeros((800, 800))
  277. output_size = (640, 640)
  278. expected_center_padding = PaddingCoordinates(top=-80, bottom=-80, left=-80, right=-80)
  279. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=-160, left=0, right=-160)
  280. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  281. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  282. self.assertEqual(center_padding_coordinates, expected_center_padding)
  283. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  284. # Test Case 5: Width and Height padding required with an image of 3 channels
  285. image = np.zeros((480, 640, 3))
  286. output_size = (800, 800)
  287. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  288. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  289. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  290. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  291. self.assertEqual(center_padding_coordinates, expected_center_padding)
  292. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  293. def test_rescale_and_pad_to_size(self):
  294. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  295. output_size = (800, 500)
  296. pad_val = 114
  297. rescaled_padded_image, r = _rescale_and_pad_to_size(image, output_size, pad_val=pad_val)
  298. # Check if the rescaled and padded image has the correct shape
  299. self.assertEqual(rescaled_padded_image.shape, (3, *output_size))
  300. # Check if the image is rescaled with the correct ratio
  301. resized_image_shape = (int(image.shape[0] * r), int(image.shape[1] * r))
  302. # Check if the padding is correctly applied
  303. padded_area = rescaled_padded_image[:, resized_image_shape[0] :, :] # Right padding area
  304. self.assertTrue((padded_area == pad_val).all())
  305. padded_area = rescaled_padded_image[:, :, resized_image_shape[1] :] # Bottom padding area
  306. self.assertTrue((padded_area == pad_val).all())
  307. if __name__ == "__main__":
  308. unittest.main()
Tip!

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

Comments

Loading...