Skip to content

Commit 1cc8bed

Browse files
committed
Fix fp16 onnx export for detection
Add dumping onnx model to model entity Fix action tests Fix segmentaion tests
1 parent 3299c66 commit 1cc8bed

File tree

10 files changed

+27
-7
lines changed

10 files changed

+27
-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/task.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,16 @@ def export(
225225

226226
bin_file = outputs.get("bin")
227227
xml_file = outputs.get("xml")
228+
onnx_file = outputs.get("onnx")
228229

229-
if xml_file is None or bin_file is None:
230-
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
230+
if xml_file is None or bin_file is None or onnx_file is None:
231+
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
231232
with open(bin_file, "rb") as f:
232233
output_model.set_data("openvino.bin", f.read())
233234
with open(xml_file, "rb") as f:
234235
output_model.set_data("openvino.xml", f.read())
236+
with open(onnx_file, "rb") as f:
237+
output_model.set_data("model.onnx", f.read())
235238
output_model.precision = self._precision
236239
output_model.optimization_methods = self._optimization_methods
237240
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/test_task.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _explain_model(*args, **kwargs):
3939
pass
4040

4141
def _export_model(*args, **kwargs):
42-
return {"outputs": {"bin": f"/tmp/model.xml", "xml": f"/tmp/model.bin"}}
42+
return {"outputs": {"bin": f"/tmp/model.xml", "xml": f"/tmp/model.bin", "onnx": f"/tmp/model.onnx"}}
4343

4444

4545
class MockModel:

0 commit comments

Comments
 (0)