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 22 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  1. import copy
  2. import unittest
  3. import cv2
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from super_gradients.training.transforms import KeypointsMixup, KeypointsCompose
  7. from super_gradients.training.transforms.keypoint_transforms import (
  8. KeypointsRandomHorizontalFlip,
  9. KeypointsRandomVerticalFlip,
  10. KeypointsRandomAffineTransform,
  11. KeypointsPadIfNeeded,
  12. KeypointsLongestMaxSize,
  13. )
  14. from super_gradients.training.samples import PoseEstimationSample
  15. from super_gradients.training.transforms.keypoints import KeypointsBrightnessContrast, KeypointsMosaic
  16. from super_gradients.training.transforms.transforms import (
  17. DetectionImagePermute,
  18. DetectionPadToSize,
  19. DetectionHorizontalFlip,
  20. DetectionVerticalFlip,
  21. )
  22. from super_gradients.training.transforms.utils import (
  23. _rescale_image,
  24. _rescale_bboxes,
  25. _pad_image,
  26. _shift_bboxes,
  27. _rescale_and_pad_to_size,
  28. _rescale_xyxy_bboxes,
  29. _get_center_padding_coordinates,
  30. _get_bottom_right_padding_coordinates,
  31. PaddingCoordinates,
  32. )
  33. class TestTransforms(unittest.TestCase):
  34. def test_keypoints_random_affine(self):
  35. image = np.random.rand(640, 480, 3)
  36. mask = np.random.rand(640, 480)
  37. # Cover all image pixels with keypoints. This would guarantee test coverate of all possible keypoint locations
  38. # without relying on randomly generated keypoints.
  39. x = np.arange(image.shape[1])
  40. y = np.arange(image.shape[0])
  41. xv, yv = np.meshgrid(x, y, indexing="xy")
  42. joints = np.stack([xv.flatten(), yv.flatten(), np.ones_like(yv.flatten())], axis=-1) # [N, 3]
  43. joints = joints.reshape((-1, 1, 3)).repeat(17, axis=1) # [N, 17, 3]
  44. 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)
  45. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  46. joints_outside_image = (
  47. (aug_joints[:, :, 0] < 0) | (aug_joints[:, :, 1] < 0) | (aug_joints[:, :, 0] >= aug_image.shape[1]) | (aug_joints[:, :, 1] >= aug_image.shape[0])
  48. )
  49. # Ensure that keypoints outside the image are not visible
  50. self.assertTrue((aug_joints[joints_outside_image, 2] == 0).all(), msg=f"{aug_joints[joints_outside_image]}")
  51. # Ensure that all keypoints with visible status are inside the image
  52. # (There is no intersection of two sets: keypoints outside the image and keypoints with visible status)
  53. self.assertFalse((joints_outside_image & (aug_joints[:, :, 2] == 1)).any())
  54. def test_keypoints_horizontal_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 = KeypointsRandomHorizontalFlip(flip_index=[16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], 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(image.shape[1] - aug_joints[:, ::-1, 0] - 1, joints[..., 0])
  63. np.testing.assert_array_equal(aug_joints[:, ::-1, 1], joints[..., 1])
  64. np.testing.assert_array_equal(aug_joints[:, ::-1, 2], joints[..., 2])
  65. def test_keypoints_vertical_flip(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 = KeypointsRandomVerticalFlip(prob=1)
  70. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  71. np.testing.assert_array_equal(aug_image, image[::-1, :, :])
  72. np.testing.assert_array_equal(aug_mask, mask[::-1, :])
  73. np.testing.assert_array_equal(aug_joints[..., 0], joints[..., 0])
  74. np.testing.assert_array_equal(image.shape[0] - aug_joints[..., 1] - 1, joints[..., 1])
  75. np.testing.assert_array_equal(aug_joints[..., 2], joints[..., 2])
  76. def test_keypoints_pad_if_needed(self):
  77. image = np.random.rand(640, 480, 3)
  78. mask = np.random.rand(640, 480)
  79. joints = np.random.randint(0, 100, size=(1, 17, 3))
  80. aug = KeypointsPadIfNeeded(min_width=768, min_height=768, image_pad_value=0, mask_pad_value=0)
  81. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  82. self.assertEqual(aug_image.shape, (768, 768, 3))
  83. self.assertEqual(aug_mask.shape, (768, 768))
  84. np.testing.assert_array_equal(aug_joints, joints)
  85. def test_keypoints_longest_max_size(self):
  86. image = np.random.rand(640, 480, 3)
  87. mask = np.random.rand(640, 480)
  88. joints = np.random.randint(0, 480, size=(1, 17, 3))
  89. aug = KeypointsLongestMaxSize(max_height=512, max_width=512)
  90. aug_image, aug_mask, aug_joints, _, _ = aug(image, mask, joints, None, None)
  91. self.assertEqual(aug_image.shape[:2], aug_mask.shape[:2])
  92. self.assertLessEqual(aug_image.shape[0], 512)
  93. self.assertLessEqual(aug_image.shape[1], 512)
  94. self.assertTrue((aug_joints[..., 0] < aug_image.shape[1]).all())
  95. self.assertTrue((aug_joints[..., 1] < aug_image.shape[0]).all())
  96. def test_detection_image_permute(self):
  97. aug = DetectionImagePermute(dims=(2, 1, 0))
  98. image = np.random.rand(640, 480, 3)
  99. sample = {"image": image}
  100. output = aug(sample)
  101. self.assertEqual(output["image"].shape, (3, 480, 640))
  102. def test_detection_pad_to_size(self):
  103. aug = DetectionPadToSize(output_size=(640, 640), pad_value=123)
  104. image = np.ones((512, 480, 3))
  105. # Boxes in format (x1, y1, x2, y2, class_id)
  106. boxes = np.array([[0, 0, 100, 100, 0], [100, 100, 200, 200, 1]])
  107. sample = {"image": image, "target": boxes}
  108. output = aug(sample)
  109. shift_x = (640 - 480) // 2
  110. shift_y = (640 - 512) // 2
  111. expected_boxes = np.array(
  112. [[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]]
  113. )
  114. self.assertEqual(output["image"].shape, (640, 640, 3))
  115. np.testing.assert_array_equal(output["target"], expected_boxes)
  116. def test_rescale_image(self):
  117. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  118. target_shape = (320, 240)
  119. rescaled_image = _rescale_image(image, target_shape)
  120. # Check if the rescaled image has the correct target shape
  121. self.assertEqual(rescaled_image.shape[:2], target_shape)
  122. def test_detection_horizontal_flip(self):
  123. aug = DetectionHorizontalFlip(prob=1)
  124. image = np.random.rand(100, 100, 3)
  125. image_original = image.copy()
  126. # [x0, y0, x1, y1]
  127. bboxes = np.array(
  128. (
  129. (10, 10, 20, 20),
  130. (90, 90, 100, 100),
  131. )
  132. )
  133. bboxes_expected = np.array(
  134. (
  135. (80, 10, 90, 20),
  136. (0, 90, 10, 100),
  137. )
  138. )
  139. # run transform
  140. sample = {"image": image}
  141. sample["target"] = bboxes
  142. sample["crowd_targets"] = bboxes.copy()
  143. output = aug(sample)
  144. image = output["image"]
  145. target = output["target"]
  146. crowd_targets = output["crowd_targets"]
  147. # check image hasn't changed shape
  148. self.assertEqual(image.shape, image_original.shape)
  149. # check the first two cols of original image
  150. # match last two rows of flipped image
  151. self.assertTrue(np.array_equal(image_original[:, 0], image[:, -1]))
  152. self.assertTrue(np.array_equal(image_original[:, 1], image[:, -2]))
  153. # check bboxes as expected
  154. self.assertTrue(np.array_equal(target, bboxes_expected))
  155. self.assertTrue(np.array_equal(crowd_targets, bboxes_expected))
  156. def test_detection_vertical_flip(self):
  157. aug = DetectionVerticalFlip(prob=1)
  158. image = np.random.rand(100, 100, 3)
  159. image_original = image.copy()
  160. # [x0, y0, x1, y1]
  161. bboxes = np.array(
  162. (
  163. (10, 10, 20, 20),
  164. (90, 90, 100, 100),
  165. )
  166. )
  167. bboxes_expected = np.array(
  168. (
  169. (10, 80, 20, 90),
  170. (90, 0, 100, 10),
  171. )
  172. )
  173. # run transform
  174. sample = {"image": image}
  175. sample["target"] = bboxes
  176. sample["crowd_targets"] = bboxes.copy()
  177. output = aug(sample)
  178. image = output["image"]
  179. target = output["target"]
  180. crowd_targets = output["crowd_targets"]
  181. # check image hasn't changed shape
  182. self.assertEqual(image.shape, image_original.shape)
  183. # check top two rows of original image
  184. # matches bottom rows of flipped image
  185. self.assertTrue(np.array_equal(image_original[0], image[-1]))
  186. self.assertTrue(np.array_equal(image_original[1], image[-2]))
  187. # check bboxes as expected
  188. self.assertTrue(np.array_equal(target, bboxes_expected))
  189. self.assertTrue(np.array_equal(crowd_targets, bboxes_expected))
  190. def test_rescale_bboxes(self):
  191. sy, sx = (2.0, 0.5)
  192. # Empty bboxes
  193. bboxes = np.zeros((0, 4))
  194. expected_bboxes = np.zeros((0, 4))
  195. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  196. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  197. # Not empty bboxes
  198. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  199. 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)
  200. rescaled_bboxes = _rescale_bboxes(targets=bboxes, scale_factors=(sy, sx))
  201. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  202. def test_pad_image(self):
  203. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  204. padding_coordinates = PaddingCoordinates(top=80, bottom=80, left=60, right=60)
  205. pad_value = 0
  206. shifted_image = _pad_image(image, padding_coordinates, pad_value)
  207. # Check if the shifted image has the correct shape
  208. self.assertEqual(shifted_image.shape, (800, 600, 3))
  209. # Check if the padding values are correct
  210. self.assertTrue((shifted_image[: padding_coordinates.top, :, :] == pad_value).all())
  211. self.assertTrue((shifted_image[-padding_coordinates.bottom :, :, :] == pad_value).all())
  212. self.assertTrue((shifted_image[:, : padding_coordinates.left, :] == pad_value).all())
  213. self.assertTrue((shifted_image[:, -padding_coordinates.right :, :] == pad_value).all())
  214. def test_shift_bboxes(self):
  215. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  216. shift_w, shift_h = 60, 80
  217. shifted_bboxes = _shift_bboxes(bboxes, shift_w, shift_h)
  218. # Check if the shifted bboxes have the correct values
  219. expected_bboxes = np.array([[70, 100, 110, 140, 1], [90, 120, 140, 170, 2]], dtype=np.float32)
  220. np.testing.assert_array_equal(shifted_bboxes, expected_bboxes)
  221. def test_rescale_xyxy_bboxes(self):
  222. bboxes = np.array([[10, 20, 50, 60, 1], [30, 40, 80, 90, 2]], dtype=np.float32)
  223. r = 0.5
  224. rescaled_bboxes = _rescale_xyxy_bboxes(bboxes, r)
  225. # Check if the rescaled bboxes have the correct values
  226. 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)
  227. np.testing.assert_array_equal(rescaled_bboxes, expected_bboxes)
  228. def test_padding(self):
  229. # Test Case 1: Padding needed
  230. image = np.array([[1, 2], [3, 4]])
  231. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  232. expected_padded_image = np.array([[1, 2, 114, 114], [3, 4, 114, 114], [114, 114, 114, 114]])
  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 2: No padding needed
  236. image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  237. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=0, right=0)
  238. expected_padded_image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  239. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=114)
  240. np.testing.assert_array_equal(padded_image, expected_padded_image)
  241. # Test Case 3: Image with channel dimension
  242. image = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
  243. padding_coordinates = PaddingCoordinates(top=0, left=0, bottom=1, right=2)
  244. expected_padded_image = np.array(
  245. [
  246. [[1, 2, 3], [4, 5, 6], [0, 0, 0], [0, 0, 0]],
  247. [[7, 8, 9], [10, 11, 12], [0, 0, 0], [0, 0, 0]],
  248. [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
  249. ],
  250. )
  251. padded_image = _pad_image(image=image, padding_coordinates=padding_coordinates, pad_value=0)
  252. np.testing.assert_array_equal(padded_image, expected_padded_image)
  253. def test_get_padding_coordinates(self):
  254. # Test Case 1: Width padding required
  255. image = np.zeros((640, 480))
  256. output_size = (640, 640)
  257. expected_center_padding = PaddingCoordinates(top=0, bottom=0, left=80, right=80)
  258. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=0, left=0, right=160)
  259. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  260. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  261. self.assertEqual(center_padding_coordinates, expected_center_padding)
  262. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  263. # Test Case 2: Height padding required
  264. image = np.zeros((480, 640))
  265. output_size = (640, 640)
  266. expected_center_padding = PaddingCoordinates(top=80, bottom=80, left=0, right=0)
  267. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=160, left=0, right=0)
  268. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  269. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  270. self.assertEqual(center_padding_coordinates, expected_center_padding)
  271. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  272. # Test Case 3: Width and Height padding required
  273. image = np.zeros((480, 640))
  274. output_size = (800, 800)
  275. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  276. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  277. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  278. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  279. self.assertEqual(center_padding_coordinates, expected_center_padding)
  280. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  281. # Test Case 4: Image shape is bigger than output shape
  282. image = np.zeros((800, 800))
  283. output_size = (640, 640)
  284. expected_center_padding = PaddingCoordinates(top=-80, bottom=-80, left=-80, right=-80)
  285. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=-160, left=0, right=-160)
  286. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  287. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  288. self.assertEqual(center_padding_coordinates, expected_center_padding)
  289. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  290. # Test Case 5: Width and Height padding required with an image of 3 channels
  291. image = np.zeros((480, 640, 3))
  292. output_size = (800, 800)
  293. expected_center_padding = PaddingCoordinates(top=160, bottom=160, left=80, right=80)
  294. expected_bottom_right_padding = PaddingCoordinates(top=0, bottom=320, left=0, right=160)
  295. center_padding_coordinates = _get_center_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  296. bottom_right_padding_coordinates = _get_bottom_right_padding_coordinates(input_shape=image.shape, output_shape=output_size)
  297. self.assertEqual(center_padding_coordinates, expected_center_padding)
  298. self.assertEqual(bottom_right_padding_coordinates, expected_bottom_right_padding)
  299. def test_rescale_and_pad_to_size(self):
  300. image = np.random.randint(0, 256, size=(640, 480, 3), dtype=np.uint8)
  301. output_size = (800, 500)
  302. pad_val = 114
  303. rescaled_padded_image, r = _rescale_and_pad_to_size(image, output_size, pad_val=pad_val)
  304. # Check if the rescaled and padded image has the correct shape
  305. self.assertEqual(rescaled_padded_image.shape, (3, *output_size))
  306. # Check if the image is rescaled with the correct ratio
  307. resized_image_shape = (int(image.shape[0] * r), int(image.shape[1] * r))
  308. # Check if the padding is correctly applied
  309. padded_area = rescaled_padded_image[:, resized_image_shape[0] :, :] # Right padding area
  310. self.assertTrue((padded_area == pad_val).all())
  311. padded_area = rescaled_padded_image[:, :, resized_image_shape[1] :] # Bottom padding area
  312. self.assertTrue((padded_area == pad_val).all())
  313. def test_keypoints_brightness_contrast(self):
  314. image = np.random.randint(0, 255, (640, 480, 3), dtype=np.uint8)
  315. image = cv2.boxFilter(image, -1, (13, 13))
  316. plt.figure()
  317. plt.imshow(image)
  318. plt.title("Original image")
  319. plt.show()
  320. aug = KeypointsBrightnessContrast(brightness_range=(1.1, 1.5), contrast_range=(1, 1), prob=1)
  321. sample = aug.apply_to_sample(
  322. PoseEstimationSample(image=image, joints=None, bboxes_xywh=None, areas=None, mask=None, is_crowd=None, additional_samples=None)
  323. )
  324. plt.figure()
  325. plt.imshow(sample.image)
  326. plt.title("Augmented image")
  327. plt.show()
  328. def test_keypoints_mixup(self):
  329. sample1 = PoseEstimationSample(
  330. image=np.zeros((256, 256, 3), dtype=np.uint8) + np.array([55, 0, 0], dtype=np.uint8),
  331. mask=np.zeros((256, 256), dtype=np.uint8),
  332. joints=np.random.randint(0, 256, size=(2, 17, 3)),
  333. is_crowd=np.zeros((2,), dtype=bool),
  334. areas=None,
  335. bboxes_xywh=np.random.randint(32, 64, size=(2, 4)),
  336. additional_samples=None,
  337. )
  338. sample2 = PoseEstimationSample(
  339. image=np.zeros((256, 256, 3), dtype=np.uint8) + np.array([55, 0, 0], dtype=np.uint8),
  340. mask=np.zeros((256, 256), dtype=np.uint8),
  341. joints=np.random.randint(0, 256, size=(3, 17, 3)),
  342. is_crowd=np.zeros((3,), dtype=bool),
  343. areas=None,
  344. bboxes_xywh=np.random.randint(32, 64, size=(3, 4)),
  345. additional_samples=None,
  346. )
  347. compose = KeypointsCompose([KeypointsMixup(prob=1)], load_sample_fn=lambda: sample2)
  348. sample = compose.apply_to_sample(sample1)
  349. self.assertEqual(sample.image.shape, (256, 256, 3))
  350. self.assertEqual(len(sample.joints), len(sample1.joints) + len(sample2.joints))
  351. def test_keypoints_mosaic(self):
  352. sample1 = PoseEstimationSample(
  353. image=np.zeros((128, 128, 3), dtype=np.uint8) + 255,
  354. mask=np.zeros((128, 128), dtype=np.uint8),
  355. joints=np.random.randint(0, 128, size=(1, 17, 3)),
  356. is_crowd=np.zeros((1,), dtype=bool),
  357. areas=None,
  358. bboxes_xywh=np.random.randint(0, 64, size=(2, 4)),
  359. additional_samples=None,
  360. )
  361. sample2 = PoseEstimationSample(
  362. image=np.zeros((256, 256, 3), dtype=np.uint8) + np.array([55, 0, 0], dtype=np.uint8),
  363. mask=np.zeros((256, 256), dtype=np.uint8),
  364. joints=np.random.randint(0, 256, size=(1, 17, 3)),
  365. is_crowd=np.zeros((1,), dtype=bool),
  366. areas=None,
  367. bboxes_xywh=np.random.randint(32, 64, size=(2, 4)),
  368. additional_samples=None,
  369. )
  370. sample3 = PoseEstimationSample(
  371. image=np.zeros((512, 512, 3), dtype=np.uint8) + np.array([0, 55, 0], dtype=np.uint8),
  372. mask=np.zeros((512, 512), dtype=np.uint8),
  373. joints=np.random.randint(0, 512, size=(1, 17, 3)),
  374. is_crowd=np.zeros((1,), dtype=bool),
  375. areas=None,
  376. bboxes_xywh=np.random.randint(128, 256, size=(2, 4)),
  377. additional_samples=None,
  378. )
  379. sample4 = PoseEstimationSample(
  380. image=np.zeros((64, 64, 3), dtype=np.uint8) + np.array([0, 0, 55], dtype=np.uint8),
  381. mask=np.zeros((64, 64), dtype=np.uint8),
  382. joints=np.random.randint(0, 64, size=(1, 17, 3)),
  383. is_crowd=np.zeros((1,), dtype=bool),
  384. areas=None,
  385. bboxes_xywh=np.random.randint(0, 32, size=(2, 4)),
  386. additional_samples=None,
  387. )
  388. input_mixup = copy.deepcopy(sample4)
  389. input_mixup.additional_samples = [sample1, sample2, sample3]
  390. self.show_sample(sample1)
  391. self.show_sample(sample2)
  392. self.show_sample(sample3)
  393. self.show_sample(sample4)
  394. aug = KeypointsMosaic(prob=1)
  395. sample = aug.apply_to_sample(input_mixup)
  396. self.show_sample(sample)
  397. def show_sample(self, sample: PoseEstimationSample):
  398. image = sample.image.copy()
  399. poses = sample.joints
  400. for joints in poses:
  401. for joint in joints:
  402. cv2.circle(image, (int(joint[0]), int(joint[1])), 3, (0, 0, 255), -1)
  403. for (x, y, w, h) in sample.bboxes_xywh:
  404. x = int(x)
  405. y = int(y)
  406. w = int(w)
  407. h = int(h)
  408. cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
  409. plt.figure()
  410. plt.imshow(image)
  411. plt.show()
  412. if __name__ == "__main__":
  413. unittest.main()
Tip!

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

Comments

Loading...