Python 使用C++;。使用c+从onnx模型中提取图层、输入和输出形状+;

Python 使用C++;。使用c+从onnx模型中提取图层、输入和输出形状+;,python,c++,onnx,onnxruntime,Python,C++,Onnx,Onnxruntime,我试图从onnx模型中提取输入层、输出层及其形状等数据。我知道有python接口可以做到这一点。我想做类似的事情,但在C++。我还粘贴了链接中的代码。我在python中尝试过它,它对我很有效。我想知道是否有C++ API的作用。 import onnx model = onnx.load(r"model.onnx") # The model is represented as a protobuf structure and it can be accessed # us

我试图从onnx模型中提取输入层、输出层及其形状等数据。我知道有python接口可以做到这一点。我想做类似的事情,但在C++。我还粘贴了链接中的代码。我在python中尝试过它,它对我很有效。我想知道是否有C++ API的作用。
import onnx

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

# The model is represented as a protobuf structure and it can be accessed
# using the standard python-for-protobuf methods

# iterate through inputs of the graph
for input in model.graph.input:
    print (input.name, end=": ")
    # get type of input tensor
    tensor_type = input.type.tensor_type
    # check if it has a shape:
    if (tensor_type.HasField("shape")):
        # iterate through dimensions of the shape:
        for d in tensor_type.shape.dim:
            # the dimension may have a definite (integer) value or a symbolic identifier or neither:
            if (d.HasField("dim_value")):
                print (d.dim_value, end=", ")  # known dimension
            elif (d.HasField("dim_param")):
                print (d.dim_param, end=", ")  # unknown dimension with symbolic name
            else:
                print ("?", end=", ")  # unknown dimension with no name
    else:
        print ("unknown rank", end="")
    print()

我也是C++新手,请帮助我。

Onnx格式基本上是A,所以它可以用任何语言Prutoc编译器支持。
import onnx

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

# The model is represented as a protobuf structure and it can be accessed
# using the standard python-for-protobuf methods

# iterate through inputs of the graph
for input in model.graph.input:
    print (input.name, end=": ")
    # get type of input tensor
    tensor_type = input.type.tensor_type
    # check if it has a shape:
    if (tensor_type.HasField("shape")):
        # iterate through dimensions of the shape:
        for d in tensor_type.shape.dim:
            # the dimension may have a definite (integer) value or a symbolic identifier or neither:
            if (d.HasField("dim_value")):
                print (d.dim_value, end=", ")  # known dimension
            elif (d.HasField("dim_param")):
                print (d.dim_param, end=", ")  # unknown dimension with symbolic name
            else:
                print ("?", end=", ")  # unknown dimension with no name
    else:
        print ("unknown rank", end="")
    print()
如果是C++

  • 获取onnx协议文件()
  • 使用
    protoc--cpp_out=编译它。onnx.proto3
    命令。它将生成
    onnx.proto3.pb.cc
    onnx.proto3.pb.h
    文件
  • 链接protobuf库(可能是protobuf lite),生成cpp文件和以下代码:
  • #包括
    #包括
    #包括“onnx.proto3.pb.h”
    无效打印尺寸(常数::onnx::张量ShapeProto尺寸和尺寸)
    {
    开关(尺寸值\外壳())
    {
    大小写onnx::TensorShapeProto_维度::ValueCase::kDimParam:
    
    非常感谢你,伙计。我一直在努力寻找这个。我会试试这个:)