c++ - Tensorflow 从 C++ 中的 Graph Def 获取形状

标签 c++ tensorflow protocol-buffers

在进行任何推理调用之前,我试图在 C++ 中获取输入张量的形状。所以我只能通过 Graph Def 来完成。

我正在尝试:

 auto att = graph_def.node(i).attr();
 att["shape"].PrintDebugString();
 Tensor tensor;
 std::cerr << tensor.FromProto(att["shape"].tensor()) << std::endl;

它返回错误。当 PrintDebugString 打印时:

shape { dim { size: -1 } dim { size: 1024 } dim { size: 1024 } dim { size: 3 } }

所以我只需要获得 1024x1024x3。我对 Protobuf 协议(protocol)不是很熟悉,对我来说完全不清楚该怎么做。

最佳答案

我设法做到了这一点:

auto shape = graph_def.node().Get(0).attr().at("shape").shape();
for (int i = 0; i < shape.dim_size(); i++) {
    std::cout << shape.dim(i).size()<<std::endl;
}

在我的例子中是:1 128 128 3

关于c++ - Tensorflow 从 C++ 中的 Graph Def 获取形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47753701/

相关文章:

c++ - 无法在 C++ 中获取 protobuf 示例代码链接步骤

c++ - 在对所需抽象基的引用不起作用的地方传递了临时 concreate 派生对象?

c++ - 视觉 C++ : creating a dll using libcurl

c++ - QKeyEvent 处理非拉丁布局

python - 为什么 Tensorflow 不覆盖张量的 __eq__ ?

c# - protobuf-net 如何实现可观的性能?

c++ - 数组/指针混淆

python - AttributeError: 'function' 对象没有属性 'ParseFromString'

python - reshape 数组后keras中的索引错误

javascript - Protocol Buffer : WebApi -> JS - Decoded object is empty