By jereksel

2016-10-05 12:44:35 8 Comments

I'm trying to use Keras' model in "pure" TensorFlow (I want to use it in Android app). I've successfully exported Keras model to protobuf and imported it to Tensorflow. However running tensorflow model requires providing input and output tensors' names and I don't know how to find them. My model looks like this:

seq = Sequential()
seq.add(Convolution2D(32, 3, 3, input_shape=(3, 15, 15), name="Conv1"))
seq.add(Activation('softmax', name="Act4"))

When I'm printing tensors in TensorFlow I can find:

Tensor("Conv1_W/initial_value:0", shape=(32, 3, 3, 3), dtype=float32)
Tensor("Conv1_W:0", dtype=float32_ref)
Tensor("Conv1_W/Assign:0", shape=(32, 3, 3, 3), dtype=float32_ref)
Tensor("Conv1_W/read:0", dtype=float32)

Tensor("Act4_sample_weights:0", dtype=float32)
Tensor("Act4_target:0", dtype=float32)

Hovewer, there are no tensors that have shape (3,15,15).

I've seen here that I can add "my_input_tensor" as input, hovewer I don't know which type is it - I've tried TensorFlow's and Keras' placeholders and they gave me this error:

/XXXXXXXXX/lib/python2.7/site-packages/keras/engine/topology.pyc in __init__(self, input, output, name)
   1599             # check that x is an input tensor
   1600             layer, node_index, tensor_index = x._keras_history
-> 1601             if len(layer.inbound_nodes) > 1 or (layer.inbound_nodes and layer.inbound_nodes[0].inbound_layers):
   1602                 cls_name = self.__class__.__name__
   1603                 warnings.warn(cls_name + ' inputs must come from '

AttributeError: 'NoneType' object has no attribute 'inbound_nodes'


@rahullak 2019-03-31 06:52:54

To get the input and output tensors of your Keras models, do the following:

input_tensor = seq.inputs[0]
output_tensor = seq.outputs[0]
print("Inputs: "+str(input_tensor))
print("Outputs: "+str(output_tensor))

The above assumes that there is only 1 input tensor and 1 output tensor. If you have more, then you would have to use the appropriate index to get those tensors.

Note that there is a difference between layer output shapes and tensor output shapes. The two are usually the same, but not always.

@Daniel Möller 2017-09-05 22:10:43

Call a model.summary() in Keras to see all the layers.

An input tensor will often be called input_1, input_2, etc. See in the summary the correct name.

When you use input_shape=(3,15,15) in Keras, you're actually using tensors that have shape (None, 3, 15, 15). Where None will be replaced by the batch size in training or prediction.

Often, for these unknonw dimensions, you use -1, such as in (-1, 3, 15, 15). But I cannot assure you that it will work like this. It works perfectly for reshaping tensors, but for creating I've never tested.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Tensorflow: how to save/restore a model?

2 Answered Questions

Combine keras functional api with tensorflow

  • 2018-06-01 22:19:35
  • lab_rat
  • 786 View
  • 1 Score
  • 2 Answer
  • Tags:   tensorflow keras

2 Answered Questions

2 Answered Questions

1 Answered Questions

[SOLVED] building keras Model ValueError

5 Answered Questions

[SOLVED] Tensor is not an element of this graph; deploying Keras model

1 Answered Questions

1 Answered Questions

[SOLVED] TensorFlow to Keras Tensor

Sponsored Content