Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#578 Feature/sg 516 support head replacement for local pretrained weights unknown dataset

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-516_support_head_replacement_for_local_pretrained_weights_unknown_dataset
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
  1. import os.path
  2. import tempfile
  3. import unittest
  4. import numpy as np
  5. import onnx
  6. import onnxruntime as ort
  7. import torch.jit
  8. from super_gradients.training.utils.bbox_formats import NormalizedXYWHCoordinateFormat, CXCYWHCoordinateFormat, YXYXCoordinateFormat
  9. from super_gradients.training.utils.output_adapters import DetectionOutputAdapter, ConcatenatedTensorFormat, BoundingBoxesTensorSliceItem, TensorSliceItem
  10. NORMALIZED_XYWH_SCORES_LABELS = ConcatenatedTensorFormat(
  11. layout=(
  12. BoundingBoxesTensorSliceItem(name="bboxes", format=NormalizedXYWHCoordinateFormat()),
  13. TensorSliceItem(length=1, name="scores"),
  14. TensorSliceItem(length=1, name="labels"),
  15. )
  16. )
  17. CXCYWH_SCORES_LABELS = ConcatenatedTensorFormat(
  18. layout=(
  19. BoundingBoxesTensorSliceItem(name="bboxes", format=CXCYWHCoordinateFormat()),
  20. TensorSliceItem(length=1, name="scores"),
  21. TensorSliceItem(length=1, name="labels"),
  22. )
  23. )
  24. CXCYWH_LABELS_SCORES_DISTANCE_ATTR = ConcatenatedTensorFormat(
  25. layout=(
  26. BoundingBoxesTensorSliceItem(name="bboxes", format=CXCYWHCoordinateFormat()),
  27. TensorSliceItem(length=1, name="labels"),
  28. TensorSliceItem(length=1, name="scores"),
  29. TensorSliceItem(length=1, name="distance"),
  30. TensorSliceItem(length=4, name="attributes"),
  31. )
  32. )
  33. ATTR_YXYX = ConcatenatedTensorFormat(
  34. layout=(
  35. TensorSliceItem(length=4, name="attributes"),
  36. BoundingBoxesTensorSliceItem(name="bboxes", format=YXYXCoordinateFormat()),
  37. )
  38. )
  39. class TestDetectionOutputAdapter(unittest.TestCase):
  40. @torch.no_grad()
  41. def test_select_only_some_outputs(self):
  42. adapter = DetectionOutputAdapter(CXCYWH_LABELS_SCORES_DISTANCE_ATTR, ATTR_YXYX, image_shape=(640, 640)).eval()
  43. example_inputs = (
  44. torch.randn((300, CXCYWH_LABELS_SCORES_DISTANCE_ATTR.num_channels)),
  45. torch.randn((4, 300, CXCYWH_LABELS_SCORES_DISTANCE_ATTR.num_channels)),
  46. )
  47. for expected_input in example_inputs:
  48. intermediate = adapter(expected_input)
  49. self.assertEqual(ATTR_YXYX.num_channels, intermediate.size(-1))
  50. @torch.no_grad()
  51. def test_output_adapter_convert_vice_versa(self):
  52. adapter = DetectionOutputAdapter(NORMALIZED_XYWH_SCORES_LABELS, CXCYWH_SCORES_LABELS, image_shape=(640, 640)).eval()
  53. adapter_back = DetectionOutputAdapter(CXCYWH_SCORES_LABELS, NORMALIZED_XYWH_SCORES_LABELS, image_shape=(640, 640)).eval()
  54. example_inputs = (
  55. torch.randn((300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  56. torch.randn((4, 300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  57. )
  58. for expected_input in example_inputs:
  59. intermediate = adapter(expected_input)
  60. output_actual = adapter_back(intermediate)
  61. self.assertTrue(torch.allclose(expected_input, output_actual, atol=1e-4))
  62. @torch.no_grad()
  63. def test_output_adapter_can_be_traced(self):
  64. adapter = DetectionOutputAdapter(NORMALIZED_XYWH_SCORES_LABELS, CXCYWH_SCORES_LABELS, image_shape=(640, 640)).eval()
  65. example_inputs = (
  66. torch.randn((300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  67. torch.randn((4, 300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  68. )
  69. for inp in example_inputs:
  70. traced_adapter = torch.jit.trace(adapter, example_inputs=inp, strict=True)
  71. output_expected = adapter(inp)
  72. output_actual = traced_adapter(inp)
  73. self.assertTrue(output_expected.eq(output_actual).all())
  74. @torch.no_grad()
  75. def test_output_adapter_can_be_scripted(self):
  76. adapter = DetectionOutputAdapter(NORMALIZED_XYWH_SCORES_LABELS, CXCYWH_SCORES_LABELS, image_shape=(640, 640)).eval()
  77. example_inputs = (
  78. torch.randn((300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  79. torch.randn((4, 300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  80. )
  81. for inp in example_inputs:
  82. scripted_adapter = torch.jit.script(adapter, example_inputs=[inp])
  83. output_expected = adapter(inp)
  84. output_actual = scripted_adapter(inp)
  85. self.assertTrue(output_expected.eq(output_actual).all())
  86. @torch.no_grad()
  87. def test_output_adapter_can_be_onnx_exported(self):
  88. adapter = DetectionOutputAdapter(NORMALIZED_XYWH_SCORES_LABELS, CXCYWH_SCORES_LABELS, image_shape=(640, 640)).eval()
  89. example_inputs = (
  90. torch.randn((300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  91. torch.randn((4, 300, NORMALIZED_XYWH_SCORES_LABELS.num_channels)),
  92. )
  93. for inp in example_inputs:
  94. expected_output = adapter(inp).numpy()
  95. with tempfile.TemporaryDirectory() as tmpdirname:
  96. adapter_fname = os.path.join(tmpdirname, "adapter.onnx")
  97. torch.onnx.export(adapter, inp, f=adapter_fname, input_names=["predictions"], output_names=["output_predictions"])
  98. onnx_model = onnx.load(adapter_fname)
  99. onnx.checker.check_model(onnx_model)
  100. ort_sess = ort.InferenceSession(adapter_fname)
  101. actual_output = ort_sess.run(None, {"predictions": inp.numpy()})[0]
  102. np.testing.assert_allclose(actual_output, expected_output)
  103. if __name__ == "__main__":
  104. unittest.main()
Discard
Tip!

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