8

I found this GitHub example about print activation maps. Code is quite simple. All I did was copy pasted the function.

def get_activations(model, model_inputs, print_shape_only=False, layer_name=None):
    print('----- activations -----')
    activations = []
    inp = model.input

    model_multi_inputs_cond = True
    if not isinstance(inp, list):
        # only one input! let's wrap it in a list.
        inp = [inp]
        model_multi_inputs_cond = False

    outputs = [layer.output for layer in model.layers if
               layer.name == layer_name or layer_name is None]  # all layer outputs

    funcs = [K.function(inp + [K.learning_phase()], [out]) for out in outputs]  # evaluation functions

    if model_multi_inputs_cond:
        list_inputs = []
        list_inputs.extend(model_inputs)
        list_inputs.append(0.)
    else:
        list_inputs = [model_inputs, 0.]


    print list_inputs
    layer_outputs = [func(list_inputs)[0] for func in funcs]
    for layer_activations in layer_outputs:
        activations.append(layer_activations)
        if print_shape_only:
            print(layer_activations.shape)
        else:
            print(layer_activations)
    return activations

And then I passed my model and inputs. However, it generates this error

Traceback (most recent call last):
  File "test_cnn_128.py", line 80, in <module>
    get_activations(model, test_x)
  File "test_cnn_128.py", line 45, in get_activations
    layer_outputs = [func(list_inputs)[0] for func in funcs]
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2666, in __call__
    return self._call(inputs)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2635, in _call
    session)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2587, in _make_callable
    callable_fn = session._make_callable_from_options(callable_opts)
  File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1414, in _make_callable_from_options
    return BaseSession._Callable(self, callable_options)
  File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1368, in __init__
    session._session, options_ptr, status)
  File "/home/fatima/.local/lib/python2.7/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: input_1:0 is both fed and fetched.
Exception tensorflow.python.framework.errors_impl.InvalidArgumentError: InvalidArgumentError() in <bound method _Callable.__del__ of <tensorflow.python.client.session._Callable object at 0x7f44de0cd210>> ignored

I am unsure of how to fix this.

Ralf
  • 16,086
  • 4
  • 44
  • 68
Nerd Giraffe
  • 131
  • 1
  • 2
  • 7
  • what tensorflow version are you using? – betelgeuse Oct 02 '18 at 11:57
  • @mnis version is 1.10.1 – Nerd Giraffe Oct 02 '18 at 13:29
  • 2
    I think the problem may be that your first layer is an `Input` layer representing the input, you are trying to extract its value while feeding it at the same time, which is not allowed by TensorFlow (if that's the case I am not sure why it worked in the referenced code, maybe they didn't start with `Input`?...). In the definition of `outputs`, try instead `outputs = [layer.output for layer in model.layers[1:] if ]`, that is taking all the layer outputs except the first one, and see if that works and produces all the hidden and output layer activations. – jdehesa Oct 02 '18 at 13:54
  • Still the same error @jdehesa – Nerd Giraffe Oct 02 '18 at 16:42
  • @NerdGiraffe That's interesting. Maybe you could try to generate the values in a loop, like `for func, layer in zip(funcs, model.layers):`, instead of a list comprehension, and print the `layer` before calling each `func`, so at least you have an idea if any of the layers can be evaluated and where it is failing. – jdehesa Oct 02 '18 at 16:47
  • did you manage to fix this error @NerdGiraffe?, I am facing the same problem – dm5 Feb 25 '19 at 21:37

2 Answers2

11

As I posted on the thread Keras, How to get the output of each layer?, the way to solve this is to replace the line

outputs = [
    layer.output
    for layer in model.layers
    if layer.name == layer_name or layer_name is None
]

with

outputs = [
    layer.output
    for layer in model.layers
    if layer.name == layer_name or layer_name is None
][1:]

...in order to skip the input layer.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
KamKam
  • 546
  • 6
  • 14
1

Exclude the input layer from your output/fetch layers (which is why the error says "fed & fetch" - can't get output(fetch) for input(feed) from the input layer (feed) itself)

    outputs = [layer.output for layer in model.layers if
               layer.name == layer_name or layer_name is None][1:]

OR by dropping layer name stuff altogether;

    outputs = [layer.output for layer in model.layers][1:]

should fix the issue (notice --> [1:] at the end which is the simple fix for your issue)

Rob Streeting
  • 1,675
  • 3
  • 16
  • 27
Chaturlal
  • 11
  • 1