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 5.8 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
  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:], 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:], 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:], 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(input_dim=self.image.shape[1:], input_format=LABEL_XYXY, output_format=LABEL_NORMALIZED_CXCYWH)
  43. t_output = transform(sample)["target"]
  44. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  45. def test_normalized_xyxy_2_cxcywh(self):
  46. _, h, w = self.image.shape
  47. input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  48. output = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
  49. sample = {"image": self.image, "target": input}
  50. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], input_format=LABEL_NORMALIZED_XYXY, output_format=LABEL_CXCYWH)
  51. t_output = transform(sample)["target"]
  52. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  53. def test_normalized_xyxy_2_normalized_cxcywh(self):
  54. _, h, w = self.image.shape
  55. input = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  56. output = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
  57. sample = {"image": self.image, "target": input}
  58. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], input_format=LABEL_NORMALIZED_XYXY, output_format=LABEL_NORMALIZED_CXCYWH)
  59. t_output = transform(sample)["target"]
  60. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  61. def test_cxcywh_2_xyxy(self):
  62. output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  63. input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
  64. sample = {"image": self.image, "target": input}
  65. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], input_format=LABEL_CXCYWH, output_format=LABEL_XYXY)
  66. t_output = transform(sample)["target"]
  67. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  68. def test_cxcywh_2_normalized_xyxy(self):
  69. _, h, w = self.image.shape
  70. output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  71. input = np.array([[10, 30, 40, 20, 20]], dtype=np.float32)
  72. sample = {"image": self.image, "target": input}
  73. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], input_format=LABEL_CXCYWH, output_format=LABEL_NORMALIZED_XYXY)
  74. t_output = transform(sample)["target"]
  75. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  76. def test_normalized_cxcywh_2_xyxy(self):
  77. _, h, w = self.image.shape
  78. input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
  79. output = np.array([[10, 20, 30, 40, 50]], dtype=np.float32)
  80. sample = {"image": self.image, "target": input}
  81. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], input_format=LABEL_NORMALIZED_CXCYWH, output_format=LABEL_XYXY)
  82. t_output = transform(sample)["target"]
  83. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  84. def test_normalized_cxcywh_2_normalized_xyxy(self):
  85. _, h, w = self.image.shape
  86. output = np.array([[10, 20 / w, 30 / h, 40 / w, 50 / h]], dtype=np.float32)
  87. input = np.array([[10, 30 / w, 40 / h, 20 / w, 20 / h]], dtype=np.float32)
  88. sample = {"image": self.image, "target": input}
  89. transform = DetectionTargetsFormatTransform(input_dim=self.image.shape[1:], input_format=LABEL_NORMALIZED_CXCYWH, output_format=LABEL_NORMALIZED_XYXY)
  90. t_output = transform(sample)["target"]
  91. self.assertTrue(np.allclose(output, t_output, atol=1e-6))
  92. if __name__ == "__main__":
  93. unittest.main()
Tip!

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

Comments

Loading...