Skip to content

Commit

Permalink
Add onnx to the resulting ModelEntity in export() (#1976)
Browse files Browse the repository at this point in the history
* Fix fp16 onnx export for detection

Add dumping onnx model to model entity

Fix action tests

Fix segmentaion tests

* Update changelog
  • Loading branch information
sovrasov authored Apr 12, 2023
1 parent 055c809 commit cacc35a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.

- Add generating feature cli_report.log in output for otx training (<https://github.com/openvinotoolkit/training_extensions/pull/1959>)
- Support multiple python versions up to 3.10 (<https://github.com/openvinotoolkit/training_extensions/pull/1978>)
- Support export of onnx models (<https://github.com/openvinotoolkit/training_extensions/pull/1976>)

### Enhancements

Expand Down
3 changes: 3 additions & 0 deletions otx/algorithms/action/tasks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,13 @@ def export(
exporter.export()
bin_file = [f for f in os.listdir(self._output_path) if f.endswith(".bin")][0]
xml_file = [f for f in os.listdir(self._output_path) if f.endswith(".xml")][0]
onnx_file = [f for f in os.listdir(self._output_path) if f.endswith(".onnx")][0]
with open(os.path.join(self._output_path, bin_file), "rb") as f:
output_model.set_data("openvino.bin", f.read())
with open(os.path.join(self._output_path, xml_file), "rb") as f:
output_model.set_data("openvino.xml", f.read())
with open(os.path.join(self._output_path, onnx_file), "rb") as file:
output_model.set_data("model.onnx", file.read())
output_model.set_data(
"confidence_threshold", np.array([self.confidence_threshold], dtype=np.float32).tobytes()
)
Expand Down
2 changes: 2 additions & 0 deletions otx/algorithms/anomaly/tasks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ def export(
output_model.set_data("openvino.bin", file.read())
with open(xml_file, "rb") as file:
output_model.set_data("openvino.xml", file.read())
with open(onnx_path, "rb") as file:
output_model.set_data("model.onnx", file.read())

output_model.precision = self.precision
output_model.optimization_methods = self.optimization_methods
Expand Down
7 changes: 5 additions & 2 deletions otx/algorithms/classification/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,19 +250,22 @@ def export(

bin_file = outputs.get("bin")
xml_file = outputs.get("xml")
onnx_file = outputs.get("onnx")

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

if xml_file is None or bin_file is None:
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
if xml_file is None or bin_file is None or onnx_file is None:
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
with open(bin_file, "rb") as f:
output_model.set_data("openvino.bin", f.read())
with open(xml_file, "rb") as f:
output_model.set_data("openvino.xml", f.read())
with open(onnx_file, "rb") as f:
output_model.set_data("model.onnx", f.read())
output_model.precision = self._precision
output_model.has_xai = dump_features
output_model.set_data(
Expand Down
1 change: 1 addition & 0 deletions otx/algorithms/common/adapters/mmcv/tasks/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def run(self, cfg, **kwargs): # noqa: C901
"outputs": {
"bin": os.path.join(cfg.work_dir, f"{model_name}.bin"),
"xml": os.path.join(cfg.work_dir, f"{model_name}.xml"),
"onnx": os.path.join(cfg.work_dir, f"{model_name}.onnx"),
"partitioned": [
{
"bin": os.path.join(cfg.work_dir, name.replace(".xml", ".bin")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def run(self, model_cfg, model_ckpt, data_cfg, **kwargs): # noqa: C901
"outputs": {
"bin": os.path.join(cfg.work_dir, f"{model_name}.bin"),
"xml": os.path.join(cfg.work_dir, f"{model_name}.xml"),
"onnx": os.path.join(cfg.work_dir, f"{model_name}.onnx"),
"partitioned": [
{
"bin": os.path.join(cfg.work_dir, name.replace(".xml", ".bin")),
Expand Down
7 changes: 5 additions & 2 deletions otx/algorithms/detection/adapters/mmdet/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,18 +513,21 @@ def export(

bin_file = outputs.get("bin")
xml_file = outputs.get("xml")
onnx_file = outputs.get("onnx")

ir_extra_data = get_det_model_api_configuration(
self._task_environment.label_schema, self._task_type, self.confidence_threshold
)
embed_ir_model_data(xml_file, ir_extra_data)

if xml_file is None or bin_file is None:
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
if xml_file is None or bin_file is None or onnx_file is None:
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
with open(bin_file, "rb") as f:
output_model.set_data("openvino.bin", f.read())
with open(xml_file, "rb") as f:
output_model.set_data("openvino.xml", f.read())
with open(onnx_file, "rb") as f:
output_model.set_data("model.onnx", f.read())
output_model.set_data(
"confidence_threshold",
np.array([self.confidence_threshold], dtype=np.float32).tobytes(),
Expand Down
7 changes: 5 additions & 2 deletions otx/algorithms/segmentation/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,16 @@ def export(

bin_file = outputs.get("bin")
xml_file = outputs.get("xml")
onnx_file = outputs.get("onnx")

if xml_file is None or bin_file is None:
raise RuntimeError("invalid status of exporting. bin and xml should not be None")
if xml_file is None or bin_file is None or onnx_file is None:
raise RuntimeError("invalid status of exporting. bin and xml or onnx should not be None")
with open(bin_file, "rb") as f:
output_model.set_data("openvino.bin", f.read())
with open(xml_file, "rb") as f:
output_model.set_data("openvino.xml", f.read())
with open(onnx_file, "rb") as f:
output_model.set_data("model.onnx", f.read())
output_model.precision = self._precision
output_model.optimization_methods = self._optimization_methods
output_model.has_xai = dump_features
Expand Down
1 change: 1 addition & 0 deletions tests/test_suite/run_test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def otx_export_testing(template, root, dump_features=False, half_precision=False
path_to_xml = os.path.join(save_path, "openvino.xml")
assert os.path.exists(path_to_xml)
assert os.path.exists(os.path.join(save_path, "openvino.bin"))
assert os.path.exists(os.path.join(save_path, "model.onnx"))
assert os.path.exists(os.path.join(save_path, "label_schema.json"))

if dump_features:
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/algorithms/action/tasks/test_action_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def export(self):
f.write(dummy_data)
with open(self.work_dir + ".xml", "wb") as f:
f.write(dummy_data)
with open(self.work_dir + ".onnx", "wb") as f:
f.write(dummy_data)


class TestActionInferenceTask:
Expand Down Expand Up @@ -298,6 +300,7 @@ def test_export(self, mocker, precision: ModelPrecision, dump_features: bool) ->
assert _model.precision[0] == precision
assert _model.get_data("openvino.bin") is not None
assert _model.get_data("openvino.xml") is not None
assert _model.get_data("model.onnx") is not None
assert _model.get_data("confidence_threshold") is not None
assert _model.precision == self.cls_task._precision
assert _model.optimization_methods == self.cls_task._optimization_methods
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/algorithms/segmentation/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _explain_model(*args, **kwargs):
pass

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


class MockModel:
Expand Down

0 comments on commit cacc35a

Please sign in to comment.