albermax / innvestigate

A toolbox to iNNvestigate neural networks' predictions!
Other
1.25k stars 233 forks source link

Problem with running iNNvestigate with tf-2.0? #186

Closed JJGraff closed 2 years ago

JJGraff commented 4 years ago

Hi, First of all many thanks for this great package! I have been using iNNvestigate to perform LRP on keras models successfully. However, I recently upgraded tensorflow from version 1.14 to version 2.0 and it seems to cause problems when I try to use iNNvestigate on keras models with tf-2.0 backend. For instance, when I try to analyse a very simple toy model as follows:

import numpy as np

from keras.models import Sequential
from keras.layers import  Dense

import innvestigate

data_x = np.empty(shape=(1000,10))
data_y = np.empty(shape=(1000))
for i in range(1000):
    data_x[i] = np.random.rand((10))
    data_y[i] = np.random.random()

model = Sequential()
model.add(Dense(32,input_dim=10))
model.add(Dense(1,activation='sigmoid'))

model.compile(optimizer='adam', loss='mse')

model.fit(data_x,data_y,batch_size=64,epochs=5,validation_split=0.2)

lrp_analyser = innvestigate.create_analyzer("lrp.epsilon",model)
analysis = lrp_analyser.analyze(data_x[[600]])

I get the following error path for the last line:

Traceback (most recent call last):
  File "/media/joris/New Volume/PycharmProjects/synthData/Explanations/test.py", line 30, in <module>
    analysis = lrp_analyser.analyze(data_x[[600]])
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 473, in analyze
    self.create_analyzer_model()
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 411, in create_analyzer_model
    model, stop_analysis_at_tensors=stop_analysis_at_tensors)
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/analyzer/relevance_based/relevance_analyzer.py", line 453, in _create_analysis
    return super(LRP, self)._create_analysis(*args, **kwargs)
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 711, in _create_analysis
    return_all_reversed_tensors=return_all_reversed_tensors)
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 700, in _reverse_model
    return_all_reversed_tensors=return_all_reversed_tensors)
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py", line 922, in reverse_model
    reapply_on_copied_layers=reapply_on_copied_layers)
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py", line 551, in trace_model_execution
    if all([y in used_as_input for y in Ys]):
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py", line 551, in <listcomp>
    if all([y in used_as_input for y in Ys]):
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 757, in __bool__
    self._disallow_bool_casting()
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 526, in _disallow_bool_casting
    self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
  File "/media/joris/New Volume/PycharmProjects/synthData/venv/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 515, in _disallow_in_graph_mode
    " this function with @tf.function.".format(task))
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

It seems tf 2.0 does not support the condition 'if all([y in used_as_input for y in Ys])'. I have looked for solutions to this error but haven't found anything easily applicable to this case. Is this problem known for iNNvestigate and if so do you know if there is a fix for it? Or would you advise reverting back to tf 1.x?

Thanks in advance!

enryH commented 4 years ago

Yes, that's unfortunate. Albermax is currently checking if the transition to TF2 is straight forward or not. See the branch with the release candiate 0 for TF2. So the transition to TF2 is planned, but not yet realized...

JJGraff commented 4 years ago

Thanks for the reply; that's good to know! I'll just revert to TF1.x for now then and follow future updates.

7PintsOfCherryGarcia commented 4 years ago

Is there any more info on the transition to tf2? Tried to install innvestigate from branch feature/version2.0_rc0 but can not load the module:

Installation (In virtualenv):

    git clone https://github.com/albermax/innvestigate.git
    git checkout --track remotes/origin/feature/version2.0_rc0
    python setup.py install

Running:

    import innvestigate
~/Software/innvestigate/innvestigate/__init__.py in <module>
      1
----> 2 from . import analyzer
      3 from .analyzer import create_analyzer
      4 from .analyzer import NotAnalyzeableModelException
      5

~/Software/innvestigate/innvestigate/analyzer/__init__.py in <module>
      8 ###############################################################################
      9
---> 10 from .base import NotAnalyzeableModelException
     11 from .deeplift import DeepLIFTWrapper
     12 from .gradient_based import BaselineGradient

~/Software/innvestigate/innvestigate/analyzer/base.py in <module>
     19 from .. import layers as ilayers
     20 from .. import utils as iutils
---> 21 from ..utils.keras import checks as kchecks
     22 from ..utils.keras import graph as kgraph
     23

~/Software/innvestigate/innvestigate/utils/keras/checks.py in <module>
     10
     11 import inspect
---> 12 import tensorflow.python.keras.engine.network as keras_engine_network
     13 import tensorflow.keras.activations as keras_activations
     14 import tensorflow.keras.layers as keras_layers

AttributeError: module 'tensorflow' has no attribute 'python'

pip freeze

absl-py==0.9.0
astor==0.8.1
backcall==0.1.0
cachetools==4.0.0
certifi==2019.11.28
chardet==3.0.4
colorlover==0.3.0
cycler==0.10.0
decorator==4.4.2
future==0.18.2
gast==0.2.2
google-auth==1.11.2
google-auth-oauthlib==0.4.1
google-pasta==0.1.8
grpcio==1.27.2
h5py==2.10.0
idna==2.9
innvestigate==2.0.0
ipython==7.13.0
ipython-genutils==0.2.0
jedi==0.14.1
JellyBelly==0.0.0
joblib==0.14.1
Keras-Applications==1.0.8
Keras-Preprocessing==1.1.0
kiwisolver==1.1.0
llvmlite==0.31.0
Markdown==3.2.1
matplotlib==3.2.1
numba==0.48.0
numpy==1.18.1
oauthlib==3.1.0
opt-einsum==3.2.0
parso==0.6.2
pexpect==4.8.0
pickleshare==0.7.5
Pillow==7.0.0
pkg-resources==0.0.0
plotly==4.5.4
prompt-toolkit==3.0.4
protobuf==3.11.3
ptyprocess==0.6.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
Pygments==2.6.1
pyparsing==2.4.6
python-dateutil==2.8.1
requests==2.23.0
requests-oauthlib==1.3.0
retrying==1.3.3
rsa==4.0
scikit-learn==0.22.2.post1
scipy==1.4.1
six==1.14.0
tensorboard==2.1.1
tensorflow==2.1.0
tensorflow-estimator==2.1.0
termcolor==1.1.0
traitlets==4.3.3
umap-learn==0.3.10
urllib3==1.25.8
wcwidth==0.1.8
Werkzeug==1.0.0
wrapt==1.12.1
bmyzk commented 4 years ago

@7PintsOfCherryGarcia Following import sentence worked in my environment with branch updates_towards_tf2.0. from tensorflow.python.keras.engine import network as keras_engine_network

I really appreciate it if this awesome library is updated to support tf2!

adrhill commented 2 years ago

Hi @JJGraff, @7PintsOfCherryGarcia and @bmyzk,

we just released iNNvestigate 2.0 with support for TensorFlow 2! Please try it out and let me know if this fixes your issue. :)

JONAHKYAGABA commented 1 year ago

Hello @adrhill this issue has repeated in tensorflow 2.12.0 . The error i get is : No module named 'innvestigate.utils.keras' as i try to implement LRP in mobile nets

adrhill commented 1 year ago

Hi @JONAHKYAGABA, could you please open a new issue with your platform information and a full stack-trace? Ideally, a minimal code example to reproduce the error as well. Otherwise there isn't much I can do to help.

JONAHKYAGABA commented 1 year ago

Thank you for the ressponse @adrhill i have been occupied lately but am back and working on the project .Let me provide the issue.