raghakot / keras-vis

Neural network visualization toolkit for keras
https://raghakot.github.io/keras-vis
MIT License
2.97k stars 664 forks source link

visualize_cam output array shape is not the same of input array shape #102

Open tofahito2 opened 6 years ago

tofahito2 commented 6 years ago

visualize_cam output array shape is not the same of input array shape. It gives me (224,3,3) as an output for (224,224,3) input shape. Any help ? Thanks in advanced.

shsshs commented 6 years ago

for me visualize_cam is fine (224,224,3) to (224,224,3). But this command jet_heatmap = np.uint8(cm.jet(grads)[..., :3] * 255) from https://github.com/raghakot/keras-vis/blob/master/examples/vggnet/attention.ipynb gives me (224,224,3,3).

shahruk10 commented 6 years ago

same problem here;

I plotted each of the 3 images (iterating the last index) in jet_heatmap; they seem to color different regions of the image. Not sure how to merge them.

fig

shahruk10 commented 6 years ago

ok, I found a workaround, you can directly plot the grads as a color map by the following :

`

    grads = visualize_cam(model, layer_idx, filter_indices=20,seed_input=img, backprop_modifier=modifier)

    # no need for this line
    # jet_heatmap = np.uint8(cm.jet(grads)[..., :3] * 255)

    # first plot the base img
    ax.imshow(img)    

    # add gradient as overlay
    ax.imshow(grads,cmap='jet', alpha = 0.6)
    plt.show()

fig

`

ghost commented 6 years ago

can someone help with this? I could find no examples in the documentation to generate guided grad_CAM as shown in the paper (with the point-wise multiplication of guided_back propagation and a bi-linear interpolated grad_CAM). It would be great if someone can answer this question, add this function to the toolkit or give the same here for instance.

eballesteros commented 5 years ago

I have noticed the output shape for visualize_cam is different for 0.41 version and for master.

with input_cam.shape (544, 544, 3) this:

out_cam = visualize_cam(model = my_model,
                        layer_idx = my_layer_idx,
                        filter_indices=0,
                        seed_input=input_cam,
                        penultimate_layer_idx = my_penultimate_layer_idx,
                        backprop_modifier=None,
                        grad_modifier=None)
print(out_cam.shape)

use to output shape (544, 544, 3) on 0.41 but with the version from the master branch it outputs (544, 544)

eballesteros commented 5 years ago

I ended up adding

if len(out_cam.shape) == 2:
    out_cam = (cm.jet(out_cam)[:,:,:-1]*255).astype(np.uint8) #to RGBA - A

to make the code compatible with bot 0.41 and posterior versions