美文网首页
onnx模型解析

onnx模型解析

作者: i_1312 | 来源:发表于2023-12-20 23:37 被阅读0次
    image.png
    ir_version: 3
    producer_name: "onnx.utils.extract_model"
    graph {
      node {
        input: "Input3"
        input: "Constant339"
        output: "Minus340_Output_0"
        name: "Minus340"
        op_type: "Sub"
        doc_string: ""
        domain: ""
      }
      node {
        input: "Minus340_Output_0"
        input: "Constant343"
        output: "Block352_Output_0"
        name: "Block352"
        op_type: "Div"
        doc_string: ""
        domain: ""
      }
      name: "Extracted from {CNTKGraph}"
      initializer {
        data_type: 1
        float_data: 127.5
        name: "Constant339"
      }
      initializer {
        data_type: 1
        float_data: 255.0
        name: "Constant343"
      }
      input {
        name: "Input3"
        type {
          tensor_type {
            elem_type: 1
            shape {
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 64
              }
              dim {
                dim_value: 64
              }
            }
          }
        }
      }
      output {
        name: "Block352_Output_0"
        type {
          tensor_type {
            elem_type: 1
            shape {
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 64
              }
              dim {
                dim_value: 64
              }
            }
          }
        }
      }
      value_info {
        name: "Minus340_Output_0"
        type {
          tensor_type {
            elem_type: 1
            shape {
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 64
              }
              dim {
                dim_value: 64
              }
            }
          }
        }
      }
      value_info {
        name: "Block352_Output_0"
        type {
          tensor_type {
            elem_type: 1
            shape {
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 1
              }
              dim {
                dim_value: 64
              }
              dim {
                dim_value: 64
              }
            }
          }
        }
      }
    }
    opset_import {
      domain: ""
      version: 7
    }
    
    
    # 了解了onnx的结构后,我们可以根据它的结构将其拆分成多个单节点的onnx模型,以便于对整体模型的单个节点进行测试和分析。
    import onnx
    from onnx import helper,numpy_helper
    
    
    
    
    #获取对应输入信息
    def getInputTensorValueInfo(input_name,model):
        in_tvi = []
        for name in input_name:
            for params_input in model.graph.input:
                if params_input.name == name:
                   in_tvi.append(params_input)
            for inner_output in model.graph.value_info:
                if inner_output.name == name:
                    in_tvi.append(inner_output)
        return in_tvi
    
    #获取对应输出信息
    def getOutputTensorValueInfo(output_name,model):
        out_tvi = []
        for name in output_name:
            out_tvi = [inner_output for inner_output in model.graph.value_info if inner_output.name == name]
            if name == model.graph.output[0].name:
                out_tvi.append(model.graph.output[0])
        return out_tvi
    
    #获取对应超参数值
    def getInitTensorValue(input_name,model):
        init_t = []
        for name in input_name:
            init_t = [init for init in model.graph.initializer if init.name == name]
        return init_t
    
    #构建单个节点onnx模型
    def createSingelOnnxModel(ModelPath,nodename,SaveType="",SavePath=""):
        model = loadOnnxModel(str(ModelPath))
        Node,input_name,output_name = getNodeAndIOname(nodename,model)
        in_tvi = getInputTensorValueInfo(input_name,model)
        out_tvi = getOutputTensorValueInfo(output_name,model)
        init_t = getInitTensorValue(input_name,model)
    
        graph_def = helper.make_graph(
                    [Node],
                    nodename,
                    inputs=in_tvi,  # 输入
                    outputs=out_tvi,  # 输出
                    initializer=init_t,  # initalizer
                )
        model_def = helper.make_model(graph_def, producer_name='onnx-example')
        print(nodename+"onnx模型生成成功!")
    
    # 获取整个ONNX模型的一些信息
    
    
    
    
    
    #获取节点名列表
    def getNodeNameList(model):
        NodeNameList = []
        for i in range(len(model.graph.node)):
            NodeNameList.append(model.graph.node[i].name)
        return NodeNameList
    
    
    
    
    
    def get_node_attributes(node):
        return node.attribute
    
    
    def get_node_inputs(node):
        return node.input
    
    
    def get_node_outputs(node):
        return node.output
    
    
    def show_weight(weight):
        print("="*10, "details of weight: ", weight.name, "="*10)
        print("data type: ", weight.data_type)
        print("shape: ", weight.dims)
        data_numpy = numpy_helper.to_array(weight)
        # data_numpy = np.frombuffer(weight.raw_data, dtype=xxx)
        # print("detail data:", data_numpy)
        print("="*40)
    
    
    
    # onnx.utils.extract_model("emotion-ferplus-7.onnx","mini_model.onnx",["Input3"],["Block352_Output_0"])
    
    model = onnx.load("mini_model.onnx")
    # print(model.ir_version)   # IR的版本
    # print(model.producer_name)   #
    # print(model.opset_import)   # opset 版本信息
    
    
    # graph   
    # graph中有node(NodeProto类型),input(ValueInfoProto类型),output(ValueInfoProto类型)和initializer(TensorProto类型)
    # 其中node中存放着模型中的所有计算节点,input中存放着模型所有的输入节点,output存放着模型所有的输出节点,initializer存放着模型所有的权重;
    #value_info存放了各个tensor 的信息
    # node 通过input和output的指向关系,描绘出一个深度学习模型的拓扑图
    # for node in model.graph.node:
    #     print(node)
    
    #获取节点和节点的输入输出名列表,一般节点的输入将来自于上一层的输出放在列表前面,参数放在列表后面
    def getNode(nodename,model):
        for i in range(len(model.graph.node)):
            if model.graph.node[i].name == nodename:
                Node = model.graph.node[i]
                input_name = model.graph.node[i].input
                output_name = model.graph.node[i].output
        return Node,input_name,output_name
    
    
        
    for node in model.graph.node:
        print(get_node_inputs(node))
        print(get_node_outputs(node))
        print(get_node_attributes(node))
    
    
    # print(model.graph.input)
    # print(model.graph.output)
    
    
    # for value_inifo in  model.graph.value_info:
    #     print(value_inifo)
    
    
    # for initializer in  model.graph.initializer:
    #     print(initializer)
    
    
    
    #获取节点数量
    print(len(model.graph.node))
    
    
    
    
    # with open("model.txt","w") as f:
    #     f.write(str(model))
        
    # print(model.graph)
    
    print(getNodeNameList(model))
    
    
    
    
    

    https://github.com/ZhangGe6/onnx-modifier/tree/master
    https://github.com/bindog/onnx-surgery/blob/master/surgery.py
    https://bindog.github.io/blog/2020/03/13/deep-learning-model-convert-and-depoly/
    https://www.zhihu.com/question/386526462
    https://blog.csdn.net/ChuiGeDaQiQiu/article/details/123794387

    相关文章

      网友评论

          本文标题:onnx模型解析

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