-
Notifications
You must be signed in to change notification settings - Fork 13
Onnx model testing v2 #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| pytorch2.7.1:� | ||
| � | ||
| input | ||
| conv.weight | ||
| conv.biasoutput | ||
| /conv/Conv"Conv* | ||
| dilations@@�* | ||
| group�* | ||
| kernel_shape@@�* | ||
| pads@@@@�* | ||
| strides@@� | ||
| main_graph*=Bconv.weightJ$�>�_�>���=`ͩ��X8>`��=x�8�V�=��>*B conv.biasJ�CS�Z$ | ||
| input | ||
| batch | ||
| b% | ||
| output | ||
| batch | ||
| B |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| pytorch2.7.1:� | ||
| @ | ||
| input/model/model.0/Relu_output_0/model/model.0/Relu"Relu | ||
| W | ||
| /model/model.0/Relu_output_0/model/model.1/Relu_output_0/model/model.1/Relu"Relu | ||
| W | ||
| /model/model.1/Relu_output_0/model/model.2/Relu_output_0/model/model.2/Relu"Relu | ||
| m | ||
| /model/model.2/Relu_output_0/model/model.3/Flatten_output_0/model/model.3/Flatten"Flatten* | ||
| axis� | ||
| W | ||
| /model/model.3/Flatten_output_0output/model/model.4/Flatten"Flatten* | ||
| axis� | ||
| main_graphZ$ | ||
| input | ||
| batch | ||
| b | ||
| output | ||
| batch | ||
| �B |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| print("Exporting Wrong Dimension model to ONNX format...") | ||
|
|
||
| import torch | ||
| import sys | ||
| import os | ||
|
|
||
| # Add the models directory to Python path - using relative path | ||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx'))) | ||
|
|
||
| try: | ||
| from wrong_dimension_model import WrongDimensionModel | ||
|
|
||
| model = WrongDimensionModel() | ||
| model.eval() | ||
| dummy_input = torch.randn(1, 1, 28, 28) | ||
|
|
||
| print("Testing forward pass...") | ||
| with torch.no_grad(): | ||
| output = model(dummy_input) | ||
| print(f"Model output shape: {output.shape}") | ||
|
|
||
| print("Exporting to ONNX...") | ||
| torch.onnx.export( | ||
| model, dummy_input, "model_wrong_dimension.onnx", | ||
| input_names=["input"], output_names=["output"], | ||
| opset_version=11 | ||
| ) | ||
| print("✅ Exported model_wrong_dimension.onnx successfully!") | ||
|
|
||
| except Exception as e: | ||
| print(f"❌ Error: {e}") | ||
| import traceback | ||
| traceback.print_exc() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you open this file in the
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||
| print("Generating input and output for Conv2D ONNX model...") | ||||||
|
|
||||||
| import onnxruntime as ort | ||||||
| import torch | ||||||
| import json | ||||||
| import numpy as np | ||||||
|
|
||||||
| try: | ||||||
| input_tensor = torch.randn(1, 1, 28, 28) | ||||||
| input_numpy = input_tensor.numpy() | ||||||
|
|
||||||
| session = ort.InferenceSession("/Users/elenapashkova/GravyTesting-Internal/python/models/models_onnx/model_conv2d.onnx") | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| print("ONNX model loaded") | ||||||
|
|
||||||
| outputs = session.run(None, {"input": input_numpy}) | ||||||
| print("Inference complete") | ||||||
|
|
||||||
| with open("input.json", "w") as f: | ||||||
| json.dump(input_numpy.tolist(), f) | ||||||
| print("Input saved") | ||||||
|
|
||||||
| with open("expected_output.json", "w") as f: | ||||||
| json.dump(outputs[0].tolist(), f) | ||||||
| print("Output saved") | ||||||
| except Exception as e: | ||||||
| print("Error:", e) | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| print("Generating input/output for Redundant model...") | ||
|
|
||
| import torch | ||
| import json | ||
| import os | ||
| import sys | ||
|
|
||
| # Add the models directory to Python path - using relative path | ||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx'))) | ||
|
|
||
| try: | ||
| from redundant_layers_model import RedundantModel | ||
|
|
||
| model = RedundantModel() | ||
| model.eval() | ||
| dummy_input = torch.randn(1, 1, 28, 28) | ||
|
|
||
| with torch.no_grad(): | ||
| output = model(dummy_input) | ||
|
|
||
| input_data = dummy_input.numpy().tolist() | ||
| output_data = output.detach().numpy().tolist() | ||
|
|
||
| io_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'testing', 'core', 'input_output_data', 'model_redundant')) | ||
| os.makedirs(io_dir, exist_ok=True) | ||
|
|
||
| with open(os.path.join(io_dir, "input.json"), "w") as f: | ||
| json.dump({"input": input_data}, f) | ||
|
|
||
| with open(os.path.join(io_dir, "output.json"), "w") as f: | ||
| json.dump({"output": output_data}, f) | ||
|
|
||
| print("✅ Successfully saved input/output JSONs to:", io_dir) | ||
|
|
||
| except Exception as e: | ||
| print(f"❌ Error: {e}") | ||
| import traceback | ||
| traceback.print_exc() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| print("Generating input/output for Wrong Dimension model...") | ||
|
|
||
| import torch | ||
| import json | ||
| import os | ||
| import sys | ||
|
|
||
| # Add the models directory to Python path - using relative path | ||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'models', 'models_onnx'))) | ||
|
|
||
| try: | ||
| from wrong_dimension_model import WrongDimensionModel | ||
|
|
||
| model = WrongDimensionModel() | ||
| model.eval() | ||
| dummy_input = torch.randn(1, 1, 28, 28) | ||
|
|
||
| with torch.no_grad(): | ||
| output = model(dummy_input) | ||
|
|
||
| input_data = dummy_input.numpy().tolist() | ||
| output_data = output.detach().numpy().tolist() | ||
|
|
||
| print("Creating output directory...") | ||
| io_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'testing', 'core', 'input_output_data', 'model_wrong_dimension')) | ||
| os.makedirs(io_dir, exist_ok=True) | ||
|
|
||
| with open(os.path.join(io_dir, "input.json"), "w") as f: | ||
| json.dump({"input": input_data}, f) | ||
|
|
||
| with open(os.path.join(io_dir, "output.json"), "w") as f: | ||
| json.dump({"output": output_data}, f) | ||
|
|
||
| print("✅ Successfully saved input/output JSONs to:", io_dir) | ||
|
|
||
| except Exception as e: | ||
| print(f"❌ Error: {e}") | ||
| import traceback | ||
| traceback.print_exc() |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||
| import onnxruntime as ort | ||||||
| import numpy as np | ||||||
| import json | ||||||
| import os | ||||||
|
|
||||||
| def test_conv2d_model_output(): | ||||||
| # Paths - using relative paths since files are in the same directory as the test | ||||||
| onnx_model_path = "python/models/models_onnx/model_conv2d.onnx" | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| input_json_path = "input.json" # File is in the same directory as this test | ||||||
| expected_output_json_path = "expected_output.json" # File is in the same directory as this test | ||||||
|
|
||||||
| # Get the directory where this test file is located | ||||||
| test_dir = os.path.dirname(os.path.abspath(__file__)) | ||||||
|
|
||||||
| # Build full paths to the JSON files | ||||||
| input_json_path = os.path.join(test_dir, "input.json") | ||||||
| expected_output_json_path = os.path.join(test_dir, "expected_output.json") | ||||||
|
|
||||||
| # Load input and expected output | ||||||
| with open(input_json_path, "r") as f: | ||||||
| input_data = np.array(json.load(f)).astype(np.float32) | ||||||
|
|
||||||
| with open(expected_output_json_path, "r") as f: | ||||||
| expected_output = np.array(json.load(f)).astype(np.float32) | ||||||
|
|
||||||
| # Run ONNX inference | ||||||
| session = ort.InferenceSession(onnx_model_path) | ||||||
| outputs = session.run(None, {"input": input_data}) | ||||||
| output = outputs[0] | ||||||
|
|
||||||
| # Check that output is close enough | ||||||
| np.testing.assert_allclose(output, expected_output, rtol=1e-3, atol=1e-5) | ||||||
| print("Test passed!") | ||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
| test_conv2d_model_output() | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| print("Testing Redundant Model...") | ||
|
|
||
| import torch | ||
| import json | ||
| import os | ||
| import sys | ||
|
|
||
| # Add the models directory to Python path - using relative path | ||
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'models', 'models_onnx'))) | ||
|
|
||
| try: | ||
| print("Importing RedundantModel...") | ||
| from redundant_layers_model import RedundantModel | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm this one might be left out of the commit too or potentially I missed it somehow again too |
||
|
|
||
| model = RedundantModel() | ||
| model.eval() | ||
| dummy_input = torch.randn(1, 1, 28, 28) | ||
|
|
||
| with torch.no_grad(): | ||
| output = model(dummy_input) | ||
|
|
||
| input_data = dummy_input.numpy().tolist() | ||
| output_data = output.detach().numpy().tolist() | ||
|
|
||
| print("Creating output directory...") | ||
| # Save input/output | ||
| io_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'input_output_data', 'model_redundant')) | ||
| os.makedirs(io_dir, exist_ok=True) | ||
|
|
||
| with open(os.path.join(io_dir, "input.json"), "w") as f: | ||
| json.dump({"input": input_data}, f) | ||
|
|
||
| with open(os.path.join(io_dir, "output.json"), "w") as f: | ||
| json.dump({"output": output_data}, f) | ||
|
|
||
| print("✅ Successfully saved input/output JSONs") | ||
|
|
||
| except Exception as e: | ||
| print(f"❌ Error: {e}") | ||
| import traceback | ||
| traceback.print_exc() | ||
|
|
||
| def test_redundant_model(): | ||
| print("Test passed!") | ||
| assert True | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably missed this but I'm not sure the git commit included this class