Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Mutation warning being printed at every layer of EfficientNet when running LRP analyzer #324

Open
palatyle opened this issue Nov 22, 2023 · 1 comment
Labels
triage Bug report that needs assessment

Comments

@palatyle
Copy link

Describe the bug

Running any LRP analyzer, I get tensorflow warnings in the format of something like:
2023-11-22 10:49:33.032371: W tensorflow/c/c_api.cc:305] Operation '{name:'mul_1355/x' id:43796 op device:{requested: '', assigned: ''} def:{{{node mul_1355/x}} = Const[_has_manual_control_dependencies=true, dtype=DT_FLOAT, value=Tensor<type: float shape: [] values: 1e-07>]()}}' was changed by setting attribute after it was run by a session. This mutation will have no effect, and will trigger an error in the future. Either don't modify nodes after running them or create a new session.

Being printed at what looks to be every single layer in the EfficientNet model I'm using. It also runs very slowly, taking about 15 minutes (longer if I use the sequential presets) to analyze the model. Is this runtime normal due to the size of the network? Can I safely ignore these warnings or is something actually wrong?

Steps to reproduce the bug

NOTE: I had to implement juliowissing-iis's fix here for this code block to run without errors.

import tensorflow as tf
import tensorflow.keras.utils as ku
import numpy as np
import innvestigate as inn
from tensorflow.keras.applications import EfficientNetB5
import time

tf.compat.v1.disable_eager_execution()

# create EfficientNet B5 model with imagenet weights
model = EfficientNetB5(weights='imagenet')

# Read in data
img = ku.load_img('img.jpg',target_size = (456,456))
# Conver1 to numpy array
img_arr = ku.img_to_array(img)
# Add in extra 1st dim
img_CNN = np.array([img_arr])

# Remove last softmax layer (required for INNvestigate)
model_wo_softmax = inn.model_wo_softmax(model)

# Create LRP epsilon analyzer
analyzer = inn.create_analyzer("lrp.epsilon", model_wo_softmax,**{'epsilon':1})

# Analyze input image
t0=time.time()
a = analyzer.analyze(img_CNN)
t1=time.time()

total_time = t1-t0
print(f'Anaylzer took {total_time} seconds')

Expected behavior

Minimal mutation warnings and a code that doesn't take 15 minutes to run.

Platform information

  • OS: Mac OSX 13.6.1 (this behavior also occurs on CentOS 7)
  • Python version: 3.10.13
  • iNNvestigate version: 2.1.2
  • TensorFlow version: 2.14.0
@palatyle palatyle added the triage Bug report that needs assessment label Nov 22, 2023
@dsucuzhanay
Copy link

dsucuzhanay commented Jul 31, 2024

It is because of the tf.compat.v1.disable_eager_execution(). I am working with EfficientNetV2B0 and the analyze method takes to much time to execute.

Even worst on my side, using the following code: inn.create_analyzer("lrp.epsilon", model_wo_softmax,**{'epsilon':1}) returns an error related with LRP only supports convolutional layers (EfficientNetV2B0 has DepthwiseConv2D layers).

I could execute LRP analysis with the following code:

from innvestigate.analyzer import LRP
analyzer = LRP(model_wo_softmax, rule="Epsilon", **{'reverse_verbose': True})

But I had to do a change in the library code (file: relevance_analyzer.py; line:188):

self._axis = axes_number_in_batch_normalization_layer #integer

the original code is getting this value from config["axis"], but in EfficientnetV2B0 the value of config["axis"] is a list (i.e. 'axis': ListWrapper([3])).

Of course, modifying the library code is not a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Bug report that needs assessment
Projects
None yet
Development

No branches or pull requests

2 participants