raghakot / keras-vis

Neural network visualization toolkit for keras
https://raghakot.github.io/keras-vis
MIT License
2.98k stars 664 forks source link

Attention on ResNet50 notebook #159

Closed waspinator closed 5 years ago

waspinator commented 5 years ago

Are the notebooks in the example folder still working?

I have tried to use 0.4.1 and master. Tensorflow 1.12, python 3.5.

When running the third block I get the following error:

from vis.visualization import visualize_saliency, overlay
from vis.utils import utils
from keras import activations

# Utility to search for layer index by name. 
# Alternatively we can specify this as -1 since it corresponds to the last layer.
layer_idx = utils.find_layer_idx(model, 'fc1000')

f, ax = plt.subplots(1, 2)
for i, img in enumerate([img1, img2]):    
    # 20 is the imagenet index corresponding to `ouzel`
    grads = visualize_saliency(model, layer_idx, filter_indices=20, seed_input=img)

    # visualize grads as heatmap
    ax[i].imshow(grads, cmap='jet')

Error:

InvalidArgumentErrorTraceback (most recent call last)
<ipython-input-3-24d4c03cb474> in <module>
     10 for i, img in enumerate([img1, img2]):
     11     # 20 is the imagenet index corresponding to `ouzel`
---> 12     grads = visualize_saliency(model, layer_idx, filter_indices=20, seed_input=img)
     13 
     14     # visualize grads as heatmap

/usr/local/lib/python3.5/dist-packages/vis/visualization/saliency.py in visualize_saliency(model, layer_idx, filter_indices, seed_input, backprop_modifier, grad_modifier)
    123         (ActivationMaximization(model.layers[layer_idx], filter_indices), -1)
    124     ]
--> 125     return visualize_saliency_with_losses(model.input, losses, seed_input, grad_modifier)
    126 
    127 

/usr/local/lib/python3.5/dist-packages/vis/visualization/saliency.py in visualize_saliency_with_losses(input_tensor, losses, seed_input, grad_modifier)
     71     """
     72     opt = Optimizer(input_tensor, losses, norm_grads=False)
---> 73     grads = opt.minimize(seed_input=seed_input, max_iter=1, grad_modifier=grad_modifier, verbose=False)[1]
     74 
     75     channel_idx = 1 if K.image_data_format() == 'channels_first' else -1

/usr/local/lib/python3.5/dist-packages/vis/optimizer.py in minimize(self, seed_input, max_iter, input_modifiers, grad_modifier, callbacks, verbose)
    141 
    142             # 0 learning phase for 'test'
--> 143             computed_values = self.compute_fn([seed_input, 0])
    144             losses = computed_values[:len(self.loss_names)]
    145             named_losses = zip(self.loss_names, losses)

/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2713                 return self._legacy_call(inputs)
   2714 
-> 2715             return self._call(inputs)
   2716         else:
   2717             if py_any(is_tensor(x) for x in inputs):

/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
   2669                                 feed_symbols,
   2670                                 symbol_vals,
-> 2671                                 session)
   2672         if self.run_metadata:
   2673             fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)

/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in _make_callable(self, feed_arrays, feed_symbols, symbol_vals, session)
   2621             callable_opts.run_options.CopyFrom(self.run_options)
   2622         # Create callable.
-> 2623         callable_fn = session._make_callable_from_options(callable_opts)
   2624         # Cache parameters corresponding to the generated callable, so that
   2625         # we can detect future mismatches and refresh the callable.

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _make_callable_from_options(self, callable_options)
   1412     """
   1413     self._extend_graph()
-> 1414     return BaseSession._Callable(self, callable_options)
   1415 
   1416 

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in __init__(self, session, callable_options)
   1366         with errors.raise_exception_on_not_ok_status() as status:
   1367           self._handle = tf_session.TF_SessionMakeCallable(
-> 1368               session._session, options_ptr, status)
   1369       finally:
   1370         tf_session.TF_DeleteBuffer(options_ptr)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    517             None, None,
    518             compat.as_text(c_api.TF_Message(self.status.status)),
--> 519             c_api.TF_GetCode(self.status.status))
    520     # Delete the underlying status object from memory otherwise it stays alive
    521     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: input_1_1:0 is both fed and fetched.
xiaohk commented 5 years ago

I got the same error for my own model. How did you solve it?

waspinator commented 5 years ago

I don't remember exactly, but I think it was that I had to model.compile(). Still not working super well from what I remember though.

keisen commented 5 years ago

Hi there.

The following command solve the problem. You should try it :smiley:

pip install git+https://github.com/raghakot/keras-vis.git -U