項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 search-input 尚未註冊或是沒有一個 view.php 檔案.

【ONNX 模型分析 #2】分析 onnx model 每一層的內容

前言

分析每一層 onnx model 的內容

範例程式碼

import onnx

model_path = "model.onnx"
model = onnx.load(model_path)

inferred_model = onnx.shape_inference.infer_shapes(model)

# input
print("> Model Inputs:")
for input in inferred_model.graph.input:
    input_shape = [dim.dim_value for dim in input.type.tensor_type.shape.dim]
    print(f"Input: {input.name}, Shape: {input_shape}")

# output edge name
print("> Hidden Layers:")
# for node in inferred_model.graph.value_info:
#     shape = [dim.dim_value for dim in node.type.tensor_type.shape.dim]
#     print(f"Node: {node.name}, Shape: {shape}")


# output layer name with I/O shape
def get_node_shapes(node_outputs):
    shapes = []
    for output in node_outputs:
        for value_info in inferred_model.graph.value_info:
            if value_info.name == output:
                shape = [dim.dim_value for dim in value_info.type.tensor_type.shape.dim]
                shapes.append(shape)
    return shapes


for node in inferred_model.graph.node:
    node_type = node.op_type
    node_input_shapes = get_node_shapes(node.input)
    node_output_shapes = get_node_shapes(node.output)

    print(
        f"Node Name: {node.name}, Type: {node_type}, Input Shapes: {node_input_shapes}, Output Shapes: {node_output_shapes}"
    )

# output
print("> Model Outputs:")
for output in inferred_model.graph.output:
    output_shape = [dim.dim_value for dim in output.type.tensor_type.shape.dim]
    print(f"Output: {output.name}, Shape: {output_shape}")
Howard Weng
Howard Weng

我是 Howard Weng,很多人叫我嗡嗡。這個網站放了我的各種筆記。希望這些筆記也能順便幫助到有需要的人們!如果文章有幫助到你的話,歡迎幫我點讚哦!
另外,因為定位是「個人的隨手筆記」,有些文章內容「⚠️可能我理解有誤⚠️」或「?只寫到一半?」,如果有發現這樣的情況,歡迎在該文章的最下面留言提醒我!我會儘快修正或補上!感謝大家的建議與幫忙,讓網站能變得更好?

文章: 890

★留個言吧!內容有誤或想要補充也歡迎與我討論!