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

All layer names should be unique. #130

Closed
Tahedi1 opened this issue Jan 24, 2019 · 10 comments
Closed

All layer names should be unique. #130

Tahedi1 opened this issue Jan 24, 2019 · 10 comments

Comments

@Tahedi1
Copy link

Tahedi1 commented Jan 24, 2019

I tested the package methods with VGG16 and it works. But, I have tested it with trained InceptionV3 model constructed in separate train script as following:

base_model = InceptionV3(include_top=False, weights='imagenet',input_shape = (299,299,3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.2)(x)
predictions = Dense(nbr_classes, activation='softmax')(x)
model = Model(input=base_model.input, output=predictions)

And loaded in visualisation script as following (I based on imagenet_compare_methods.ipynb) :

model_path = 'path_of_model'
model_weights_path = 'path_of_model_weights'
model = load_model(model_path)
model.load_weights(model_weights_path)
model = keras.models.Model(model.inputs, model.outputs)
model.compile(optimizer="adam", loss="categorical_crossentropy")
preprocess_f=keras.applications.inception_v3.preprocess_input
color_coding="RGB"
channels_first = keras.backend.image_data_format() == "channels_first"
color_conversion = "BGRtoRGB" if color_coding == "BGR" else None
input_range = (-1,1)
noise_scale = (input_range[1]-input_range[0]) * 0.1

I got the following error with iutils.keras.graph.model_wo_softmax function:

Traceback (most recent call last):
  File "imagenet_compare_methods_incepV3.py", line 91, in <module>
    model_wo_softmax = iutils.keras.graph.model_wo_softmax(model)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/innvestigate/utils/keras/graph.py", line 312, in model_wo_softmax
    name=model.name)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 231, in _init_graph_network
    self.inputs, self.outputs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 1455, in _map_graph_network
    ' times in the model. '
ValueError: The name "dense_1" is used 2 times in the model. All layer names should be unique.

To handle this error I have modified the names of my dense layers and I added this code:

model.layers[-3].name='dense1'
model.layers[-1].name='dense_final'

These methodes [Input, Gradient] work proprly and SmoothGrad works but with out of memory warning. But with Guided Backprop I got an another error :

Traceback (most recent call last):
  File "imagenet_compare_methods_incepV3.py", line 133, in <module>
    a = analyzer.analyze(x_pp)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 479, in analyze
    self.create_analyzer_model()
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 443, in create_analyzer_model
    outputs=analysis_outputs+debug_outputs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 231, in _init_graph_network
    self.inputs, self.outputs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 1455, in _map_graph_network
    ' times in the model. '
ValueError: The name "activation_1" is used 2 times in the model. All layer names should be unique.

Please help me to solve this issue.

@albermax
Copy link
Owner

How about:

base_model = InceptionV3(include_top=False, weights='imagenet',input_shape = (299,299,3))
x = base_model.output
x = GlobalAveragePooling2D(name='special_name_01')(x)
x = Dense(1024, activation='relu', name='special_name_02')(x)
x = Dropout(0.2, name='special_name_03')(x)
predictions = Dense(nbr_classes, activation='softmax', name='special_name_04')(x)
model = Model(input=base_model.input, output=predictions)

@Tahedi1
Copy link
Author

Tahedi1 commented Jan 24, 2019

I will retrain the model as you suggested and I will test the script.
The problem is I have already many trained model and I would like to use them directly. I have examined all layers names and I found them with differents names.
I dont understand why this error is appeared.

@albermax
Copy link
Owner

Retraining will not help you, I did also not suggest a that.
If you look I suggested to change the name of the layers before you apply them.

The error appears because Keras does accidentally reuse layer names.

@Tahedi1
Copy link
Author

Tahedi1 commented Jan 24, 2019

Ok, albermax. I will test your proposition.

@Tahedi1
Copy link
Author

Tahedi1 commented Jan 24, 2019

In ordre to modify layers name as you sugested :

model.layers[-1].name='special_name_04'
model.layers[-2].name='special_name_03'
model.layers[-3].name='special_name_02'
model.layers[-4].name='special_name_01'

But I have the same error:

Traceback (most recent call last):
  File "visio_incepV3.py", line 134, in <module>
    a = analyzer.analyze(x_pp)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 479, in analyze
    self.create_analyzer_model()
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/innvestigate/analyzer/base.py", line 443, in create_analyzer_model
    outputs=analysis_outputs+debug_outputs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 231, in _init_graph_network
    self.inputs, self.outputs)
  File "/home_nfs/brahimim/.conda/envs/mohammed.tf.1.9.0/lib/python3.6/site-packages/keras/engine/network.py", line 1455, in _map_graph_network
    ' times in the model. '
ValueError: The name "activation_1" is used 2 times in the model. All layer names should be unique.

@albermax
Copy link
Owner

I am afraid but this is not what I suggested.
This worked for me:

base_model = InceptionV3(include_top=False, weights='imagenet',input_shape = (299,299,3))
x = base_model.output
x = GlobalAveragePooling2D(name='special_name_01')(x)
x = Dense(1024, activation='relu', name='special_name_02')(x)
x = Dropout(0.2, name='special_name_03')(x)
predictions = Dense(nbr_classes, activation='softmax', name='special_name_04')(x)
model = Model(input=base_model.input, output=predictions)
# workaround to get unique names
for l in model.layers:
  l.name = "%s_workaround" % l.name
# Create model with new names
model = Model(input=model.input, output=model.output)

Hope this helps!

@Tahedi1
Copy link
Author

Tahedi1 commented Jan 24, 2019

I'm not working with imagnet dataset. For this reason, I'm not using your code. I have already trained a model and I have two files (model.h5, model_weights.h5).

@Tahedi1
Copy link
Author

Tahedi1 commented Jan 24, 2019

I have solved the issue but the problem is analyser is very fast with VGG16 but with inceptionV3 is very slow.

@albermax
Copy link
Owner

albermax commented Jan 24, 2019

Again you are not very specific. If you need help please provide some example code and more evidence.
We don't experience those problems. Once compiled the code is typically as fast as a gradient computation.

I close the issue your problems is fixed.

@dalia1992
Copy link

I have solved the issue but the problem is analyser is very fast with VGG16 but with inceptionV3 is very slow.

Could you tell me how you solved it? I'm having the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants