diff --git a/modelconverter/utils/config.py b/modelconverter/utils/config.py index 807733f..eb65707 100644 --- a/modelconverter/utils/config.py +++ b/modelconverter/utils/config.py @@ -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 diff --git a/modelconverter/utils/metadata.py b/modelconverter/utils/metadata.py index 3798d82..5f8cb81 100644 --- a/modelconverter/utils/metadata.py +++ b/modelconverter/utils/metadata.py @@ -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 + ) content = csv_path.read_text() metadata = {} diff --git a/modelconverter/utils/onnx_tools.py b/modelconverter/utils/onnx_tools.py index 65b76ed..3ffa46f 100644 --- a/modelconverter/utils/onnx_tools.py +++ b/modelconverter/utils/onnx_tools.py @@ -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") @@ -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 diff --git a/tests/test_utils/test_config.py b/tests/test_utils/test_config.py index e73d87a..75173e2 100644 --- a/tests/test_utils/test_config.py +++ b/tests/test_utils/test_config.py @@ -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(