albermax / innvestigate

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

AttributeError: 'Node' object has no attribute 'output_masks' #165

Closed lorenzocesconetto closed 2 years ago

lorenzocesconetto commented 5 years ago

I've been getting the error below, and I just can't get around it. This is my code:

import tensorflow from tensorflow.keras.models import load_model import innvestigate from vis.visualization import visualize_activation from vis.utils import utils

mobile_net = load_model('/mobile_net_v1.h5', compile=False)

img_1 = plt.imread('my_img.jpg')

img_1_array = np.expand_dims(img_1, axis=0) analyzer = innvestigate.create_analyzer("gradient", mobile_net)

Doesn't work either way below

analysis = analyzer.analyze(img_1_array) analysis = analyzer.analyze(img_1)

tensorflow.version

2.0.0

innvestigate.version

1.0.8

Error output


AttributeError Traceback (most recent call last)

in 1 # analysis = analyzer.analyze(img_1_array) ----> 2 analysis = analyzer.analyze(img_1) ~/Desktop/ITAU-ANALYTICS/Skin-Cancer/skin_env/lib/python3.7/site-packages/innvestigate/analyzer/base.py in analyze(self, X, neuron_selection) 471 """ 472 if not hasattr(self, "_analyzer_model"): --> 473 self.create_analyzer_model() 474 475 X = iutils.to_list(X) ~/Desktop/ITAU-ANALYTICS/Skin-Cancer/skin_env/lib/python3.7/site-packages/innvestigate/analyzer/base.py in create_analyzer_model(self) 403 """ 404 model_inputs = self._model.inputs --> 405 tmp = self._prepare_model(self._model) 406 model, analysis_inputs, stop_analysis_at_tensors = tmp 407 self._analysis_inputs = analysis_inputs ~/Desktop/ITAU-ANALYTICS/Skin-Cancer/skin_env/lib/python3.7/site-packages/innvestigate/analyzer/base.py in _prepare_model(self, model) 373 if neuron_selection_mode == "max_activation": 374 l = ilayers.Max(name="iNNvestigate_max") --> 375 model_output = l(model_output) 376 self._special_helper_layers.append(l) 377 elif neuron_selection_mode == "index": ~/Desktop/ITAU-ANALYTICS/Skin-Cancer/skin_env/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs) 473 474 # Handle mask propagation. --> 475 previous_mask = _collect_previous_mask(inputs) 476 user_kwargs = kwargs.copy() 477 if not is_all_none(previous_mask): ~/Desktop/ITAU-ANALYTICS/Skin-Cancer/skin_env/lib/python3.7/site-packages/keras/engine/base_layer.py in _collect_previous_mask(input_tensors) 1439 inbound_layer, node_index, tensor_index = x._keras_history 1440 node = inbound_layer._inbound_nodes[node_index] -> 1441 mask = node.output_masks[tensor_index] 1442 masks.append(mask) 1443 else: AttributeError: 'Node' object has no attribute 'output_masks'
simeier commented 4 years ago

Hi, i had the same error recently and figured out, that innvestigate doesn't support tf.keras (as far as i know). So you might need to recreate the model in keras: https://github.com/albermax/innvestigate/issues/175 and https://github.com/albermax/innvestigate/issues/159

ambitiousrahul commented 2 years ago

can somebody please help .. I am still getting a similar error after installing innvestigate and trying to import the library . I am using tensorflow 2.10.1 Error ImportError: cannot import name 'Node' from 'keras.engine.base_layer' (/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py)

image
adrhill commented 2 years ago

Hi @ambitiousrahul, it looks like you're using an outdated version of iNNvestigate, which isn't compatible with TF2. Please update iNNvestigate to the latest version:

pip install innvestigate==2.0.1 

I'm closing this issue as it most likely isn't relevant for iNNvestigate 2.0 anymore. Please open a new issue in case the problem persists.