albermax / innvestigate

A toolbox to iNNvestigate neural networks' predictions!
Other
1.24k stars 235 forks source link

[BUG] KeyError: 2670675173184. #293

Closed OliverZar closed 1 year ago

OliverZar commented 1 year ago

Describe the bug

I have a problem with certain architectures, that when I try to create 2 parsers, it gives me this error, and only in some cases, when I restart the jupyter server, it allows me to run them. I get this error: KeyError: 2670675173184.


import cv2
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
# Eligiendo la imagen a analizar:

width_shape = 224
height_shape = 224

imaget_path = "Cells/test5/BCELL/BCELL_14.png"
imaget= cv2.resize(cv2.imread(imaget_path),(width_shape, height_shape), interpolation = cv2.INTER_AREA)

xt = np.asarray(imaget)
xt=preprocess_input(xt)
xt = np.expand_dims(xt,axis=0)

plot.imshow(xt.squeeze(),cmap="seismic",  clim=(-1, 1))

#Test con la red ModelCNN
from keras.models import load_model
import tensorflow as tf

#CnnModel = load_model('CNN_model.h5')
CnnModel = load_model('Model_MobileTransfer.h5')

# Eliminación de la capa de activación softmax del modelo
model_wo_sm = innvestigate.model_wo_softmax(CnnModel)

# Creando el analizador
gradient_analyzer = innvestigate.analyzer.Gradient(model_wo_sm)

# Aplicando el analizador
analysis = gradient_analyzer.analyze(xt)

# Aggregate along color channels and normalize to [-1, 1]
analysis = analysis.sum(axis=np.argmax(np.asarray(analysis.shape) == 3))
analysis /= np.max(np.abs(analysis))

# Plot
#plot.imshow(analysis.squeeze(), cmap="seismic", interpolation="nearest")
plot.imshow(analysis[0], cmap="seismic", clim=(-1, 1))

plot.show()

analyzer = innvestigate.create_analyzer("deep_taylor", model_wo_sm)
a = analyzer.analyze(xt)

# Aggregate along color channels and normalize to [-1, 1]
a = a.sum(axis=np.argmax(np.asarray(a.shape) == 3))
a /= np.max(np.abs(a))

# Plot
plot.imshow(a[0], cmap="seismic", clim=(-1, 1))

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots


KeyError Traceback (most recent call last)

Input In [7], in <cell line: 2>() 1 analyzer = innvestigate.create_analyzer("deep_taylor", model_wo_sm) ----> 2 a = analyzer.analyze(xt) 4 # Aggregate along color channels and normalize to [-1, 1] 5 a = a.sum(axis=np.argmax(np.asarray(a.shape) == 3))

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\analyzer\network_base.py:250, in AnalyzerNetworkBase.analyze(self, X, neuron_selection) 247 # TODO: what does should mean in docstring? 249 if self._analyzer_model_done is False: --> 250 self.create_analyzer_model() 252 if neuron_selection is not None and self._neuron_selection_mode != "index": 253 raise ValueError( 254 f"neuron_selection_mode {self._neuron_selection_mode} doesn't support ", 255 "'neuron_selection' parameter.", 256 )

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\analyzer\network_base.py:164, in AnalyzerNetworkBase.create_analyzer_model(self) 161 self._analysis_inputs = analysis_inputs 162 self._prepared_model = model --> 164 tmp = self._create_analysis( 165 model, stop_analysis_at_tensors=stop_analysis_at_tensors 166 ) 167 if isinstance(tmp, tuple): 168 if len(tmp) == 3:

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\analyzer\deeptaylor.py:134, in DeepTaylor._create_analysis(self, *args, *kwargs) 110 # Layers w/o transformation 111 self._add_conditional_reverse_mapping( 112 lambda l: isinstance( 113 l, (...) 131 name="deep_taylor_no_transform", 132 ) --> 134 return super()._create_analysis(args, **kwargs)

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\analyzer\reverse_base.py:269, in ReverseAnalyzerBase._create_analysis(self, model, stop_analysis_at_tensors) 261 return_all_reversed_tensors = ( 262 self._reverse_check_min_max_values 263 or self._reverse_check_finite 264 or self._reverse_keep_tensors 265 ) 267 # if return_all_reversed_tensors is False, 268 # reversed_tensors will be None --> 269 reversed_input_tensors, reversed_tensors = self._reverse_model( 270 model, 271 stop_analysis_at_tensors=stop_analysis_at_tensors, 272 return_all_reversed_tensors=return_all_reversed_tensors, 273 ) 274 ret = self._postprocess_analysis(reversed_input_tensors) 276 if return_all_reversed_tensors:

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\analyzer\reverse_base.py:242, in ReverseAnalyzerBase._reverse_model(self, model, stop_analysis_at_tensors, return_all_reversed_tensors) 239 if stop_analysis_at_tensors is None: 240 stop_analysis_at_tensors = [] --> 242 return igraph.reverse_model( 243 model, 244 reverse_mappings=self._reverse_mapping, 245 default_reverse_mapping=self._default_reverse_mapping, 246 head_mapping=self._head_mapping, 247 stop_mapping_at_tensors=stop_analysis_at_tensors, 248 verbose=self._reverse_verbose, 249 clip_all_reversed_tensors=self._reverse_clip_values, 250 project_bottleneck_tensors=self._reverse_project_bottleneck_layers, 251 return_all_reversed_tensors=return_all_reversed_tensors, 252 )

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\backend\graph.py:1252, in reverse_model(model, reverse_mappings, default_reverse_mapping, head_mapping, stop_mapping_at_tensors, verbose, return_all_reversed_tensors, clip_all_reversed_tensors, project_bottleneck_tensors, execution_trace, reapply_on_copied_layers) 1249 add_reversed_tensors(nid, Xs, reversed_Xs) 1251 # Return requested values -> 1252 reversed_input_tensors = [ 1253 get_reversed_tensor(tmp) 1254 for tmp in model.inputs 1255 if id(tmp) not in stop_mapping_at_ids 1256 ] 1257 if return_all_reversed_tensors is True: 1258 return reversed_input_tensors, reversed_tensors

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\backend\graph.py:1253, in (.0) 1249 add_reversed_tensors(nid, Xs, reversed_Xs) 1251 # Return requested values 1252 reversed_input_tensors = [ -> 1253 get_reversed_tensor(tmp) 1254 for tmp in model.inputs 1255 if id(tmp) not in stop_mapping_at_ids 1256 ] 1257 if return_all_reversed_tensors is True: 1258 return reversed_input_tensors, reversed_tensors

File C:\ProgramData\Anaconda3\envs\TestNweEntorno\lib\site-packages\innvestigate\backend\graph.py:1104, in reverse_model..get_reversed_tensor(tensor) 1102 def get_reversed_tensor(tensor: Tensor) -> Tensor: 1103 tmp: ReverseTensorDict -> 1104 tmp = reversed_tensors[id(tensor)] 1106 if tmp["final_tensor"] is None: 1107 if len(tmp["tensors"]) == 1:

KeyError: 2670675173184

Platform information

adrhill commented 1 year ago

Hi @OliverZar, can you tell me a bit more about your model so I can narrow down the issue? What kinds of layers does it contain?

OliverZar commented 1 year ago

Hola@OliverZar, ¿puede decirme un poco más sobre su modelo para que pueda reducir el problema? ¿Qué tipo de capas contiene?


These are the layers of my model. It has ReLU activations. I manage to get images with the Gradient method, later I have to restart the jupyter server so that I can get results with deep taylor. In the same way it happens to me to obtain results with a Resnet50 architecture.

Model: "model_7"


Layer (type) Output Shape Param #

input_1 (InputLayer) [(None, 224, 224, 3)] 0

conv1 (Conv2D) (None, 112, 112, 32) 864

conv1_bn (BatchNormalizatio (None, 112, 112, 32) 128
n)

conv1_relu (ReLU) (None, 112, 112, 32) 0

conv_dw_1 (DepthwiseConv2D) (None, 112, 112, 32) 288

conv_dw_1_bn (BatchNormaliz (None, 112, 112, 32) 128
ation)

conv_dw_1_relu (ReLU) (None, 112, 112, 32) 0

conv_pw_1 (Conv2D) (None, 112, 112, 64) 2048

conv_pw_1_bn (BatchNormaliz (None, 112, 112, 64) 256
ation)

flatten (Flatten) (None, 802816) 0

fc1 (Dense) (None, 128) 102760576

dropout_40 (Dropout) (None, 128) 0

fc2 (Dense) (None, 128) 16512

dropout_41 (Dropout) (None, 128) 0

fc3 (Dense) (None, 128) 16512

dropout_42 (Dropout) (None, 128) 0

fc4 (Dense) (None, 128) 16512

dropout_43 (Dropout) (None, 128) 0

output (Dense) (None, 3) 387

adrhill commented 1 year ago

Did you use tf.compat.v1.disable_eager_execution()?

This could be related to #292 due to the use of BatchNormalization layers.