i am getting this error
Traceback (most recent call last):
File “C:\Users\Ali\PycharmProjects\pythonProject1\heatMap.py”, line 629, in
heatmap_overlay = cv2.addWeighted(np.array(image), 0.7, np.array(heatmap), 0.3, 0)
TypeError: Expected cv::UMat for argument 'src
from captum.attr import IntegratedGradients
Function to generate attributions using Integrated Gradients
def generate_integrated_gradients_attributions(model, input_image, target_label):
integrated_gradients = IntegratedGradients(model)
attributions = integrated_gradients.attribute(input_image, target=target_label)
return attributions
Example usage:
Load and preprocess the image
image_path = “I:\K_Folds\PlantDoc\Fold1\Testing\AP_C\AP_C_72.jpg”
image = Image.open(image_path)
input_image = heatmap_transform(image)
input_image = input_image.unsqueeze(0).to(device)
Define your model
model = YourModelClass()
model.to(device)
model.eval()
Get the true label for the image (replace with your label)
true_label = 0
Generate attributions using Integrated Gradients
attributions = generate_integrated_gradients_attributions(model, input_image, target_label=true_label)
heatmap = viz.visualize_image_attr(attributions[0].cpu().numpy(),
original_image=input_image[0].cpu().numpy(),
method=“heat_map”)[0] # Extract the heatmap from the tuple
heatmap_overlay = cv2.addWeighted(np.array(image), 0.7, np.array(heatmap), 0.3, 0)
Display the original image and the heatmap overlay
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title(‘Original Image’)
plt.subplot(1, 2, 2)
plt.imshow(heatmap_overlay)
plt.title(‘Heatmap Overlay’)