raghakot / keras-vis

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

3D visualization: ValueError: 'arr' does not have a suitable array shape for any mode. #112

Open sodaGH opened 6 years ago

sodaGH commented 6 years ago

Thanks for your great work! I want to visualize the grad-CAM figure for my 3D data image. However, I met some problem at this line: grads = visualize_cam(model, layer_idx, filter_indices=20, seed_input=img, backprop_modifier=modifier) in the code block:

import numpy as np import matplotlib.cm as cm from vis.visualization import visualize_cam for modifier in [None, 'guided', 'relu']: plt.figure() f, ax = plt.subplots(1, 2) plt.suptitle("vanilla" if modifier is None else modifier) for i, img in enumerate([img1, img2]):

20 is the imagenet index corresponding to ouzel

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

Lets overlay the heatmap onto original image.

jet_heatmap = np.uint8(cm.jet(grads)[..., :3] * 255) ax[i].imshow(overlay(jet_heatmap, img))

It returns the error:

Traceback (most recent call last): File "CNN_vis_CAM.py", line 319, in seed_input=img, backprop_modifier=modifier)
File "/usr/local/lib/python2.7/dist-packages/vis/visualization/saliency.py", line 239, in visualize_cam return visualize_cam_with_losses(model.input, losses, seed_input, penultimate_layer, grad_modifier) File "/usr/local/lib/python2.7/dist-packages/vis/visualization/saliency.py", line 186, in visualize_cam_with_losses heatmap = imresize(heatmap, input_dims, interp='bicubic', mode='F') File "/usr/local/lib/python2.7/dist-packages/scipy/misc/pilutil.py", line 480, in imresize im = toimage(arr, mode=mode) File "/usr/local/lib/python2.7/dist-packages/scipy/misc/pilutil.py", line 289, in toimage raise ValueError("'arr' does not have a suitable array shape for " ValueError: 'arr' does not have a suitable array shape for any mode.

My data size is 49-41-40, and I succeed in drawing the saliency figure. Do you know how to fix this problem? Thanks for your reading! And looking forward to your reply.

CanPhi commented 6 years ago

I would also be interested in whether this can be used for 3D inputs.

keisen commented 6 years ago

I think the direct cause of the error has already been corrected in current master branch. But, the problem I wrote in issues#113 also occurs here. If you are using Conv2D, please execute the step in issue#113.

jalilahmed commented 6 years ago

I am having the same issue. I have a 3D VGG. Saliency Maps are generated by visualize_cam doesnt work.

keisen commented 6 years ago

@jalilahmed , Can I see the error log of your issue ?

jalilahmed commented 6 years ago

@keisen The library was NOT UPDATED. Doing an UPDATE removed the error. Thanks for your quick response.