I am quite new to ML/DL and I am trying to use GRAD-CAM on a CNN I created using VGG16 as the convolutional base. But whenever I try doing that, it throws a Graph Disconnected: cannot obtain value for tensor, regarding vgg_input layer. I don’t know how to fix it. Can somebody help me out?

I am attaching the images of my code for reference below, thanks!

MODEL:

FUNCTION USED FOR GRAD-CAM:

ERROR:

I had the same problem and I solved it by the way I build my model so that in this way the layers of the pretrained layer are not hidden . I don’t know how to explain this exactly so I will provide an example instead :

for example rather than building your model this way :

input_shape = (224, 224, 3)

input_layer = Input(shape=input_shape)

# Load the pre-trained model

base_model = VGG16(weights=“imagenet”, include_top=False, input_shape=(224, 224, 3))

x = base_model(input_layer)

x = Conv2D(32, (3, 3), activation=‘relu’)(x)

x = GlobalAveragePooling2D()(x)

x = Dropout(0.2)(x)

predictions = Dense(1, activation=“sigmoid”)(x)

model = Model(inputs=input_layer, outputs=predictions)

you should build it this way :

inputs = tf.keras.Input(shape=(224, 224, 3))

x = keras.applications.VGG16(input_tensor=inputs ,weights=“imagenet”, include_top=False, input_shape=(224, 224, 3))

x = layers.GlobalAveragePooling2D()(x.output)

x = layers.Dropout(0.2)(x)

x = layers.Dense(1, activation=“sigmoid”)(x)

model = keras.Model(inputs, x)

You can check the difference between both approaches by printing the layers of the model for each one using model.summary()