onnx删除无用属性

这里写自定义目录标题


在推理onnx模型时,报了一个错,如下:

InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : This is an invalid model. In Node, ("Conv_0", Conv, "", -1) : ("x": tensor(float),"conv2d_68.tmp_0Weights": tensor(float),"conv2d_68.tmp_0Bias": tensor(float),) -> ("batch_norm_0.tmp_2": tensor(float),) , Error Unrecognized attribute: output_tensor_shape for operator Conv

unrecognized attribute,用netron 查看模型:
onnx删除无用属性_第1张图片
那么我们可以用以下代码对这类属性进行移除:

model = onnx.load('old.onnx')
nodes = model.graph.node
for node in nodes:
    attrs=node.attribute
    for attr in attrs[:]:
        if attr.name in ['excutor','output_tensor_type','output_tensor_shape']:
            attrs.remove(attr)
onnx.save(model,'new.onnx')

你可能感兴趣的:(ONNX,python,人工智能,开发语言)