raghakot / keras-vis

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

visualize_cam not working, error: RuntimeError: sequence argument must have length equal to input rank #184

Closed Ajax121 closed 5 years ago

Ajax121 commented 5 years ago

Hi guys, I have been working with your keras vis tool kit , its a great tool and I have been able to get good results. However today when I tried visualize_cam its throwing an error which had never occurred up until now.

The error: RuntimeError: sequence argument must have length equal to input rank

`` 7 a = np.rot90(a) 8 t_img = a.reshape(242,242,1) ----> 9 gradcam = visualize_cam(model, layer_idx, filter_indices=1,seed_input=t_img, backprop_modifier='guided') 10 plt.imshow(a, cmap='gray') 11 #plt.imshow(gradcam,cmap=cmap_AD,vmin=0.3,vmax=1,alpha = 0.5)

3 frames /usr/local/lib/python3.6/dist-packages/vis/visualization/saliency.py in visualize_cam(model, layer_idx, filter_indices, seed_input, penultimate_layer_idx, backprop_modifier, grad_modifier) 243 (ActivationMaximization(model.layers[layer_idx], filter_indices), -1) 244 ] --> 245 return visualize_cam_with_losses(model.input, losses, seed_input, penultimate_layer, grad_modifier)

/usr/local/lib/python3.6/dist-packages/vis/visualization/saliency.py in visualize_cam_with_losses(input_tensor, losses, seed_input, penultimate_layer, grad_modifier) 193 # Figure out the zoom factor. 194 zoom_factor = [i / (j * 1.0) for i, j in iter(zip(input_dims, output_dims))] --> 195 heatmap = zoom(heatmap, zoom_factor) 196 return utils.normalize(heatmap) 197

/usr/local/lib/python3.6/dist-packages/scipy/ndimage/interpolation.py in zoom(input, zoom, output, order, mode, cval, prefilter) 593 else: 594 filtered = input --> 595 zoom = _ni_support._normalize_sequence(zoom, input.ndim) 596 output_shape = tuple( 597 [int(round(ii * jj)) for ii, jj in zip(input.shape, zoom)])

/usr/local/lib/python3.6/dist-packages/scipy/ndimage/_ni_support.py in _normalize_sequence(input, rank) 63 if len(normalized) != rank: 64 err = "sequence argument must have length equal to input rank" ---> 65 raise RuntimeError(err) 66 else: 67 normalized = [input] * rank

RuntimeError: sequence argument must have length equal to input rank ``

I also see that there has been a change made to the visualization library recently (3hours ago). However visualize_saliency seem to work fine.

keisen commented 5 years ago

Hi, @Ajax121 . Thank you for your report! As you said, keras-vis was updated. and the change about visualize_cam is only below:

https://github.com/raghakot/keras-vis/commit/d597c5c6254ba79d7a3b11e4ebd1feb7cee42de4

I'd like to help you, so could you please send us the reproduce code?

keisen commented 5 years ago

@Ajax121 , I get the code that can reproduce the problem.

keisen commented 5 years ago

I have fixed it. @Ajax121 , Could you please make sure the current code .

Ajax121 commented 5 years ago

Hi @keisen , Thanks a lot for your help, it is working now. Just out of curiosity, what seemed to be the error?

keisen commented 5 years ago

The cause of problem was that refer seed_input to get the input tensor shape. In example notebook, the input tensor's shape was (?, 224, 224, 3), but seed_input 's shape was (224, 224, 3) . The difference in their shapes was the cause of the error.

Ajax121 commented 5 years ago

Thank you @keisen