jk12
jk12

Reputation: 1

Grad-CAM for ensemble model

[I wanted to use the Grad-CAM for the ensemble model. Here is the example i am following: enter image description here

I used this code for visualization:

def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):
    # Extract the last convolutional layer output
    last_conv_layer = model.get_layer(last_conv_layer_name)
    last_conv_layer_model = Model(model.inputs, last_conv_layer.output)

    # Compute the gradient of the predicted class with respect to the last conv layer output
    with tf.GradientTape() as tape:
        last_conv_layer_output = last_conv_layer_model([img_array, img_array])
        if pred_index is None:
            pred_index = tf.argmax(last_conv_layer_output[0])

        class_output = model.output[:, pred_index]
        grads = tape.gradient(class_output, last_conv_layer_output)
        pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))

    # Generate the heatmap
    heatmap = tf.reduce_mean(last_conv_layer_output * pooled_grads[..., tf.newaxis], axis=-1)
    heatmap = tf.maximum(heatmap, 0) / tf.reduce_max(heatmap)

    return heatmap

ensemble_model = tf.keras.models.load_model("ensemble.h5")


#ensemble_model.summary()

last_conv_layer_name = 'dense_111'  # Name of the last convolutional layer
last_conv_layer_output = ensemble_model.get_layer(last_conv_layer_name).output

# Compute gradients and generate the Grad-CAM heatmap for the ensemble model
heatmap = make_gradcam_heatmap([img_array], ensemble_model, last_conv_layer_name)# Visualize the Grad-CAM heatmap
plt.imshow(img)
plt.imshow(heatmap, cmap='jet', alpha=0.5)
plt.show()

Upvotes: 0

Views: 73

Answers (0)

Related Questions