Skip to content

Commit

Permalink
Remove example_inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
sungchul2 committed Mar 5, 2024
1 parent 84ed9f4 commit 2d75684
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 73 deletions.
13 changes: 2 additions & 11 deletions src/otx/core/exporter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def export(
base_model_name: str = "exported_model",
export_format: OTXExportFormatType = OTXExportFormatType.OPENVINO,
precision: OTXPrecisionType = OTXPrecisionType.FP32,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Exports input model to the specified deployable format, such as OpenVINO IR or ONNX.
Expand All @@ -78,16 +77,14 @@ def export(
base_model_name (str, optional): exported model name
format (OTXExportFormatType): final format of the exported model
precision (OTXExportPrecisionType, optional): precision of the exported model's weights
example_inputs (dict, optional): manual input arguments for the export function.
If not provided, the exporter will set dummy inputs
Returns:
Path: path to the exported model
"""
if export_format == OTXExportFormatType.OPENVINO:
return self.to_openvino(model, output_dir, base_model_name, precision, example_inputs)
return self.to_openvino(model, output_dir, base_model_name, precision)
if export_format == OTXExportFormatType.ONNX:
return self.to_onnx(model, output_dir, base_model_name, precision, example_inputs=example_inputs)
return self.to_onnx(model, output_dir, base_model_name, precision)
if export_format == OTXExportFormatType.EXPORTABLE_CODE:
return self.to_exportable_code(model, output_dir, base_model_name, precision)

Expand All @@ -101,7 +98,6 @@ def to_openvino(
output_dir: Path,
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Export to OpenVINO Intermediate Representation format.
Expand All @@ -110,8 +106,6 @@ def to_openvino(
output_dir (Path): path to the directory to store export artifacts
base_model_name (str, optional): exported model name
precision (OTXExportPrecisionType, optional): precision of the exported model's weights
example_inputs (dict, optional): manual input arguments for the export function.
If not provided, the exporter will set dummy inputs
Returns:
Path: path to the exported model.
Expand All @@ -125,7 +119,6 @@ def to_onnx(
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
embed_metadata: bool = True,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Abstract method for ONNX export.
Expand All @@ -138,8 +131,6 @@ def to_onnx(
precision (OTXPrecisionType, optional): The precision type for the exported model.
Defaults to OTXPrecisionType.FP32.
embed_metadata (bool, optional): Flag to embed metadata in the exported ONNX model. Defaults to True.
example_inputs (dict, optional): manual input arguments for the export function.
If not provided, the exporter will set dummy inputs
Returns:
Path: The file path where the ONNX model is saved.
Expand Down
15 changes: 2 additions & 13 deletions src/otx/core/exporter/mmdeploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from contextlib import contextmanager
from copy import copy
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Iterator, Literal
from typing import TYPE_CHECKING, Callable, Iterator, Literal

import numpy as np
import onnx
Expand Down Expand Up @@ -87,7 +87,6 @@ def to_openvino(
output_dir: Path,
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Export to OpenVINO Intermediate Representation format.
Expand All @@ -97,19 +96,14 @@ def to_openvino(
base_model_name (str, optional): exported model name
precision (OTXPrecisionType, optional): precision of the exported model's weights
metadata (dict[tuple[str, str], str] | None, optional): metadata to embed to the exported model.
example_inputs (dict, optional): manual input arguments for the export function.
If not provided, the exporter will set dummy inputs.
Returns:
Path: path to the exported model.
"""
if example_inputs is None:
example_inputs = {"input": (openvino.runtime.PartialShape(self.input_size),)}

onnx_path = self._cvt2onnx(model, output_dir, base_model_name)
exported_model = openvino.convert_model(
str(onnx_path),
**example_inputs,
input=(openvino.runtime.PartialShape(self.input_size),),
)
exported_model = self._postprocess_openvino_model(exported_model)

Expand All @@ -127,7 +121,6 @@ def to_onnx(
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
embed_metadata: bool = True,
example_inputs: dict[str, Any] | None = None, # TODO (sungchul): how to apply this argument? # noqa: TD003
) -> Path:
"""Export to ONNX format.
Expand All @@ -137,15 +130,11 @@ def to_onnx(
base_model_name (str, optional): exported model name
precision (OTXPrecisionType, optional): precision of the exported model's weights
metadata (dict[tuple[str, str],str] | None, optional): metadata to embed to the exported model.
example_inputs (dict, optional): manual input arguments for the export function.
If not provided, the exporter will set dummy inputs.
Returns:
Path: path to the exported model.
"""
deploy_cfg = self._prepare_onnx_cfg()
if example_inputs is not None:
deploy_cfg.merge_from_dict(example_inputs)
save_path = self._cvt2onnx(model, output_dir, base_model_name, deploy_cfg)

onnx_model = onnx.load(str(save_path))
Expand Down
35 changes: 10 additions & 25 deletions src/otx/core/exporter/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ def to_openvino(
output_dir: Path,
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Export to OpenVINO Intermediate Representation format.
In this implementation the export is done only via standard OV/ONNX tools.
"""
if self.via_onnx:
if example_inputs is None:
example_inputs = {"args": torch.rand(self.input_size).to(next(model.parameters()).device)}
dummy_tensor = torch.rand(self.input_size).to(next(model.parameters()).device)

if self.via_onnx:
with tempfile.TemporaryDirectory() as tmpdirname:
tmp_dir = Path(tmpdirname)

Expand All @@ -65,25 +63,17 @@ def to_openvino(
base_model_name,
OTXPrecisionType.FP32,
False,
example_inputs,
)

ov_input = (
tuple(openvino.runtime.PartialShape(x.shape) for x in example_inputs["args"])
if isinstance(example_inputs["args"], tuple)
else (openvino.runtime.PartialShape(example_inputs["args"].shape),)
)
exported_model = openvino.convert_model(
tmp_dir / (base_model_name + ".onnx"),
input=ov_input,
input=(openvino.runtime.PartialShape(self.input_size),),
)
else:
if example_inputs is None:
example_inputs = {
"input": (openvino.runtime.PartialShape(self.input_size),),
"example_input": torch.rand(self.input_size).to(next(model.parameters()).device),
}
exported_model = openvino.convert_model(input_model=model, **example_inputs)
exported_model = openvino.convert_model(
model,
example_input=dummy_tensor,
input=(openvino.runtime.PartialShape(self.input_size),),
)
exported_model = self._postprocess_openvino_model(exported_model)

save_path = output_dir / (base_model_name + ".xml")
Expand All @@ -99,7 +89,6 @@ def to_onnx(
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
embed_metadata: bool = True,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Export the given PyTorch model to ONNX format and save it to the specified output directory.
Expand All @@ -110,18 +99,14 @@ def to_onnx(
precision (OTXPrecisionType, optional): The precision type for the exported model.
Defaults to OTXPrecisionType.FP32.
embed_metadata (bool, optional): Whether to embed metadata in the ONNX model. Defaults to True.
example_inputs (dict, optional): Manual arguments for the export function.
If not provided, the exporter will set dummy inputs.
Returns:
Path: The path to the saved ONNX model.
"""
if example_inputs is None:
example_inputs = {"args": torch.rand(self.input_size).to(next(model.parameters()).device)}

dummy_tensor = torch.rand(self.input_size).to(next(model.parameters()).device)
save_path = str(output_dir / (base_model_name + ".onnx"))

torch.onnx.export(model=model, f=save_path, **example_inputs, **self.onnx_export_configuration)
torch.onnx.export(model, dummy_tensor, save_path, **self.onnx_export_configuration)

onnx_model = onnx.load(save_path)
onnx_model = self._postprocess_onnx_model(onnx_model, embed_metadata, precision)
Expand Down
4 changes: 0 additions & 4 deletions src/otx/core/model/entity/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ def export(
base_name: str,
export_format: OTXExportFormatType,
precision: OTXPrecisionType = OTXPrecisionType.FP32,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Export this model to the specified output directory.
Expand All @@ -256,8 +255,6 @@ def export(
base_name: (str): base name for the exported model file. Extension is defined by the target export format
export_format (OTXExportFormatType): format of the output model
precision (OTXExportPrecisionType): precision of the output model
example_inputs (dict, optional): manual arguments for the export function.
If not provided, the exporter will set dummy inputs
Returns:
Path: path to the exported model.
Expand All @@ -269,7 +266,6 @@ def export(
base_name,
export_format,
precision,
example_inputs,
)
self._restore_model_forward()
return exported_model_path
Expand Down
36 changes: 16 additions & 20 deletions src/otx/core/model/module/anomaly/anomaly_lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,13 @@ def to_openvino(
output_dir: Path,
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
example_inputs: dict[str, Any] | None = None,
) -> Path:
if example_inputs is None:
example_inputs = {
"input": (openvino.runtime.PartialShape(self.input_size)),
"example_input": torch.rand(self.input_size),
}

save_path = str(output_dir / f"{base_model_name}.xml")
exported_model = openvino.convert_model(input_model=model, **example_inputs)
exported_model = openvino.convert_model(
input_model=model,
example_input=torch.rand(self.input_size),
input=(openvino.runtime.PartialShape(self.input_size)),
)
exported_model = self._postprocess_openvino_model(exported_model)
openvino.save_model(exported_model, save_path, compress_to_fp16=(precision == OTXPrecisionType.FP16))
return Path(save_path)
Expand All @@ -112,18 +109,19 @@ def to_onnx(
base_model_name: str = "exported_model",
precision: OTXPrecisionType = OTXPrecisionType.FP32,
embed_metadata: bool = True,
example_inputs: dict[str, Any] | None = None,
) -> Path:
if example_inputs is None:
example_inputs = {
"args": (torch.rand(1, 3, self.orig_height, self.orig_width)).to(next(model.parameters()).device),
"dynamic_axes": {"input": {0: "batch_size"}, "output": {0: "batch_size"}},
"input_names": ["input"],
"output_names": ["output"],
}

save_path = str(output_dir / f"{base_model_name}.onnx")
torch.onnx.export(model=model, f=save_path, opset_version=14, **example_inputs)
torch.onnx.export(
model=model,
args=(torch.rand(1, 3, self.orig_height, self.orig_width)).to(
next(model.parameters()).device,
),
f=save_path,
opset_version=14,
dynamic_axes={"input": {0: "batch_size"}, "output": {0: "batch_size"}},
input_names=["input"],
output_names=["output"],
)
onnx_model = onnx.load(save_path)
onnx_model = self._postprocess_onnx_model(onnx_model, embed_metadata, precision)
onnx.save(onnx_model, save_path)
Expand Down Expand Up @@ -430,7 +428,6 @@ def export(
base_name: str,
export_format: OTXExportFormatType,
precision: OTXPrecisionType = OTXPrecisionType.FP32,
example_inputs: dict[str, Any] | None = None,
) -> Path:
"""Export this model to the specified output directory.
Expand Down Expand Up @@ -459,5 +456,4 @@ def export(
base_model_name=base_name,
export_format=export_format,
precision=precision,
example_inputs=example_inputs,
)

0 comments on commit 2d75684

Please sign in to comment.