jacobgil / keras-grad-cam

An implementation of Grad-CAM with keras
MIT License
660 stars 236 forks source link

ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported. #17

Open hrishikeshv opened 6 years ago

hrishikeshv commented 6 years ago

I am getting the following error when I am trying to run your code on one of the example images. Is this due to some missing package? Thanks!

Predicted class:
boxer (n02108089) with probability 0.42
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1040, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 235, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 214, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", line 421, 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 "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 524, in _apply_op_helper
    values, as_ref=input_arg.is_ref).dtype.name
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1040, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 235, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/constant_op.py", line 214, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/tensor_util.py", line 421, 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.py", line 135, in <module>
    cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, "block5_conv3")
  File "grad-cam.py", line 99, in grad_cam
    grads = normalize(K.gradients(loss, conv_output)[0])
  File "grad-cam.py", line 22, in normalize
    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)
  File "/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py", line 1435, in square
    return tf.square(x)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/math_ops.py", line 470, in square
    return gen_math_ops.square(x, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 7813, in square
    "Square", x=x, name=name)
  File "/usr/local/lib/python3.5/dist-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.
shsshs commented 6 years ago

experiencing the same issue. Found this as well:

https://github.com/tensorflow/tensorflow/issues/783

avnishbm commented 6 years ago

I am also getting the same error, can this issue be fixed?

osimeoni commented 6 years ago

I made it work following piece of code from : https://github.com/tensorflow/tensorflow/issues/783

def _compute_gradients(tensor, var_list): grads = tf.gradients(tensor, var_list) return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)]

and transforming from grads = normalize(K.gradients(loss, conv_output)[0]) to grads = normalize(_compute_gradients(loss, [conv_output])[0])

ccivit commented 6 years ago

osimeoni's answer worked out for me

wugoukanle commented 6 years ago

I get another error when i use: def _compute_gradients(tensor, var_list): def _compute_gradients(tensor, var_list): grads = tf.gradients(tensor, var_list) &nbsp return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)] the error is: zip argument #1 must support iteration

wugoukanle commented 6 years ago

I finally find out where is key problem: K.gradient return None, because it counter an op that can't gradient I doubt that the Sequential have used to encapsulate vgg16 , that is a Model type here is code in grad_cam funciton after alter:

    nb_classes = 1000
    target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
    last = Lambda(target_layer, output_shape=target_category_loss_output_shape)(input_model.output)
    model = Model(inputs=input_model.input, outputs=last)
    loss = K.sum(model.layers[-1].output)  
    # loss = model.layers[-1].output              
    conv_output = [l for l in model.layers if l.name is layer_name][0].output    
    grads = normalize(K.gradients(loss, conv_output)[0])
    # grads = normalize(_compute_gradients(loss, conv_output)[0])    
    gradient_function = K.function([model.inputs[0]], [conv_output, grads])   
PowerOfCreation commented 5 years ago

For everyone who is facing the same issue: I forked this repository and fixed all errors I was facing.

https://github.com/jacobgil/keras-grad-cam/pull/27 https://github.com/PowerOfCreation/keras-grad-cam

jht0664 commented 5 years ago

@PowerOfCreation Thanks, it really helps. It works!

mrshinest commented 3 years ago

For anyone who still experience this error with tensorflow 2.x. The default argument at tf.gradients (unconnected_gradients='none') causes this error. Set it to ='zero' could solve your problem.