question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting model activations with K.function doesn't work since v2.2.0

See original GitHub issue

Hi there,

I’m running into problems with getting the activations of a model using K.function since Keras 2.2.0. With 2.1.6, everything works fine. I’m using the tensorflow backend with a convnet with multiple outputs.

A (shortened) code snippet can be found below:

saved_model = ks.models.load_model('...') # the model has been saved with 2.2.0
inp = saved_model.input

generator = generate_batches_from_hdf5_file(...)
model_inputs, ys = next(generator) 
lp = 0. if learning_phase == 'test' else 1.

outputs = [layer.output for layer in saved_model.layers if
               layer.name == layer_name or layer_name is None]  # all layer outputs -> empty tf.tensors 

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

list_inputs = [model_inputs, lp]

layer_outputs = [func(list_inputs)[0] for func in funcs] <- throws an error since Keras 2.2.0
...

The last line yields the following error since 2.2.0:

File ".../visualization_tools.py", line 135, in get_activations_and_weights
    layer_outputs = [func(list_inputs)[0] for func in funcs]
  File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2661, in __call__
    return self._call(inputs)
  File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2630, in _call
    session)
  File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 2582, in _make_callable
    callable_fn = session._make_callable_from_options(callable_opts)
  File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1480, in _make_callable_from_options
    return BaseSession._Callable(self, callable_options)
  File "/home/hpc/capn/mppi033h/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1441, in __init__
    session._session, options_ptr, status)
  File "/home/hpc/capn/mppi033h/.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_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 0x7f35b1f20a10>> ignored

Any idea what could have changed in 2.2.0?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
yanboliangcommented, Sep 6, 2018

@ViaFerrata After checking your code, I found the input_layer was passed into both feed and fetch. This is illegal for TF since keras switch to use tf.Session._make_callable_from_options. You can insert outputs = outputs[1:] before

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

to remove the first input_layer from fetch, then it can work well.

0reactions
JennaEllicommented, Jul 26, 2018

Similar issue here too. I’m trying to run the example script for ‘Activation Maximization on VGGNet’.

Traceback (most recent call last): File “kvis.py”, line 24, in <module> img = visualize_activation(model, layer_idx, filter_indices=20) File “/home/ycc62267/.local/lib/python3.6/site-packages/vis/visualization/activation_maximization.py”, line 107, in visualize_activation return visualize_activation_with_losses(model.input, losses, seed_input, input_range, **optimizer_params) File “/home/ycc62267/.local/lib/python3.6/site-packages/vis/visualization/activation_maximization.py”, line 41, in visualize_activation_with_losses img = opt.minimize(**optimizer_params)[0] File “/home/ycc62267/.local/lib/python3.6/site-packages/vis/optimizer.py”, line 143, in minimize computed_values = self.compute_fn([seed_input, 0]) File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”, line 2661, in call return self._call(inputs) File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”, line 2630, in _call session) File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py”, line 2582, in _make_callable callable_fn = session._make_callable_from_options(callable_opts) File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, line 1480, in _make_callable_from_options return BaseSession._Callable(self, callable_options) File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, line 1441, in init session._session, options_ptr, status) File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/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_1:0 is both fed and fetched. Exception ignored in: <bound method BaseSession._Callable.del of <tensorflow.python.client.session.BaseSession._Callable object at 0x7f1417e97be0>> Traceback (most recent call last): File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, line 1464, in del self._session._session, self._handle, status) File “/dls_sw/apps/python/anaconda/1.7.0/64/envs/python3/lib/python3.6/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: No such callable handle: 139726883220512

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the activations after layer k with Keras?
To run a model after you've finished training it you need to use keras predict(). This will evaluate the graph, given your input...
Read more >
Creating Custom Activation Functions with Lambda Layers in ...
Learning to create a simple custom ReLU activation function using lambda layers in TensorFlow 2 ... In this article, we look at how...
Read more >
3 ways to create a Keras model with TensorFlow 2.0 ...
Our models.py contains three functions to build Keras/TensorFlow 2.0 models using the Sequential, Functional and Model subclassing APIs, ...
Read more >
tf.keras.Sequential | TensorFlow v2.11.0
Sequential groups a linear stack of layers into a tf.keras.Model. ... your Model cannot be run inside a tf.function . run_eagerly=True is not...
Read more >
TensorFlow 2 Tutorial: Get Started in Deep Learning with tf.keras
How to develop deep learning models with tf.keras ... of the model, configuring each layer with a number of nodes and activation function, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found