jacobgil/keras-grad-cam

Problem with "target_category_loss" function

thiippal opened this issue · 8 comments

Running the script raises the following error:

File "grad-cam.py", line 35, in <lambda>
target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
File "grad-cam.py", line 13, in target_category_loss
return tf.mul(x, K.one_hot([category_index], nb_classes))
File "theano_backend.py", line 726, in one_hot
input_shape = tuple((indices.shape[i] for i in range(indices.ndim)))
AttributeError: 'list' object has no attribute 'ndim'

Any ideas how to fix this?

Currently only the tensorflow backend is supported, so the immediate thing would be to switch the keras backend to tensorflow.
I'll take a look later to try get it working with theano too.

D'oh ... obviously! My bad: I switched to TensorFlow backend and now get the following error message:

File "grad-cam.py", line 72, in <module>
cam  = grad_cam(model, preprocessed_input, predicted_class, "block5_pool")
File "grad-cam.py", line 41, in grad_cam
grads = normalize(K.gradients(loss, conv_output)[0])
TypeError: Expected binary or unicode string, got <keras.layers.pooling.MaxPooling2D object at 0x119beaf10>

Can you please check your tensorflow and keras versions?
That can be done by:
import tensorflow as tf
print tf.__version__
import keras
print keras.__version__

Perhaps tensorflow should be upgraded.

Also please make sure the code in grad-cam.py was not modified.

Sure: tensorflow is at 0.11.0 and Keras at 1.1.1.

Haven't dabbled with the code either!

@thiippal I think you can use the debug mode to check the code line by line, and then tell him the code in which line is not correct. I use theano instead of tensorflow, but I carefully read the code and it seems that there are no errors.

I wasn't able to reproduce the error with these tensorflow and keras versions.
Are you sure that both the dimension ordering and backend are tensorflow, like this:
{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
?

I checked my Keras configuration file: the backend and image dimensions are both set for tensorflow.

Anyhow, I created a fresh virtual environment with Keras and TensorFlow and cloned the repository again, and now it works. I don't recall making any changes to the code, but it is likely that I might have tinkered with it.

My apologies for the unnecessary work on your behalf; I really appreciate your Keras implementations of all these new techniques.

Thank you.
If you have thoughts on how to improve this or experimentations worth doing please suggest!