Maximus
Maximus

Reputation: 669

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();
 att["shape"].PrintDebugString();
 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.

Upvotes: 1

Views: 2193

Answers (1)

Admiral_x
Admiral_x

Reputation: 206

I managed to do it this way:

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;
}

In my case it was: 1 128 128 3

Upvotes: 5

Related Questions