4

I have some Keras experience and am learning TensorFlowSharp (https://github.com/migueldeicaza/TensorFlowSharp). Using the ExampleInceptionInference project, I want to use a model I trained in my Keras python code.

(I have created a .pb file using @jdehesa reply here: How to export Keras .h5 to tensorflow .pb?)

When I load the .pb model file in TensorFlowSharp, this line fails:

runner.AddInput (graph ["input"] [0], tensor).Fetch (graph ["output"] [0]);

It seems that graph ["input"] and graph ["output"] are both null. Do I need to name the layers in my model perhaps?

The Keras model I am using is a modified and retrained version of VGG16.

Is it possible to output a list of named layers in TensorFlowSharp? Or is it possible to reference them in some other way? (by number?)

Sugrue
  • 3,629
  • 5
  • 35
  • 53

3 Answers3

3

The GetEnumerator() method of a TFGraph allows you to enumerate all the ops.

List<TFOperation> op_list = new List<TFOperation>( graph.GetEnumerator() );

You can examine the TFOperation Name and OpType to see what's in the graph.

2

I ran into this same problem with TensorFlowSharp. The easiest way to find input and output names is to load the model into Netron (https://github.com/lutzroeder/netron), which visually displays the model. You can click on the very first node to see the input 'id:' and the very last node 'name' property.

ScottS
  • 21
  • 1
0

In the end, I found two ways of solving this. One is to look at the pb file in HxD and find the name of the layers there. The input layer is easy to find right at the top of the file:

HxD view of the pd file

The output layer is harder to find. But option 2 for solving this is to name the layer:

outputlayer.name = "output"

However, when loading the layer, the name is slightly different and includes the type of activation. In this case its "output/Softmax", and you can find that in the binary of the pb file too.

Sugrue
  • 3,629
  • 5
  • 35
  • 53