From 414bbedf326fad5333b96bd7445d9d33cbe5ce74 Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Sat, 8 Mar 2025 17:44:06 +0800 Subject: [PATCH 1/3] [tools/onnx-subgraph] mse comparing of src model and multi sub models mse comparing of src model and multi sub models ONE-DCO-1.0-Signed-off-by: Youxin Chen --- tools/onnx_subgraph/single_vs_multiple_onnx.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tools/onnx_subgraph/single_vs_multiple_onnx.py b/tools/onnx_subgraph/single_vs_multiple_onnx.py index 86d3723ec09..c68a0621849 100644 --- a/tools/onnx_subgraph/single_vs_multiple_onnx.py +++ b/tools/onnx_subgraph/single_vs_multiple_onnx.py @@ -139,6 +139,20 @@ def prepare_initial_input_data(onnx_model_path, default_input_data): return initial_input_data +def compare_results(output_single, output_multiple): + """ + Compares the Mean Squared Error (MSE) between identically named outputs from + two inference result dictionaries.Ensures each output name is processed only once. + """ + all_keys = set(output_single.keys()).union(set(output_multiple.keys())) + for key in sorted(all_keys): + if key in output_single and key in output_multiple: + single_output = np.array(output_single[key]) + multiple_output = np.array(output_multiple[key]) + mse = np.mean((single_output - multiple_output)**2) + print(f"Output '{key}' MSE: {mse}") + else: + print(f"Output '{key}' is missing in one of the result sets.") if __name__ == "__main__": arg_parser = argparse.ArgumentParser() @@ -176,3 +190,6 @@ def prepare_initial_input_data(onnx_model_path, default_input_data): output_multiple = model_inference.infer_multiple_onnx_models( initial_input_data, output_names_list) print("Multiple subgraph inference completed!") + + print("Comparing inference results between single ONNX model and multiple subgraphs...") + compare_results(output_single, output_multiple) From fd6ac44f6793b6539517d8804bc6a59fe7679aea Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Sat, 8 Mar 2025 18:37:45 +0800 Subject: [PATCH 2/3] format checking issue --- tools/onnx_subgraph/single_vs_multiple_onnx.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/onnx_subgraph/single_vs_multiple_onnx.py b/tools/onnx_subgraph/single_vs_multiple_onnx.py index c68a0621849..12fac14ea2a 100644 --- a/tools/onnx_subgraph/single_vs_multiple_onnx.py +++ b/tools/onnx_subgraph/single_vs_multiple_onnx.py @@ -30,7 +30,7 @@ class ModelInference: Description: Subgraphsiostxt_path is a txt file that describes the structure of the model graph and is used to get input/output node names.The model_path contains paths to multiple onnx files. - The load_sessions function will sort the onnx models in the model_path according to the + The load_sessions function will sort the onnx models in the model_path according to the order specified in subgraphsiostxt_path. """ def __init__(self, model_path, subgraphsiostxt_path): @@ -139,9 +139,10 @@ def prepare_initial_input_data(onnx_model_path, default_input_data): return initial_input_data + def compare_results(output_single, output_multiple): """ - Compares the Mean Squared Error (MSE) between identically named outputs from + Compares the Mean Squared Error (MSE) between identically named outputs from two inference result dictionaries.Ensures each output name is processed only once. """ all_keys = set(output_single.keys()).union(set(output_multiple.keys())) @@ -154,6 +155,7 @@ def compare_results(output_single, output_multiple): else: print(f"Output '{key}' is missing in one of the result sets.") + if __name__ == "__main__": arg_parser = argparse.ArgumentParser() arg_parser.add_argument('-s', @@ -191,5 +193,6 @@ def compare_results(output_single, output_multiple): initial_input_data, output_names_list) print("Multiple subgraph inference completed!") - print("Comparing inference results between single ONNX model and multiple subgraphs...") + print("Comparing inference results between single ONNX model \ + and multiple subgraphs...") compare_results(output_single, output_multiple) From 386276d226bb83ab016f6b4252d9717bc9b6ce92 Mon Sep 17 00:00:00 2001 From: chenyx113 Date: Mon, 10 Mar 2025 09:31:14 +0800 Subject: [PATCH 3/3] Update single_vs_multiple_onnx.py update as code review comment --- tools/onnx_subgraph/single_vs_multiple_onnx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/onnx_subgraph/single_vs_multiple_onnx.py b/tools/onnx_subgraph/single_vs_multiple_onnx.py index 12fac14ea2a..8ffb8cb7a21 100644 --- a/tools/onnx_subgraph/single_vs_multiple_onnx.py +++ b/tools/onnx_subgraph/single_vs_multiple_onnx.py @@ -30,7 +30,7 @@ class ModelInference: Description: Subgraphsiostxt_path is a txt file that describes the structure of the model graph and is used to get input/output node names.The model_path contains paths to multiple onnx files. - The load_sessions function will sort the onnx models in the model_path according to the + The load_sessions function will sort the onnx models in the model_path according to the order specified in subgraphsiostxt_path. """ def __init__(self, model_path, subgraphsiostxt_path):