Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attention maps - ValueError: operands could not be broadcast together #41

Closed
walid0925 opened this issue Jun 20, 2017 · 1 comment
Closed

Comments

@walid0925
Copy link

Hi,

I am trying to use visualize_cam with a model that has been trained as channels_first, and I have been receiving the following error:

    266     # Create jet heatmap.
    267     heatmap_colored = np.uint8(cm.jet(heatmap)[..., :3] * 255)
--> 268     heatmap_colored = np.uint8(seed_img * alpha + heatmap_colored * (1. - alpha))
    269     return heatmap_colored

ValueError: operands could not be broadcast together with shapes (1,3,224,224) (224,224,3)

My preprocessing is as follows:

from keras import backend as K
import numpy as np
from scipy.misc import imresize, imread
from vis.utils import utils
from vis.visualization import visualize_saliency
from vis.visualization import visualize_cam

import my_model

# get Keras model
model = my_model.get_model()

K.set_image_data_format('channels_first')

heatmaps = []
for path in image_paths:
    x = imread(path)
    x = x.astype('float32')
    x = imresize(x, (224, 224, 3))

    # change BGR -> RGB
    x[:,:,[0,1,2]] = x[:,:,[2,1,0]]

    # transpose to channels first
    x = x.transpose((2, 0, 1))
    x = np.expand_dims(x, axis =0)
    x = x / 255
    pred_class = np.argmax(model.predict(x))
    heatmap = visualize_cam(model, layer_idx, [pred_class], x)
    heatmaps.append(heatmap)

It seems as though the same issue occurs with saliency maps as well.

@raghakot
Copy link
Owner

With the latest commit, I separated the code that is responsible for overlaying heatmap onto image because the heatmap can be generated for N-dim images and overlay only makes sense for rgb or grayscale images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants