Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions modelconverter/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ def _parse_values(
return [value, value, value]
return value

def requires_onnx_input_modification(
self, *, reverse_only: bool = False
) -> bool:
if self.encoding_mismatch:
return True
if reverse_only:
return False
return (
self.mean_values is not None
and any(v != 0 for v in self.mean_values)
) or (
self.scale_values is not None
and any(v != 1 for v in self.scale_values)
)


class TargetConfig(BaseModelExtraForbid):
disable_calibration: bool = False
Expand Down
12 changes: 8 additions & 4 deletions modelconverter/utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ def _get_metadata_dlc(path: Path) -> Metadata:
if path.suffix == ".csv":
csv_path = path
else:
csv_path = Path("info.csv")
subprocess_run(
["snpe-dlc-info", "-i", path, "-s", csv_path], silent=True
)
csv_path = path.with_suffix(".info.csv")
if (
not csv_path.exists()
or csv_path.stat().st_mtime < path.stat().st_mtime
):
subprocess_run(
["snpe-dlc-info", "-i", path, "-s", csv_path], silent=True
)
Comment thread
klemen1999 marked this conversation as resolved.
content = csv_path.read_text()

metadata = {}
Expand Down
15 changes: 10 additions & 5 deletions modelconverter/utils/onnx_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ def onnx_attach_normalization_to_inputs(
*,
reverse_only: bool = False,
) -> Path:
if not any(
cfg.requires_onnx_input_modification(reverse_only=reverse_only)
for cfg in input_configs.values()
):
logger.info(
"No ONNX input normalization changes requested; using original model."
)
return model_path

model = onnx.load(str(model_path))
if model_path.with_suffix(".onnx_data").exists():
model_data_path = str(model_path).replace(".onnx", ".onnx_data")
Expand All @@ -136,11 +145,7 @@ def onnx_attach_normalization_to_inputs(
if input_name not in input_configs:
continue
cfg = input_configs[input_name]
if (
cfg.encoding.from_ == cfg.encoding.to
and cfg.mean_values is None
and cfg.scale_values is None
):
if not cfg.requires_onnx_input_modification(reverse_only=reverse_only):
continue

shape = cfg.shape
Expand Down
24 changes: 24 additions & 0 deletions tests/test_utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,30 @@ def test_modified_onnx(keys: list[str], values: list[str]):
assert input_configs[inp].scale_values is None or [1, 1, 1]


def test_modified_onnx_noop_returns_original_model():
model_path = DATA_DIR / "dummy_model.onnx"
modified_path = DATA_DIR / "dummy_model_noop_modified.onnx"
config = Config.get_config(
None,
{
"input_model": str(model_path),
"encoding": "NONE",
},
)
input_configs = {
inp.name: inp for inp in next(iter(config.stages.values())).inputs
}

result = onnx_attach_normalization_to_inputs(
model_path,
modified_path,
input_configs,
)

assert result == model_path
assert not modified_path.exists()


def test_output_nn_config_from_yaml_raw_input_with_archive_preprocess():
model_path = DATA_DIR / "dummy_model.onnx"
config = Config.get_config(
Expand Down
Loading