eli5-org / eli5

A library for debugging/inspecting machine learning classifiers and explaining their predictions
MIT License
260 stars 42 forks source link

eli5, keras image explanaition error #16

Open michael135 opened 2 years ago

michael135 commented 2 years ago

I've tried to reimporduce steps in: https://eli5.readthedocs.io/en/latest/tutorials/keras-image-classifiers.html

But after running:

eli5.show_prediction(model, np.expand_dims(sample_image, 0))

I've got the following error:


---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_6576/2951291009.py in <module>
      1 # import tensorflow as tf
      2 # tf.compat.v1.disable_eager_execution()
----> 3 eli5.show_prediction(model, np.expand_dims(shorts[0], 0))

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/ipython.py in show_prediction(estimator, doc, **kwargs)
    305     """
    306     format_kwargs, explain_kwargs = _split_kwargs(kwargs)
--> 307     expl = explain_prediction(estimator, doc, **explain_kwargs)
    308     if expl.image is not None:
    309         # dispatch to image display implementation

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/functools.py in wrapper(*args, **kw)
    873                             '1 positional argument')
    874 
--> 875         return dispatch(args[0].__class__)(*args, **kw)
    876 
    877     funcname = getattr(func, '__name__', 'singledispatch function')

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/explain_prediction.py in explain_prediction_keras(model, doc, targets, layer, image)
    109     # and no other processing
    110     if image is not None or _maybe_image(model, doc):
--> 111         return explain_prediction_keras_image(model,
    112                                               doc,
    113                                               image=image,

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/explain_prediction.py in explain_prediction_keras_image(model, doc, image, targets, layer)
    185     # https://github.com/ramprs/grad-cam/blob/master/misc/utils.lua
    186     # and https://github.com/ramprs/grad-cam/blob/master/classification.lua
--> 187     values = gradcam_backend(model, doc, targets, activation_layer)
    188     weights, activations, grads, predicted_idx, predicted_val = values
    189     heatmap = gradcam(weights, activations)

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/gradcam.py in gradcam_backend(model, doc, targets, activation_layer)
    113 
    114     # score for class w.r.p.t. activation layer
--> 115     grads = _calc_gradient(predicted_val, [activation_output])
    116 
    117     # Global Average Pooling of gradients to get the weights

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/gradcam.py in _calc_gradient(ys, xs)
    143     """
    144     # differentiate ys (scalar) with respect to each variable in xs
--> 145     grads = K.gradients(ys, xs)
    146 
    147     # grads gives a python list with a tensor (containing the derivatives) for each xs

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/keras/backend.py in gradients(loss, variables)
   4261       A gradients tensor.
   4262   """
-> 4263   return tf.compat.v1.gradients(
   4264       loss, variables, colocate_gradients_with_ops=True)
   4265 

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/tensorflow/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients)
    167   # pylint: disable=protected-access
    168   with ops.get_default_graph()._mutation_lock():
--> 169     return gradients_util._GradientsHelper(
    170         ys, xs, grad_ys, name, colocate_gradients_with_ops,
    171         gate_gradients, aggregation_method, stop_gradients,

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/tensorflow/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph)
    480   """Implementation of gradients()."""
    481   if context.executing_eagerly():
--> 482     raise RuntimeError("tf.gradients is not supported when eager execution "
    483                        "is enabled. Use tf.GradientTape instead.")
    484   if src_graph is None:

RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.

It seems, that there is some issue with Tensorflow 2.+, So I've tried to fix it by turning off Eager execution:

import tensorflow as tf
tf.compat.v1.disable_eager_execution()
eli5.show_prediction(model, np.expand_dims(sample_image, 0))

But I've got another error:

---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
/tmp/ipykernel_5991/2951291009.py in <module>
      1 # import tensorflow as tf
      2 # tf.compat.v1.disable_eager_execution()
----> 3 eli5.show_prediction(model, np.expand_dims(shorts[0], 0))

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/ipython.py in show_prediction(estimator, doc, **kwargs)
    305     """
    306     format_kwargs, explain_kwargs = _split_kwargs(kwargs)
--> 307     expl = explain_prediction(estimator, doc, **explain_kwargs)
    308     if expl.image is not None:
    309         # dispatch to image display implementation

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/functools.py in wrapper(*args, **kw)
    873                             '1 positional argument')
    874 
--> 875         return dispatch(args[0].__class__)(*args, **kw)
    876 
    877     funcname = getattr(func, '__name__', 'singledispatch function')

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/explain_prediction.py in explain_prediction_keras(model, doc, targets, layer, image)
    109     # and no other processing
    110     if image is not None or _maybe_image(model, doc):
--> 111         return explain_prediction_keras_image(model,
    112                                               doc,
    113                                               image=image,

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/explain_prediction.py in explain_prediction_keras_image(model, doc, image, targets, layer)
    185     # https://github.com/ramprs/grad-cam/blob/master/misc/utils.lua
    186     # and https://github.com/ramprs/grad-cam/blob/master/classification.lua
--> 187     values = gradcam_backend(model, doc, targets, activation_layer)
    188     weights, activations, grads, predicted_idx, predicted_val = values
    189     heatmap = gradcam(weights, activations)

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/gradcam.py in gradcam_backend(model, doc, targets, activation_layer)
    106     """
    107     # score for class in targets
--> 108     predicted_idx = _get_target_prediction(targets, model)
    109     predicted_val = K.gather(model.output[0,:], predicted_idx) # access value by index
    110 

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/eli5/keras/gradcam.py in _get_target_prediction(targets, model)
    183                              '{}'.format(targets))
    184     elif targets is None:
--> 185         predicted_idx = K.argmax(model.output, axis=-1)
    186     else:
    187         raise TypeError('Invalid argument "targets" (must be list or None): %s' % targets)

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
    151     except Exception as e:
    152       filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153       raise e.with_traceback(filtered_tb) from None
    154     finally:
    155       del filtered_tb

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/keras/backend.py in argmax(x, axis)
   2628       A tensor.
   2629   """
-> 2630   return tf.argmax(x, axis)
   2631 
   2632 

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/keras/layers/core/tf_op_layer.py in handle(self, op, args, kwargs)
    105         isinstance(x, keras_tensor.KerasTensor)
    106         for x in tf.nest.flatten([args, kwargs])):
--> 107       return TFOpLambda(op)(*args, **kwargs)
    108     else:
    109       return self.NOT_SUPPORTED

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/keras/engine/base_layer_v1.py in __call__(self, *args, **kwargs)
    809           with autocast_variable.enable_auto_cast_variables(
    810               self._compute_dtype_object):
--> 811             outputs = self.call(cast_inputs, *args, **kwargs)
    812           self._handle_activity_regularization(inputs, outputs)
    813           self._set_mask_metadata(inputs, outputs, input_masks)

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/keras/layers/core/tf_op_layer.py in _call_wrapper(*args, **kwargs)
    224     # Decorate the function to produce this layer's call method
    225     def _call_wrapper(*args, **kwargs):
--> 226       return self._call_wrapper(*args, **kwargs)
    227 
    228     self.call = tf.__internal__.decorator.make_decorator(

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/keras/layers/core/tf_op_layer.py in _call_wrapper(self, *args, **kwargs)
    259       # multiple ops w/ the same name when the layer is reused)
    260       kwargs.pop('name', None)
--> 261       result = self.function(*args, **kwargs)
    262     self._check_variables(created_variables, tape.watched_variables())
    263     return result

... last 4 frames repeated, from the frame below ...

~/anaconda3/envs/tensorflow2_p38/lib/python3.8/site-packages/keras/layers/core/tf_op_layer.py in handle(self, op, args, kwargs)
    105         isinstance(x, keras_tensor.KerasTensor)
    106         for x in tf.nest.flatten([args, kwargs])):
--> 107       return TFOpLambda(op)(*args, **kwargs)
    108     else:
    109       return self.NOT_SUPPORTED

RecursionError: maximum recursion depth exceeded while calling a Python object
parthplc commented 2 years ago

I am also getting error for the notebook

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
c:\Projects\bluealtair\model_interpret\eli5\keras-image-classifiers.ipynb Cell 5' in <cell line: 2>()
      [1](vscode-notebook-cell:/c%3A/Projects/bluealtair/model_interpret/eli5/keras-image-classifiers.ipynb#ch0000061?line=0) from tensorflow import keras
----> [2](vscode-notebook-cell:/c%3A/Projects/bluealtair/model_interpret/eli5/keras-image-classifiers.ipynb#ch0000061?line=1) from keras.applications import mobilenet_v2

File c:\Users\Parth.chokhra\Miniconda3\lib\site-packages\keras\__init__.py:4, in <module>
      [1](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/__init__.py?line=0) from __future__ import absolute_import
      [3](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/__init__.py?line=2) from . import utils
----> [4](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/__init__.py?line=3) from . import activations
      [5](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/__init__.py?line=4) from . import applications
      [6](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/__init__.py?line=5) from . import backend

File c:\Users\Parth.chokhra\Miniconda3\lib\site-packages\keras\activations.py:6, in <module>
      [4](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=3) from . import backend as K
      [5](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=4) from .utils.generic_utils import deserialize_keras_object
----> [6](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=5) from .engine import Layer
[9](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=8) def softmax(x, axis=-1):
     [10](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=9)     """Softmax activation function.
     [11](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=10) 
     [12](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=11)     # Arguments
   (...)
     [20](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=19)         ValueError: In case `dim(x) == 1`.
     [21](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/activations.py?line=20)     """

File c:\Users\Parth.chokhra\Miniconda3\lib\site-packages\keras\engine\__init__.py:8, in <module>
      [6](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/__init__.py?line=5) from .topology import Layer
      [7](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/__init__.py?line=6) from .topology import get_source_inputs
----> [8](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/__init__.py?line=7) from .training import Model

File c:\Users\Parth.chokhra\Miniconda3\lib\site-packages\keras\engine\training.py:25, in <module>
     [23](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/training.py?line=22) from .. import metrics as metrics_module
     [24](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/training.py?line=23) from ..utils.generic_utils import Progbar
---> [25](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/training.py?line=24) from .. import callbacks as cbks
[26](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/training.py?line=25) from ..legacy import interfaces
     [29](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/training.py?line=28) def _standardize_input_data(data, names, shapes=None,
     [30](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/training.py?line=29)                             check_batch_axis=True,
     [31](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/engine/training.py?line=30)                             exception_prefix=''):

File c:\Users\Parth.chokhra\Miniconda3\lib\site-packages\keras\callbacks.py:26, in <module>
     [24](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=23) if K.backend() == 'tensorflow':
     [25](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=24)     import tensorflow as tf
---> [26](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=25)     from tensorflow.contrib.tensorboard.plugins import projector
     [29](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=28) class CallbackList(object):
     [30](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=29)     """Container abstracting a list of callbacks.
     [31](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=30) 
     [32](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=31)     # Arguments
   (...)
     [35](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=34)             running statistics over callback execution time.
     [36](file:///c%3A/Users/Parth.chokhra/Miniconda3/lib/site-packages/keras/callbacks.py?line=35)     """

ModuleNotFoundError: No module named 'tensorflow.contrib'