Skip to content

Commit 1fe00ef

Browse files
committed
Fix fp16 onnx export for detection
Add dumping onnx model to model entity Fix action tests Fix segmentaion tests
1 parent 9410c0d commit 1fe00ef

File tree

10 files changed

+37
-7
lines changed

10 files changed

+37
-7
lines changed

otx/algorithms/action/tasks/inference.py

+3
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,13 @@ def export(
362362
exporter.export()
363363
bin_file = [f for f in os.listdir(self._output_path) if f.endswith(".bin")][0]
364364
xml_file = [f for f in os.listdir(self._output_path) if f.endswith(".xml")][0]
365+
onnx_file = [f for f in os.listdir(self._output_path) if f.endswith(".onnx")][0]
365366
with open(os.path.join(self._output_path, bin_file), "rb") as f:
366367
output_model.set_data("openvino.bin", f.read())
367368
with open(os.path.join(self._output_path, xml_file), "rb") as f:
368369
output_model.set_data("openvino.xml", f.read())
370+
with open(os.path.join(self._output_path, onnx_file), "rb") as file:
371+
output_model.set_data("model.onnx", file.read())
369372
output_model.set_data(
370373
"confidence_threshold", np.array([self.confidence_threshold], dtype=np.float32).tobytes()
371374
)

otx/algorithms/anomaly/tasks/inference.py

+2
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ def export(
292292
output_model.set_data("openvino.bin", file.read())
293293
with open(xml_file, "rb") as file:
294294
output_model.set_data("openvino.xml", file.read())
295+
with open(onnx_path, "rb") as file:
296+
output_model.set_data("model.onnx", file.read())
295297

296298
output_model.precision = self.precision
297299
output_model.optimization_methods = self.optimization_methods

otx/algorithms/classification/task.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,22 @@ def export(
250250

251251
bin_file = outputs.get("bin")
252252
xml_file = outputs.get("xml")
253+
onnx_file = outputs.get("onnx")
253254

254255
inference_config = get_cls_inferencer_configuration(self._task_environment.label_schema)
255256
deploy_cfg = get_cls_deploy_config(self._task_environment.label_schema, inference_config)
256257
ir_extra_data = get_cls_model_api_configuration(self._task_environment.label_schema, inference_config)
257258
ir_extra_data[("otx_config",)] = json.dumps(deploy_cfg, ensure_ascii=False)
258259
embed_ir_model_data(xml_file, ir_extra_data)
259260

260-
if xml_file is None or bin_file is None:
261-
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
261+
if xml_file is None or bin_file is None or onnx_file is None:
262+
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
262263
with open(bin_file, "rb") as f:
263264
output_model.set_data("openvino.bin", f.read())
264265
with open(xml_file, "rb") as f:
265266
output_model.set_data("openvino.xml", f.read())
267+
with open(onnx_file, "rb") as f:
268+
output_model.set_data("model.onnx", f.read())
266269
output_model.precision = self._precision
267270
output_model.has_xai = dump_features
268271
output_model.set_data(

otx/algorithms/common/adapters/mmcv/tasks/exporter.py

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def run(self, cfg, **kwargs): # noqa: C901
6161
"outputs": {
6262
"bin": os.path.join(cfg.work_dir, f"{model_name}.bin"),
6363
"xml": os.path.join(cfg.work_dir, f"{model_name}.xml"),
64+
"onnx": os.path.join(cfg.work_dir, f"{model_name}.onnx"),
6465
"partitioned": [
6566
{
6667
"bin": os.path.join(cfg.work_dir, name.replace(".xml", ".bin")),

otx/algorithms/common/adapters/mmcv/tasks/exporter_mixin.py

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def run(self, model_cfg, model_ckpt, data_cfg, **kwargs): # noqa: C901
7272
"outputs": {
7373
"bin": os.path.join(cfg.work_dir, f"{model_name}.bin"),
7474
"xml": os.path.join(cfg.work_dir, f"{model_name}.xml"),
75+
"onnx": os.path.join(cfg.work_dir, f"{model_name}.onnx"),
7576
"partitioned": [
7677
{
7778
"bin": os.path.join(cfg.work_dir, name.replace(".xml", ".bin")),

otx/algorithms/detection/adapters/mmdet/task.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -513,18 +513,21 @@ def export(
513513

514514
bin_file = outputs.get("bin")
515515
xml_file = outputs.get("xml")
516+
onnx_file = outputs.get("onnx")
516517

517518
ir_extra_data = get_det_model_api_configuration(
518519
self._task_environment.label_schema, self._task_type, self.confidence_threshold
519520
)
520521
embed_ir_model_data(xml_file, ir_extra_data)
521522

522-
if xml_file is None or bin_file is None:
523-
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
523+
if xml_file is None or bin_file is None or onnx_file is None:
524+
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
524525
with open(bin_file, "rb") as f:
525526
output_model.set_data("openvino.bin", f.read())
526527
with open(xml_file, "rb") as f:
527528
output_model.set_data("openvino.xml", f.read())
529+
with open(onnx_file, "rb") as f:
530+
output_model.set_data("model.onnx", f.read())
528531
output_model.set_data(
529532
"confidence_threshold",
530533
np.array([self.confidence_threshold], dtype=np.float32).tobytes(),

otx/algorithms/segmentation/tasks/inference.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,16 @@ def export(
175175

176176
bin_file = outputs.get("bin")
177177
xml_file = outputs.get("xml")
178-
if xml_file is None or bin_file is None:
179-
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
178+
onnx_file = outputs.get("onnx")
179+
180+
if xml_file is None or bin_file is None or onnx_file is None:
181+
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
180182
with open(bin_file, "rb") as f:
181183
output_model.set_data("openvino.bin", f.read())
182184
with open(xml_file, "rb") as f:
183185
output_model.set_data("openvino.xml", f.read())
186+
with open(onnx_file, "rb") as f:
187+
output_model.set_data("model.onnx", f.read())
184188
output_model.precision = self._precision
185189
output_model.optimization_methods = self._optimization_methods
186190
output_model.has_xai = dump_features

tests/test_suite/run_test_command.py

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def otx_export_testing(template, root, dump_features=False, half_precision=False
233233
path_to_xml = os.path.join(save_path, "openvino.xml")
234234
assert os.path.exists(path_to_xml)
235235
assert os.path.exists(os.path.join(save_path, "openvino.bin"))
236+
assert os.path.exists(os.path.join(save_path, "model.onnx"))
236237
assert os.path.exists(os.path.join(save_path, "label_schema.json"))
237238

238239
if dump_features:

tests/unit/algorithms/action/tasks/test_action_inference.py

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ def export(self):
115115
f.write(dummy_data)
116116
with open(self.work_dir + ".xml", "wb") as f:
117117
f.write(dummy_data)
118+
with open(self.work_dir + ".onnx", "wb") as f:
119+
f.write(dummy_data)
118120

119121

120122
class TestActionInferenceTask:
@@ -298,6 +300,7 @@ def test_export(self, mocker, precision: ModelPrecision, dump_features: bool) ->
298300
assert _model.precision[0] == precision
299301
assert _model.get_data("openvino.bin") is not None
300302
assert _model.get_data("openvino.xml") is not None
303+
assert _model.get_data("model.onnx") is not None
301304
assert _model.get_data("confidence_threshold") is not None
302305
assert _model.precision == self.cls_task._precision
303306
assert _model.optimization_methods == self.cls_task._optimization_methods

tests/unit/algorithms/segmentation/tasks/test_segmentation_inference.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,23 @@ def test_export_with_model_files(self, mocker, precision: ModelPrecision, dump_f
102102
f.write(b"foo")
103103
with open(f"{self.output_path}/model.bin", "wb") as f:
104104
f.write(b"bar")
105+
with open(f"{self.output_path}/model.onnx", "wb") as f:
106+
f.write(b"bar")
105107

106-
fake_output = {"outputs": {"bin": f"{self.output_path}/model.xml", "xml": f"{self.output_path}/model.bin"}}
108+
fake_output = {
109+
"outputs": {
110+
"bin": f"{self.output_path}/model.xml",
111+
"xml": f"{self.output_path}/model.bin",
112+
"onnx": f"{self.output_path}/model.onnx",
113+
}
114+
}
107115
mock_run_task = mocker.patch.object(BaseTask, "_run_task", return_value=fake_output)
108116
self.seg_train_task.export(ExportType.OPENVINO, self.model, precision, dump_features)
109117

110118
mock_run_task.assert_called_once()
111119
assert self.model.get_data("openvino.bin")
112120
assert self.model.get_data("openvino.xml")
121+
assert self.model.get_data("model.onnx")
113122
assert self.model.has_xai == dump_features
114123

115124
@e2e_pytest_unit

0 commit comments

Comments
 (0)