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_targets_format_transform_test.py 6.0 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
  1. import numpy as np
  2. import unittest
  3. from super_gradients.training.transforms.transforms import DetectionTargetsFormatTransform
  4. from super_gradients.training.datasets.data_formats.default_formats import (
  5. XYXY_LABEL,
  6. LABEL_XYXY,
  7. LABEL_CXCYWH,
  8. LABEL_NORMALIZED_XYXY,
  9. LABEL_NORMALIZED_CXCYWH,
  10. )
  11. class DetectionTargetsTransformTest(unittest.TestCase):
  12. def setUp(self) -> None:
  13. self.image = np.zeros((3, 100, 200))
  14. def test_label_first_2_label_last(self):
  15. input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  16. output = np.array([[50, 10, 20, 30, 40]], dtype=np.float32)
  17. sample = {"image": self.image, "target": input}
  18. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], max_targets=1, input_format=XYXY_LABEL, output_format=LABEL_XYXY)
  19. t_output = transform(sample)["target"]
  20. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  21. def test_xyxy_2_normalized_xyxy(self):
  22. input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  23. _, h, w = self.image.shape
  24. output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  25. sample = {"image": self.image, "target": input}
  26. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_XYXY, output_format=LABEL_NORMALIZED_XYXY)
  27. t_output = transform(sample)["target"]
  28. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  29. def test_xyxy_2_cxcywh(self):
  30. input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  31. _, h, w = self.image.shape
  32. output = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
  33. sample = {"image": self.image, "target": input}
  34. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_XYXY, output_format=LABEL_CXCYWH)
  35. t_output = transform(sample)["target"]
  36. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  37. def test_xyxy_2_normalized_cxcywh(self):
  38. input = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  39. _, h, w = self.image.shape
  40. output = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
  41. sample = {"image": self.image, "target": input}
  42. transform = DetectionTargetsFormatTransform(
  43. input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_XYXY, output_format=LABEL_NORMALIZED_CXCYWH
  44. )
  45. t_output = transform(sample)["target"]
  46. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  47. def test_normalized_xyxy_2_cxcywh(self):
  48. _, h, w = self.image.shape
  49. input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  50. output = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
  51. sample = {"image": self.image, "target": input}
  52. transform = DetectionTargetsFormatTransform(
  53. input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_XYXY, output_format=LABEL_CXCYWH
  54. )
  55. t_output = transform(sample)["target"]
  56. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  57. def test_normalized_xyxy_2_normalized_cxcywh(self):
  58. _, h, w = self.image.shape
  59. input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  60. output = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
  61. sample = {"image": self.image, "target": input}
  62. transform = DetectionTargetsFormatTransform(
  63. input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_XYXY, output_format=LABEL_NORMALIZED_CXCYWH
  64. )
  65. t_output = transform(sample)["target"]
  66. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  67. def test_cxcywh_2_xyxy(self):
  68. output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  69. input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
  70. sample = {"image": self.image, "target": input}
  71. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_CXCYWH, output_format=LABEL_XYXY)
  72. t_output = transform(sample)["target"]
  73. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  74. def test_cxcywh_2_normalized_xyxy(self):
  75. _, h, w = self.image.shape
  76. output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  77. input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
  78. sample = {"image": self.image, "target": input}
  79. transform = DetectionTargetsFormatTransform(
  80. input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_CXCYWH, output_format=LABEL_NORMALIZED_XYXY
  81. )
  82. t_output = transform(sample)["target"]
  83. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  84. def test_normalized_cxcywh_2_xyxy(self):
  85. _, h, w = self.image.shape
  86. input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
  87. output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  88. sample = {"image": self.image, "target": input}
  89. transform = DetectionTargetsFormatTransform(
  90. input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_CXCYWH, output_format=LABEL_XYXY
  91. )
  92. t_output = transform(sample)["target"]
  93. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  94. def test_normalized_cxcywh_2_normalized_xyxy(self):
  95. _, h, w = self.image.shape
  96. output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  97. input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
  98. sample = {"image": self.image, "target": input}
  99. transform = DetectionTargetsFormatTransform(
  100. input_dim=self.image.shape[1:], max_targets=1, input_format=LABEL_NORMALIZED_CXCYWH, output_format=LABEL_NORMALIZED_XYXY
  101. )
  102. t_output = transform(sample)["target"]
  103. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  104. if __name__ == "__main__":
  105. unittest.main()
Tip!

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

Comments

Loading...