- Notifications
You must be signed in to change notification settings - Fork0
Adds tools to compare models#11
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion_doc/api/index.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -11,7 +11,7 @@ API | ||
npx_jit | ||
npx_annot | ||
npx_numpy | ||
onnx_tools | ||
ort | ||
plotting | ||
tools | ||
16 changes: 16 additions & 0 deletions_doc/api/onnx_tools.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.. _l-api-onnx-tools: | ||
onnx tools | ||
========== | ||
Differences | ||
+++++++++++ | ||
.. autofunction:: onnx_array_api.validation.diff.html_diff | ||
.. autofunction:: onnx_array_api.validation.diff.text_diff | ||
Protos | ||
++++++ | ||
.. autofunction:: onnx_array_api.validation.tools.randomize_proto |
7 changes: 5 additions & 2 deletions_doc/api/tools.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Binary file added_doc/examples/data/small.onnx
Binary file not shown.
121 changes: 121 additions & 0 deletions_doc/examples/plot_optimization.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
""" | ||
.. _l-onnx-array-onnxruntime-optimization: | ||
Optimization with onnxruntime | ||
============================= | ||
Optimize a model with onnxruntime | ||
+++++++++++++++++++++++++++++++++ | ||
""" | ||
import os | ||
from pprint import pprint | ||
import numpy | ||
from pandas import DataFrame | ||
import matplotlib.pyplot as plt | ||
from onnx import load | ||
from onnx_array_api.ext_test_case import example_path | ||
from onnx_array_api.plotting.text_plot import onnx_simple_text_plot | ||
from onnx_array_api.validation.diff import text_diff, html_diff | ||
from onnxruntime import GraphOptimizationLevel, InferenceSession, SessionOptions | ||
from onnx_array_api.ext_test_case import measure_time | ||
from onnx_array_api.ort.ort_optimizers import ort_optimized_model | ||
filename = example_path("data/small.onnx") | ||
optimized = filename + ".optimized.onnx" | ||
if not os.path.exists(optimized): | ||
ort_optimized_model(filename, output=optimized) | ||
print(optimized) | ||
############################# | ||
# Output comparison | ||
# +++++++++++++++++ | ||
so = SessionOptions() | ||
so.graph_optimization_level = GraphOptimizationLevel.ORT_ENABLE_ALL | ||
img = numpy.random.random((1, 3, 112, 112)).astype(numpy.float32) | ||
sess = InferenceSession(filename, so) | ||
sess_opt = InferenceSession(optimized, so) | ||
input_name = sess.get_inputs()[0].name | ||
out = sess.run(None, {input_name: img})[0] | ||
out_opt = sess_opt.run(None, {input_name: img})[0] | ||
if out.shape != out_opt.shape: | ||
print("ERROR shape are different {out.shape} != {out_opt.shape}") | ||
diff = numpy.abs(out - out_opt).max() | ||
print(f"Differences: {diff}") | ||
#################################### | ||
# Difference | ||
# ++++++++++ | ||
# | ||
# Unoptimized model. | ||
with open(filename, "rb") as f: | ||
model = load(f) | ||
print("first model to text...") | ||
text1 = onnx_simple_text_plot(model, indent=False) | ||
print(text1) | ||
##################################### | ||
# Optimized model. | ||
with open(optimized, "rb") as f: | ||
model = load(f) | ||
print("second model to text...") | ||
text2 = onnx_simple_text_plot(model, indent=False) | ||
print(text2) | ||
######################################## | ||
# Differences | ||
print("differences...") | ||
print(text_diff(text1, text2)) | ||
##################################### | ||
# HTML version. | ||
print("html differences...") | ||
output = html_diff(text1, text2) | ||
with open("diff_html.html", "w", encoding="utf-8") as f: | ||
f.write(output) | ||
print("done.") | ||
##################################### | ||
# Benchmark | ||
# +++++++++ | ||
img = numpy.random.random((1, 3, 112, 112)).astype(numpy.float32) | ||
t1 = measure_time(lambda: sess.run(None, {input_name: img}), repeat=25, number=25) | ||
t1["name"] = "original" | ||
print("Original model") | ||
pprint(t1) | ||
t2 = measure_time(lambda: sess_opt.run(None, {input_name: img}), repeat=25, number=25) | ||
t2["name"] = "optimized" | ||
print("Optimized") | ||
pprint(t2) | ||
############################ | ||
# Plots | ||
# +++++ | ||
fig, ax = plt.subplots(1, 1, figsize=(12, 4)) | ||
df = DataFrame([t1, t2]).set_index("name") | ||
print(df) | ||
print(df["average"].values) | ||
print((df["average"] - df["deviation"]).values) | ||
ax.bar(df.index, df["average"].values, yerr=df["deviation"].values, capsize=6) | ||
ax.set_title("Measure performance of optimized model\nlower is better") | ||
plt.grid() | ||
fig.savefig("plot_optimization.png") |
Binary file added_unittests/ut_validation/data/small.onnx
Binary file not shown.
23 changes: 23 additions & 0 deletions_unittests/ut_validation/test_diff.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import unittest | ||
from onnx import load | ||
from onnx.checker import check_model | ||
from onnx_array_api.ext_test_case import ExtTestCase | ||
from onnx_array_api.ort.ort_optimizers import ort_optimized_model | ||
from onnx_array_api.validation.diff import text_diff, html_diff | ||
class TestDiff(ExtTestCase): | ||
def test_diff_optimized(self): | ||
data = self.relative_path(__file__, "data", "small.onnx") | ||
with open(data, "rb") as f: | ||
model = load(f) | ||
optimized = ort_optimized_model(model) | ||
check_model(optimized) | ||
diff = text_diff(model, optimized) | ||
self.assertIn("^^^^^^^^^^^^^^^^", diff) | ||
ht = html_diff(model, optimized) | ||
self.assertIn("<html><body>", ht) | ||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |
20 changes: 20 additions & 0 deletions_unittests/ut_validation/test_tools.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import unittest | ||
from onnx import load | ||
from onnx.checker import check_model | ||
from onnx_array_api.ext_test_case import ExtTestCase | ||
from onnx_array_api.validation.tools import randomize_proto | ||
class TestTools(ExtTestCase): | ||
def test_randomize_proto(self): | ||
data = self.relative_path(__file__, "data", "small.onnx") | ||
with open(data, "rb") as f: | ||
model = load(f) | ||
check_model(model) | ||
rnd = randomize_proto(model) | ||
self.assertEqual(len(model.SerializeToString()), len(rnd.SerializeToString())) | ||
check_model(rnd) | ||
if __name__ == "__main__": | ||
unittest.main(verbosity=2) |
30 changes: 30 additions & 0 deletionsonnx_array_api/ext_test_case.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 12 additions & 4 deletionsonnx_array_api/ort/ort_optimizers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.