onnx ,模型 获取层名字,权重,输入输出,示例

onnx ,模型 获取层名字,权重,输入输出,示例_第1张图片## 1、例如卷积层如下:
netron打开,上图onnx模型卷积层右边的显示,输入,x:input0,第二个是权重,
也有id,权重参数解析如下
代码来源于 https://github.com/MTlab/onnx2caffe

def _convert_conv(node, graph, err):  #onnx by simplify.py, have input[2],because merge bn
    weight_name = node.inputs[1]  #显示id, 1
    bottom_name = node.inputs[0]  # 显示id,  input0
    top_name = node.outputs[0]   #layer out
    layer_name = node.name

    weight_param = None
    if weight_name in node.input_tensors:  #this is input layer,
        weight_param = node.input_tensors[weight_name]

    else:
        err.missing_initializer(node, "Weight tensor: {} not found in the graph initializer".format(weight_name,))

    bias_param = None
    if len(node.inputs) > 2:
        bias_param = node.input_tensors[node.inputs[2]]


    bias_term = True
    if bias_param is None:
        bias_term = False

    channel_dim = weight_param.shape[0]

    dilations = node.attrs["dilations"]
    assert dilations[0] == dilations[1]
    dilation = dilations[0]

    group = node.attrs.get("group", 1)

    kernel_shape = node.attrs["kernel_shape"]
    kernel_h = kernel_shape[0]
    kernel_w = kernel_shape[1]

    pads = node.attrs.get("pads")
    pad_h = pads[0]
    pad_w = pads[1]

    strides = node.attrs["strides"]
    stride_h = strides[0]
    stride_w = strides[1]

    if not bias_term:
        # parameters for convolution layer with batchnorm.
        kwargs = {
            'param': [dict(lr_mult=1, decay_mult=1)],
            'weight_filler': dict(type='gaussian', std=0.01),
        }
    else:
        kwargs = {
            'param': [
                dict(lr_mult=1, decay_mult=1),
                dict(lr_mult=2, decay_mult=0)],
            'weight_filler': dict(type='xavier'),
            'bias_filler': dict(type='constant', value=0)
        }

    layer = Func(
        "Convolution",
        layer_name,
        [bottom_name],
        [top_name],
        kernel_h=kernel_h,
        kernel_w=kernel_w,
        stride_h=stride_h,
        stride_w=stride_w,
        group=group,
        pad_h=pad_h,
        pad_w=pad_w,
        num_output=channel_dim,
        dilation=dilation,
        bias_term=bias_term,
        **kwargs
    )

    graph.channel_dims[top_name] = channel_dim

    return layer        

你可能感兴趣的:(onnx ,模型 获取层名字,权重,输入输出,示例)