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

Grad-CAM Fails on Custom Model with Nested ResNet50 #1999

Open
tombinic opened this issue Nov 30, 2024 · 0 comments
Open

Grad-CAM Fails on Custom Model with Nested ResNet50 #1999

tombinic opened this issue Nov 30, 2024 · 0 comments
Assignees

Comments

@tombinic
Copy link

tombinic commented Nov 30, 2024

I am trying to implement Grad-CAM using a custom model that includes ResNet50 as its backbone. When I attempt to build a model to extract the activations of the final convolutional layer and the final predictions, 1 encounter a KeyError during the model call.
Error Message

KeyError: 'Exception encountered when calling
Arguments received by Functional.call():
• inputs=tf. Tensor(shape=(1, 224, 224, 3),
• training=None
• mask=None'

My custom model is constructed as follows:

from 
tensorflow.keras.applications import ResNet50
# Custom model
inputs = Input (shape=(224, 224, 3))
resnet_model = ResNet50(include_top=False, weights="imagenet", pooling="avg")
x = resnet_model (inputs)
outputs = Dense(1, activation="sigmoid") (x)
model = Model(inputs=inputs, outputs=outputs, name="custom_model")

And

import tensorflow as tf
from tensorflow.keras.models import Model

def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):

    # Create the grad_model using correct inputs and outputs
    grad_model = Model(
        inputs=model.input,
        outputs=[
            model.get_layer("resnet50").get_layer(last_conv_layer_name).output,
            model.output
        ]
    )

    # Trace the gradient
    with tf.GradientTape() as tape:
        last_conv_layer_output, preds = grad_model(img_array)
.....

When the code arrives in grad_model(img_array) raises the error.
How can I fix it?

Thank you.

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

2 participants