onnx simplifier(簡(jiǎn)稱 onnxsim),onnxsim 本身只提供 constant folding/propagation(即消除結(jié)果恒為常量的算子)的能力,而圖變換(即合并 conv 和 bn 等等)的能力是由 onnxsim 調(diào)用 onnx optimizer 的各種 pass 實(shí)現(xiàn)的舍悯。
onnxoptimizer航棱、onnxsim被譽(yù)為onnx的優(yōu)化利器,其中onnxsim可以優(yōu)化常量萌衬,onnxoptimizer可以對(duì)節(jié)點(diǎn)進(jìn)行壓縮饮醇。
https://github.com/daquexian/onnx-simplifier
https://github.com/onnx/optimizer
https://convertmodel.com/#input=onnx&output=onnx
import onnx
from onnxsim import simplify
onnx_model = onnx.load(ONNX_name) # load onnx model
model_simp, check = simplify(onnx_model)
assert check, "Simplified ONNX model could not be validated"
onnx.save(model_simp, "resnet18_simplify.onnx")
import onnx
import onnxoptimizer
model = onnx.load(ONNX_name)
new_model = onnxoptimizer.optimize(model)
onnx.save(new_model,"resnet18_optimize.onnx")
# use model_simp as a standard ONNX model object
import onnxruntime as rt
sess_options = rt.SessionOptions()
# Set graph optimization level
sess_options.graph_optimization_level = rt.GraphOptimizationLevel.ORT_ENABLE_EXTENDED
# GraphOptimizationLevel::ORT_DISABLE_ALL -> Disables all optimizations
# GraphOptimizationLevel::ORT_ENABLE_BASIC -> Enables basic optimizations
# GraphOptimizationLevel::ORT_ENABLE_EXTENDED -> Enables basic and extended optimizations
# GraphOptimizationLevel::ORT_ENABLE_ALL -> Enables all available optimizations including layout optimizations
# To enable model serialization after graph optimization set this
sess_options.optimized_model_filepath = "<model_output_path\optimized_model.onnx>"
session = rt.InferenceSession("<model_path>", sess_options)