Ganesh M S
Ganesh M S

Reputation: 1063

In Tensorflow, get the names of all the Tensors in a graph using c++

After loading a ".pb" model using c++,
How to get the name value (i.e add & output_TT) using c++ using tensorflow libraries.

The layers in .pb file are as below:

node {
  name: "add"
  op: "Add"
  input: "MatMul"
  input: "bias/read"
  attr {
    key: "T"
    value {
    type: DT_FLOAT
    }
  }
}
node {
  name: "output_TT"
  op: "Softmax"
  input: "add"
  attr {
    key: "T"
    value {
      type: DT_FLOAT
    }
  }  
}

I am not using bazel to build; instead I executed the inbuild makefile with some customisations.

Upvotes: 1

Views: 3064

Answers (2)

Ganesh M S
Ganesh M S

Reputation: 1063

In this below code, the vNames variable will contain the layer names.

int node_count = graph_def.node_size();
std::vector<string> vNames;
for (int i = 0; i < node_count; i++)
{

        auto n = graph_def.node(i);
        if ((has_suffix(n.name(), "/read")) || (has_suffix(n.name(), "_w")) || (has_suffix(n.name(), "_b")))
        {
                vNames.push_back(n.name());
        }
}

Upvotes: 0

Ganesh M S
Ganesh M S

Reputation: 1063

I got the output by following the steps,

int node_count = graph_def.node_size();
for (int i = 0; i < node_count; i++)
{
        auto n = graph_def.node(i);
        cout<<"Names : "<< n.name() <<endl;

}

Upvotes: 6

Related Questions