albermax / innvestigate

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

Own pretrained model does not work with 'model_wo_softmax' #177

Closed Hryniewska closed 2 years ago

Hryniewska commented 4 years ago

I wanted to use my pretrained model. It does not have duplicated layers, but I received the following error:

Traceback (most recent call last): File "/.../ARA-CNN/src/innvestigate_test.py", line 85, in model = innvestigate.utils.model_wo_softmax(model) File "/.../ARA-CNN/venv/lib/python3.6/site-packages/innvestigate/utils/init.py", line 37, in model_wo_softmax return model_wo_softmax(*args, kwargs) File "/.../ARA-CNN/venv/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py", line 387, in model_wo_softmax name=model.name) File "/.../ARA-CNN/venv/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, *kwargs) File "/.../ARA-CNN/venv/lib/python3.6/site-packages/keras/engine/network.py", line 93, in init self._init_graph_network(args, kwargs) File "/.../ARA-CNN/venv/lib/python3.6/site-packages/keras/engine/network.py", line 231, in _init_graph_network self.inputs, self.outputs) File "/.../ARA-CNN/venv/lib/python3.6/site-packages/keras/engine/network.py", line 1455, in _map_graph_network ' times in the model. ' ValueError: The name "dense_2" is used 2 times in the model. All layer names should be unique.

Can anybody help me, how to fix it, please?

enryH commented 4 years ago

If you do it in a notebook, you could try to run it several times. Then you continuously progress through the layers and it will eventuelly work. Do you use standalone keras 2.4.4?.

alenaschnurr commented 4 years ago

Hey, I got the same error and the problem seems to be that when removing the softmax the new copied layer that is added has an preexisting name. The error can be avoided by changing copy_layer_wo_activation:

    config = layer.get_config()
    if name_template is None:
        # config["name"] = None
        pass
    else:
        config["name"] = name_template % config["name"]
fhvilshoj commented 4 years ago

I also had this issue. In my case, the issue is that Keras has "running counters" on layer names. In the same vein as @enryH, the layers need to get unique names (different from the ones in the original model).

When a model is copied, the new layers get names using such counters. If the model which is being copied was loaded from a keras model file, then all these counters will be 0 and the loaded model and copied model will end up with the same ids.

Minimal working example of the issue and the solution, respectively, is the following:

model = keras.models.load(model_path)
model_wo_softmax = innvestigate.utils.keras.graph.model_wo_softmax(model)

Building the model first (even though you might not use it) solves the problem.

model = keras.models.Sequential([ ... ])  # Same code as used to build the original model
model = keras.models.load(model_path)
model_wo_softmax = innvestigate.utils.keras.graph.model_wo_softmax(model)

I admit that it is hacky, but it is a oneliner after all ;-)

adrhill commented 2 years ago

Hi @Hryniewska,

I'm closing this issue since we just released an overhauled iNNvestigate 2.0 with support for TensorFlow 2! Please try it out and let me know if this fixes your issue. :)