raghakot / keras-vis

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

Self-driving example #181

Closed panispani closed 5 years ago

panispani commented 5 years ago

The self driving example (applications/self_driving/visualize_attention.ipynb) throws an exception to me on the visualization code block:

from vis.visualization import visualize_saliency, overlay

titles = ['right steering', 'left steering', 'maintain steering']
modifiers = [None, 'negate', 'small_values']
for i, modifier in enumerate(modifiers):
    heatmap = visualize_saliency(model, layer_idx=-1, filter_indices=0, 
                                 seed_input=bgr_img, grad_modifier=modifier)
    plt.figure()
    plt.title(titles[i])
    # Overlay is used to alpha blend heatmap onto img.
    plt.imshow(overlay(img, heatmap, alpha=0.7))

Gives:

ValueError                                Traceback (most recent call last)
<ipython-input-2-56117829ec05> in <module>
      9     plt.title(titles[i])
     10     # Overlay is used to alpha blend heatmap onto img.
---> 11     plt.imshow(overlay(img, heatmap, alpha=0.7))

/anaconda3/envs/mypython3/lib/python3.6/site-packages/keras_vis-0.4.1-py3.6.egg/vis/visualization/__init__.py in overlay(array1, array2, alpha)
     46         raise ValueError("`alpha` needs to be between [0, 1]")
     47     if array1.shape != array2.shape:
---> 48         raise ValueError('`array1` and `array2` must have the same shapes')
     49 
     50     return (array1 * alpha + array2 * (1. - alpha)).astype(array1.dtype)
ValueError: `array1` and `array2` must have the same shapes

As far as I can tell, the image loaded is not (FRAME_W, FRAME_H) but rather (FRAME_W, FRAME_H, 3) and the heatmap is (FRAME_W, FRAME_H) hence the error on the overlay. When loading the data in the first code block of the notebook, I don't see how the

img = utils.load_img('images/left.png', target_size=(FRAME_H, FRAME_W))

would make sense because the image has colors.

Repro

I just installed keras-vis by building the repo with

python setup.py install

and my tensorflow version is 1.13

Thanks for any help!

keisen commented 5 years ago

Thank you for your detailed report! We are going to fix this problem.