eclique / keras-gradcam

Keras implementation of GradCAM.
MIT License
115 stars 45 forks source link

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

Closed jjaacckkyy63 closed 6 years ago

jjaacckkyy63 commented 6 years ago

Hi, I am using tutorial as a practice but when i ran the command python grad_cam.py cat_dog.png I got the error that didn't occur to me last time I ran it

Model prediction: boxer (242) with probability 0.420 bull_mastiff (243) with probability 0.282 tiger_cat (282) with probability 0.053 tiger (292) with probability 0.050 Great_Dane (246) with probability 0.050 Explanation for 'boxer'

ValueError Traceback (most recent call last)

in () 1 gradcam, gb, guided_gradcam = compute_saliency(model, guided_model, 'cat_dog.png', layer_name='block5_conv3', ----> 2 cls=-1, visualize=True, save=True) 3 # gradcam, gb, guided_gradcam = compute_saliency(model, guided_model, 'cat_dog.png', layer_name='block5_conv3' 4 # cls=282, visualize=True, save=False) in compute_saliency(model, guided_model, img_path, layer_name, cls, visualize, save) 20 gradcam = grad_cam(model, preprocessed_input, cls, layer_name) 21 gb = guided_backprop(guided_model, preprocessed_input, layer_name) ---> 22 guided_gradcam = gb * gradcam[..., np.newaxis] 23 print(guided_gradcam.shape) 24 ValueError: operands could not be broadcast together with shapes (1,3,224,224) (224,224,1) Thank you!
eclique commented 6 years ago

Hi @jjaacckkyy63,

The gb shape should be (1, 224, 224, 3) and you are getting (1, 3, 224, 224). Are you using theano backend? This line can be fixed by changing gradcam[..., np.newaxis] to gradcam[np.newaxis, ...], but you will need to correct shapes in other places too.

jjaacckkyy63 commented 6 years ago

Hi, but I was using tensorflow as backend Is there a problem about my keras version. I have updated it recently

jjaacckkyy63 commented 6 years ago

May I ask you about all the package version when you developing it including like opencv ?

eclique commented 6 years ago

I ran it with the latest conda packages yesterday. tensorflow 1.3.0 keras 2.0.5 opencv 3.1.0