jacobgil/keras-grad-cam

Code crashes for new version of TF and Keras

vianamp opened this issue ยท 6 comments

Hi. I just updated Keras (2.1.0) and TF (1.4.0) and the your code is no longer working. It seems that the function K.gradients(...) is now returning [None]. Any idea how to fix it without downgrading to older versions?

Full error report:

Traceback (most recent call last):
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 371, in make_tensor_proto
    raise ValueError("None values not supported.")
ValueError: None values not supported.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 524, in _apply_op_helper
    values, as_ref=input_arg.is_ref).dtype.name
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 371, in make_tensor_proto
    raise ValueError("None values not supported.")
ValueError: None values not supported.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "grad-cam-or.py", line 138, in <module>
    cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, "block5_conv3")
  File "grad-cam-or.py", line 100, in grad_cam
    grads = normalize(K.gradients(loss, conv_output)[0])
  File "grad-cam-or.py", line 22, in normalize
    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py", line 1412, in square
    return tf.square(x)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 449, in square
    return gen_math_ops.square(x, name=name)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py", line 4567, in square
    "Square", x=x, name=name)
  File "/Users/mviana/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 528, in _apply_op_helper
    (input_name, err))
ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported.

i have the same problem

Hi @xieenze. Apparently it has not been fixed. Please check: #8478.

Hello! any solution of this problem ?

downgrading keras worked for me.
pip3 install 'keras==2.0.9' --force-reinstall
can make it work again

vense commented

@chavdim Thanks for your comment. But it is not a perfect solution.
So, I modified code little. https://github.com/vense/keras-grad-cam/blob/master/grad-cam.py
This code is tested by keras version 2.1.3.

@vense i tried your code too but i get:

np.float32(cam) + np.float32(image)
ValueError: operands could not be broadcast together with shapes (224,224,3) (3,224,224) 

why they have different shapes? I this because of Tenorflow? My version is 1.13.1