Programming Language · 2017-12-12 0

Tensorflow get shape from Graph Def in C++


I’m trying to get shape of input tensor in C++ before I do any inference calls. So I can do it only from Graph Def.

I’m trying like:

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

And it returs false. While PrintDebugString prints:

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

So all I need is to get that 1024x1024x3. I’m not very familiar with Protobuf protocol and for me is totally unclear how to do that.


I don’t have the C++ API installed, so this suggestion might be wrong, but this operation will give you the size of the considered dimension in Python: graphDef.node[i].attr['shape'].dim[j].size. Note that node, shape, dim are google protobuf::Message derivatives.


Would love your thoughts, please comment.x