Skip to content

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

Closed
@tombinic

Description

@tombinic

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.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions