Skip to content

Commit 1a57208

Browse files
committed
Add dumping onnx model to model entity
1 parent 685b15d commit 1a57208

File tree

8 files changed

+33
-7
lines changed

8 files changed

+33
-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(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/tasks/inference.py

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

249249
bin_file = outputs.get("bin")
250250
xml_file = outputs.get("xml")
251+
onnx_file = outputs.get("onnx")
251252

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

258-
if xml_file is None or bin_file is None:
259-
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
259+
if xml_file is None or bin_file is None or onnx_file is None:
260+
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
260261
with open(bin_file, "rb") as f:
261262
output_model.set_data("openvino.bin", f.read())
262263
with open(xml_file, "rb") as f:
263264
output_model.set_data("openvino.xml", f.read())
265+
with open(onnx_file, "rb") as f:
266+
output_model.set_data("model.onnx", f.read())
264267
output_model.precision = self._precision
265268
output_model.has_xai = dump_features
266269
output_model.set_data(

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
@@ -512,18 +512,21 @@ def export(
512512

513513
bin_file = outputs.get("bin")
514514
xml_file = outputs.get("xml")
515+
onnx_file = outputs.get("onnx")
515516

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

521-
if xml_file is None or bin_file is None:
522-
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
522+
if xml_file is None or bin_file is None or onnx_file is None:
523+
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
523524
with open(bin_file, "rb") as f:
524525
output_model.set_data("openvino.bin", f.read())
525526
with open(xml_file, "rb") as f:
526527
output_model.set_data("openvino.xml", f.read())
528+
with open(onnx_file, "rb") as f:
529+
output_model.set_data("model.onnx", f.read())
527530
output_model.set_data(
528531
"confidence_threshold",
529532
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/classification/tasks/test_classification_inference.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,25 @@ def test_export_with_model_files(self, mocker, precision: ModelPrecision, dump_f
131131
f.write(b"foo")
132132
with open(f"{self.output_path}/model.bin", "wb") as f:
133133
f.write(b"bar")
134+
with open(f"{self.output_path}/model.onnx", "wb") as f:
135+
f.write(b"foo")
134136

135137
mocker.patch("otx.algorithms.classification.tasks.inference.embed_ir_model_data", return_value=None)
136-
fake_output = {"outputs": {"bin": f"{self.output_path}/model.xml", "xml": f"{self.output_path}/model.bin"}}
138+
fake_output = {
139+
"outputs": {
140+
"bin": f"{self.output_path}/model.xml",
141+
"xml": f"{self.output_path}/model.bin",
142+
"onnx": f"{self.output_path}/model.onnx",
143+
}
144+
}
137145
mock_run_task = mocker.patch.object(BaseTask, "_run_task", return_value=fake_output)
138146
self.cls_inference_task.export(ExportType.OPENVINO, self.model, precision, dump_features)
139147

140148
mock_run_task.assert_called_once()
141149
assert self.model.has_xai == dump_features
142150
assert self.model.get_data("openvino.bin")
143151
assert self.model.get_data("openvino.xml")
152+
assert self.model.get_data("model.onnx")
144153

145154
@e2e_pytest_unit
146155
def test_unload(self, mocker):

0 commit comments

Comments
 (0)