|
| 1 | +""" |
| 2 | +
|
| 3 | +.. _l-onnx-array-onnxruntime-optimization: |
| 4 | +
|
| 5 | +Optimization with onnxruntime |
| 6 | +============================= |
| 7 | +
|
| 8 | +
|
| 9 | +Optimize a model with onnxruntime |
| 10 | ++++++++++++++++++++++++++++++++++ |
| 11 | +""" |
| 12 | +importos |
| 13 | +frompprintimportpprint |
| 14 | +importnumpy |
| 15 | +frompandasimportDataFrame |
| 16 | +importmatplotlib.pyplotasplt |
| 17 | +fromonnximportload |
| 18 | +fromonnx_array_api.ext_test_caseimportexample_path |
| 19 | +fromonnx_array_api.plotting.text_plotimportonnx_simple_text_plot |
| 20 | +fromonnx_array_api.validation.diffimporttext_diff,html_diff |
| 21 | +fromonnxruntimeimportGraphOptimizationLevel,InferenceSession,SessionOptions |
| 22 | +fromonnx_array_api.ext_test_caseimportmeasure_time |
| 23 | +fromonnx_array_api.ort.ort_optimizersimportort_optimized_model |
| 24 | + |
| 25 | + |
| 26 | +filename=example_path("data/small.onnx") |
| 27 | +optimized=filename+".optimized.onnx" |
| 28 | + |
| 29 | +ifnotos.path.exists(optimized): |
| 30 | +ort_optimized_model(filename,output=optimized) |
| 31 | +print(optimized) |
| 32 | + |
| 33 | +############################# |
| 34 | +# Output comparison |
| 35 | +# +++++++++++++++++ |
| 36 | + |
| 37 | +so=SessionOptions() |
| 38 | +so.graph_optimization_level=GraphOptimizationLevel.ORT_ENABLE_ALL |
| 39 | +img=numpy.random.random((1,3,112,112)).astype(numpy.float32) |
| 40 | + |
| 41 | +sess=InferenceSession(filename,so) |
| 42 | +sess_opt=InferenceSession(optimized,so) |
| 43 | +input_name=sess.get_inputs()[0].name |
| 44 | +out=sess.run(None, {input_name:img})[0] |
| 45 | +out_opt=sess_opt.run(None, {input_name:img})[0] |
| 46 | +ifout.shape!=out_opt.shape: |
| 47 | +print("ERROR shape are different {out.shape} != {out_opt.shape}") |
| 48 | +diff=numpy.abs(out-out_opt).max() |
| 49 | +print(f"Differences:{diff}") |
| 50 | + |
| 51 | +#################################### |
| 52 | +# Difference |
| 53 | +# ++++++++++ |
| 54 | +# |
| 55 | +# Unoptimized model. |
| 56 | + |
| 57 | +withopen(filename,"rb")asf: |
| 58 | +model=load(f) |
| 59 | +print("first model to text...") |
| 60 | +text1=onnx_simple_text_plot(model,indent=False) |
| 61 | +print(text1) |
| 62 | + |
| 63 | +##################################### |
| 64 | +# Optimized model. |
| 65 | + |
| 66 | + |
| 67 | +withopen(optimized,"rb")asf: |
| 68 | +model=load(f) |
| 69 | +print("second model to text...") |
| 70 | +text2=onnx_simple_text_plot(model,indent=False) |
| 71 | +print(text2) |
| 72 | + |
| 73 | +######################################## |
| 74 | +# Differences |
| 75 | + |
| 76 | +print("differences...") |
| 77 | +print(text_diff(text1,text2)) |
| 78 | + |
| 79 | +##################################### |
| 80 | +# HTML version. |
| 81 | + |
| 82 | +print("html differences...") |
| 83 | +output=html_diff(text1,text2) |
| 84 | +withopen("diff_html.html","w",encoding="utf-8")asf: |
| 85 | +f.write(output) |
| 86 | +print("done.") |
| 87 | + |
| 88 | +##################################### |
| 89 | +# Benchmark |
| 90 | +# +++++++++ |
| 91 | + |
| 92 | +img=numpy.random.random((1,3,112,112)).astype(numpy.float32) |
| 93 | + |
| 94 | +t1=measure_time(lambda:sess.run(None, {input_name:img}),repeat=25,number=25) |
| 95 | +t1["name"]="original" |
| 96 | +print("Original model") |
| 97 | +pprint(t1) |
| 98 | + |
| 99 | +t2=measure_time(lambda:sess_opt.run(None, {input_name:img}),repeat=25,number=25) |
| 100 | +t2["name"]="optimized" |
| 101 | +print("Optimized") |
| 102 | +pprint(t2) |
| 103 | + |
| 104 | + |
| 105 | +############################ |
| 106 | +# Plots |
| 107 | +# +++++ |
| 108 | + |
| 109 | + |
| 110 | +fig,ax=plt.subplots(1,1,figsize=(12,4)) |
| 111 | + |
| 112 | +df=DataFrame([t1,t2]).set_index("name") |
| 113 | +print(df) |
| 114 | + |
| 115 | +print(df["average"].values) |
| 116 | +print((df["average"]-df["deviation"]).values) |
| 117 | + |
| 118 | +ax.bar(df.index,df["average"].values,yerr=df["deviation"].values,capsize=6) |
| 119 | +ax.set_title("Measure performance of optimized model\nlower is better") |
| 120 | +plt.grid() |
| 121 | +fig.savefig("plot_optimization.png") |