-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Extract Nested Layers from Encapsulated Model #16355
Comments
Case 2 It's not only limited to inputs = keras.Input(shape=IMG_SHAPE)
x = pretrained_model(inputs)
x = keras.layers.Dense(512, activation='relu')(x)
outputs = keras.layers.Dense(3, activation='softmax')(x)
model = keras.Model(inputs, outputs)
for l in model.layers:
print(l.name, l.output_shape)
input_7 [(None, 224, 224, 3)]
efficientnetb0 (None, 1280)
dense_7 (None, 512)
dense_8 (None, 3) nested_layers = tf.keras.models.Model(
[model.inputs],
[model.layers[1].get_layer('top_activation').output, model.output]
) It also gives
[continue] |
The Current WorkaroundCurrently, we need to inputs = keras.Input(shape=IMG_SHAPE)
pretrained_model = keras.applications.EfficientNetB0(
weights='imagenet',
include_top=False,
input_tensor=inputs,
pooling='avg'
)
pretrained_model.trainable = False
x = keras.layers.Dense(512, activation='relu')(pretrained_model.output)
outputs = keras.layers.Dense(3, activation='softmax')(x)
model = keras.Model(inputs, outputs)
Describe the expected behavior. The case 1 and case 2, described above should be done like the above-mentioned current workaround.
Standalone code to reproduce the issue. It's already provided in the above description. Please see. [End] |
@gadagashwini I was able to replicate the issue on colab using TF v2.8.0 , please find the gist.Thanks! |
I think the error message could be better, but the behavior here is logical. When you create the Does that make sense so far? Because the top layer has been called twice, To access the second output, the one you're looking for, you should be able to do: nested_layers = keras.Model(
[model.inputs], # This is the Input() you've created yourself
[pretrained_model.inbound_nodes[0].output_tensors, # This is output created when you called `pretrained_model()` on your own `Input()`
model.output] # This is the output of the final `Dense()` layer you created
) Let me know if this is clear. |
@fchollet yes, that makes sense. Thanks for your details answer. I also understood that it's not a bug, I should mention that earlier. The thing is, I saw many practitioners face such issues after training their models and later trying to compute grad-cam. It's one of the common keras-tagged questions on the stack. The fact is the modeling approach (
About this workaround, in case 1/2, nested_layers = keras.Model([
model.inputs
],[
pretrained_model.inbound_nodes[0].output_tensors,
model.output
]
) This pretrained_model.inbound_nodes?
Type: property
String form: <property object at 0x7ff9f6f22d70>
Docstring: Deprecated, do NOT use!
Only for compatibility with external Keras. Is it safe to use for now and later? |
@lucasdavid any thoughst on this? I saw some utility from here, looks promising. |
As far as I am aware, in a previous version of Keras: nested_layers = keras.Model(
[model.inputs],
[model.get_layer("inner_model").get_layer("post_relu").get_output_at(1),
model.output] # This is the output of the final `Dense()` layer you created
) This solution is better discussed in #34977#issuecomment-571878943. In the current version: About the utility you mentioned: I kept it in my own lib (keras-explainable), in case the user has an older version of keras, but I have skipped the testing of this functionality on master. |
@innat has this problem been fixed? How does the new API behave? |
Hi @lucasdavid, I didn't test this with the new API yet. |
System information.
Describe the problem and current behavior
Encapsulated Model - It can be expressed as a whole model but act as a single layer. Please see the description below for a better understanding.
Case 1
Let's say, at first you initiate a pretrained model from
keras. applications
. And next, place it toSequential API
to create a final model. Later, you successfully train the model. And then, you want to get the intermediate feature maps from thatkeras. applications
model.It gives
[continue]
The text was updated successfully, but these errors were encountered: