# Heat map for misclassified image

**URL:** <https://discuss.pytorch.org/t/heat-map-for-misclassified-image/189730>\
**Category:** vision\
**Created:** [October 11, 2023, 7:09am UTC](https://discuss.pytorch.org/t/heat-map-for-misclassified-image/189730 "2023-10-11T07:09:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alihamza](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alihamza/32/61234_2.png) [@alihamza](https://discuss.pytorch.org/u/alihamza)\
**Post date:** [October 11, 2023, 7:09am UTC](https://discuss.pytorch.org/t/heat-map-for-misclassified-image/189730/1 "2023-10-11T07:09:51Z")

</div>

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’)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 11, 2023, 3:58pm UTC](https://discuss.pytorch.org/t/heat-map-for-misclassified-image/189730/2 "2023-10-11T15:58:55Z")

</div>

Based on the error message it seems to be an OpenCV related error.  
If you get stuck, post a properly formatted, minimal, and executable code snippet reproducing the error.
