【问题标题】:caffe2 inference a onnx model , happend IndexError: Input 475 is undefinedcaffe2 推断 onnx 模型,发生 IndexError: Input 475 is undefined
【发布时间】:2021-05-26 09:08:54
【问题描述】:

错误:

当我将 caffe2 用于预训练模型时。模型来自https://github.com/onnx/models/blob/master/vision/classification/vgg/model/vgg16-7.onnx 我使用的模型是预训练模型, 我不改模型,也不用https://github.com/onnx/optimizer

代码是:

import caffe2

model=onnx.load(vgg16-7.onnx)

prepared_backend=caffe2.python.onnx.backend.prepare(model)

然后发生错误:

WARNING:root:This caffe2 python run failed to load cuda module:No module named 'caffe2.python.caffe2_pybind11_state_gpu',and AMD hip module:No module named 'caffe2.python.caffe2_pybind11_state_hip'.Will run in CPU only mode.
WARNING: ONNX Optimizer has been moved to https://github.com/onnx/optimizer.
All further enhancements and fixes to optimizers will be done in this new repo.
The optimizer code in onnx/onnx repo will be removed in 1.9 release.

Traceback (most recent call last):
File "test.py", line 20, in
init_net, predict_net = c2.onnx_graph_to_caffe2_net(onnx_model_proto)
File "/home/eeodev/.local/lib/python3.6/site-packages/caffe2/python/onnx/backend.py", line 921, in onnx_graph_to_caffe2_net
return cls._onnx_model_to_caffe2_net(model, device=device, opset_version=opset_version, include_initializers=True)
File "/home/eeodev/.local/lib/python3.6/site-packages/caffe2/python/onnx/backend.py", line 876, in _onnx_model_to_caffe2_net
onnx_model = onnx.utils.polish_model(onnx_model)
File "/usr/local/lib64/python3.6/site-packages/onnx/utils.py", line 24, in polish_model
model = onnx.optimizer.optimize(model)
File "/usr/local/lib64/python3.6/site-packages/onnx/optimizer.py", line 55, in optimize
optimized_model_str = C.optimize(model_str, passes)
IndexError: Input 475 is undefined!

谁能告诉解决办法?

另外,如果是pytorch模型,在转换为onnx模型时,我们可以使用torch.onnx.export(model, input, 'model.onnx', verbose=True, keep_initializers_as_inputs=True),通过keep_initializers_as_inputs=True,使用caffe2加载模型不会出现错误。但是我使用的模型是预训练模型,这个方法怎么用?

【问题讨论】:

    标签: onnx caffe2


    【解决方案1】:

    我认为这与 IR 差距问题有关:https://github.com/onnx/onnx/issues/2902。 目前,如果初始化程序不包含在模型的输入中,则 ONNX 存储库中已弃用的 ONNX 优化器无法处理 IR_VERSION >=4 的 ONNX 模型。 解决方法是使用以下脚本让您的模型包含来自初始化程序的输入(由 GitHub 中的@TMVector 提供):

    def add_value_info_for_constants(model : onnx.ModelProto):
        """
        Currently onnx.shape_inference doesn't use the shape of initializers, so add
        that info explicitly as ValueInfoProtos.
        Mutates the model.
        Args:
            model: The ModelProto to update.
        """
        # All (top-level) constants will have ValueInfos before IRv4 as they are all inputs
        if model.ir_version < 4:
            return
    
        def add_const_value_infos_to_graph(graph : onnx.GraphProto):
            inputs = {i.name for i in graph.input}
            existing_info = {vi.name: vi for vi in graph.value_info}
            for init in graph.initializer:
                # Check it really is a constant, not an input
                if init.name in inputs:
                    continue
    
                # The details we want to add
                elem_type = init.data_type
                shape = init.dims
    
                # Get existing or create new value info for this constant
                vi = existing_info.get(init.name)
                if vi is None:
                    vi = graph.value_info.add()
                    vi.name = init.name
    
                # Even though it would be weird, we will not overwrite info even if it doesn't match
                tt = vi.type.tensor_type
                if tt.elem_type == onnx.TensorProto.UNDEFINED:
                    tt.elem_type = elem_type
                if not tt.HasField("shape"):
                    # Ensure we set an empty list if the const is scalar (zero dims)
                    tt.shape.dim.extend([])
                    for dim in shape:
                        tt.shape.dim.add().dim_value = dim
    
            # Handle subgraphs
            for node in graph.node:
                for attr in node.attribute:
                    # Ref attrs refer to other attrs, so we don't need to do anything
                    if attr.ref_attr_name != "":
                        continue
    
                    if attr.type == onnx.AttributeProto.GRAPH:
                        add_const_value_infos_to_graph(attr.g)
                    if attr.type == onnx.AttributeProto.GRAPHS:
                        for g in attr.graphs:
                            add_const_value_infos_to_graph(g)
    
    
        return add_const_value_infos_to_graph(model.graph)
    

    【讨论】:

    • 感谢您的代码,当我使用您的代码时,“IndexError:输入 475 未定义!”问题解决了。但另一个问题发生了。当运行代码“ import caffe2.python.onnx.backend as backend model=onnx.load(model_path) model=add_value_info_for_constants(model)” 当我运行上面的代码时,没有错误hanpend。但是当我运行这个代码“ rep=backend.prepare(model,device="CPU")" an error occurred," Message='NoneType' object has no attribute 'SerializeToString'" 你知道为什么吗?
    • 请使用原版,不用抓。模型将在 add_value_info_for_constants(model) 之后更新。
    猜你喜欢
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2021-11-02
    • 2020-03-31
    相关资源
    最近更新 更多