美文网首页
onnx模型优化

onnx模型优化

作者: i_1312 | 来源:发表于2024-01-05 23:20 被阅读0次

    onnx simplifier(简称 onnxsim),onnxsim 本身只提供 constant folding/propagation(即消除结果恒为常量的算子)的能力,而图变换(即合并 conv 和 bn 等等)的能力是由 onnxsim 调用 onnx optimizer 的各种 pass 实现的。

    onnxoptimizer、onnxsim被誉为onnx的优化利器,其中onnxsim可以优化常量,onnxoptimizer可以对节点进行压缩。

    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)
    

    相关文章

      网友评论

          本文标题:onnx模型优化

          本文链接:https://www.haomeiwen.com/subject/tdaindtx.html