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

export_optical_flow_model_test.py 6.5 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
  1. import logging
  2. import os
  3. import tempfile
  4. import unittest
  5. import numpy as np
  6. import onnxruntime
  7. import torch
  8. from super_gradients.common.object_names import Models
  9. from super_gradients.conversion.gs_utils import import_onnx_graphsurgeon_or_install
  10. from super_gradients.import_utils import import_pytorch_quantization_or_install
  11. from super_gradients.module_interfaces import ExportableOpticalFlowModel, OpticalFlowModelExportResult
  12. from super_gradients.training import models
  13. gs = import_onnx_graphsurgeon_or_install()
  14. import_pytorch_quantization_or_install()
  15. class TestOpticalFlowModelExport(unittest.TestCase):
  16. def setUp(self) -> None:
  17. logging.getLogger().setLevel(logging.DEBUG)
  18. self.models_to_test = [
  19. Models.RAFT_S,
  20. Models.RAFT_L,
  21. ]
  22. # def test_infer_input_image_shape_from_model(self):
  23. # assert infer_image_shape_from_model(models.get(Models.RAFT_S, num_classes=1)) is None
  24. # assert infer_image_shape_from_model(models.get(Models.RAFT_L, num_classes=1)) is None
  25. # def test_infer_input_image_num_channels_from_model(self):
  26. # assert infer_image_input_channels(models.get(Models.RAFT_S, num_classes=1)) == 3
  27. # assert infer_image_input_channels(models.get(Models.RAFT_L, num_classes=1)) == 3
  28. def test_export_to_onnxruntime_and_run(self):
  29. """
  30. Test export to ONNX
  31. """
  32. with tempfile.TemporaryDirectory() as tmpdirname:
  33. for model_type in self.models_to_test:
  34. with self.subTest(model_type=model_type):
  35. model_name = str(model_type).lower().replace(".", "_")
  36. out_path = os.path.join(tmpdirname, f"{model_name}_onnxruntime.onnx")
  37. model_arch: ExportableOpticalFlowModel = models.get(model_name, num_classes=1)
  38. export_result = model_arch.export(
  39. out_path,
  40. input_image_shape=(640, 640), # Force .export() to infer image shape from the model itself
  41. input_image_channels=3,
  42. input_image_dtype=torch.float32,
  43. )
  44. [flow_prediction] = self._run_inference_with_onnx(export_result)
  45. self.assertTrue(flow_prediction.shape[0] == 1)
  46. self.assertTrue(flow_prediction.shape[1] == 2)
  47. self.assertTrue(flow_prediction.shape[2] == 640)
  48. self.assertTrue(flow_prediction.shape[3] == 640)
  49. # def test_export_int8_quantized_with_calibration(self):
  50. # with tempfile.TemporaryDirectory() as tmpdirname:
  51. # for model_type in self.models_to_test:
  52. # with self.subTest(model_type=model_type):
  53. # model_name = str(model_type).lower().replace(".", "_")
  54. # out_path = os.path.join(tmpdirname, f"{model_name}.onnx")
  55. #
  56. # dummy_calibration_dataset = [torch.randn((3, 640, 640), dtype=torch.float32) for _ in range(32)]
  57. # dummy_calibration_loader = DataLoader(dummy_calibration_dataset, batch_size=8)
  58. #
  59. # model_arch: ExportableOpticalFlowModel = models.get(model_name, num_classes=1)
  60. # export_result = model_arch.export(
  61. # out_path,
  62. # input_image_shape=(640, 640), # Force .export() to infer image shape from the model itself
  63. # quantization_mode=ExportQuantizationMode.INT8,
  64. # calibration_loader=dummy_calibration_loader,
  65. # )
  66. #
  67. # [flow_prediction] = self._run_inference_with_onnx(export_result)
  68. # self.assertTrue(flow_prediction.shape[0] == 1)
  69. # self.assertTrue(flow_prediction.shape[1] == 2)
  70. # self.assertTrue(flow_prediction.shape[2] == 640)
  71. # self.assertTrue(flow_prediction.shape[3] == 640)
  72. def _run_inference_with_onnx(self, export_result: OpticalFlowModelExportResult):
  73. # onnx_filename = out_path, input_shape = export_result.image_shape, output_predictions_format = output_predictions_format
  74. input = np.zeros((1, 2, 3, 640, 640)).astype(np.float32)
  75. session = onnxruntime.InferenceSession(export_result.output)
  76. inputs = [o.name for o in session.get_inputs()]
  77. outputs = [o.name for o in session.get_outputs()]
  78. result = session.run(outputs, {inputs[0]: input})
  79. return result
  80. # def test_export_already_quantized_model(self):
  81. # from super_gradients.training.utils.quantization import SelectiveQuantizer
  82. #
  83. # for model_type in self.models_to_test:
  84. # with self.subTest(model_type=model_type):
  85. # model = models.get(model_type, num_classes=1)
  86. # q_util = SelectiveQuantizer(
  87. # default_quant_modules_calibrator_weights="max",
  88. # default_quant_modules_calibrator_inputs="histogram",
  89. # default_per_channel_quant_weights=True,
  90. # default_learn_amax=False,
  91. # verbose=True,
  92. # )
  93. # q_util.quantize_module(model)
  94. #
  95. # with tempfile.TemporaryDirectory() as tmpdirname:
  96. # output_model1 = os.path.join(tmpdirname, f"{model_type}_quantized_explicit_int8.onnx")
  97. # output_model2 = os.path.join(tmpdirname, f"{model_type}_quantized.onnx")
  98. #
  99. # # If model is already quantized to int8, the export should be successful but model should not be quantized again
  100. # model.export(
  101. # output_model1,
  102. # quantization_mode=ExportQuantizationMode.INT8,
  103. # )
  104. #
  105. # # If model is quantized but quantization mode is not specified, the export should be also successful
  106. # # but model should not be quantized again
  107. # model.export(
  108. # output_model2,
  109. # quantization_mode=None,
  110. # )
  111. #
  112. # # If model is already quantized to int8, we should not be able to export model to FP16
  113. # with self.assertRaises(RuntimeError):
  114. # model.export(
  115. # "raft_s_quantized.onnx",
  116. # quantization_mode=ExportQuantizationMode.FP16,
  117. # )
  118. if __name__ == "__main__":
  119. unittest.main()
Tip!

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

Comments

Loading...