Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hrishikeshv opened this issue Apr 15, 2018 · 9 comments

Comments

@hrishikeshv
Copy link

hrishikeshv commented Apr 15, 2018

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
Copy link

shsshs commented Apr 22, 2018

experiencing the same issue. Found this as well:

tensorflow/tensorflow#783

@avnishbm
Copy link

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

@osimeoni
Copy link

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
Copy link

ccivit commented Jul 21, 2018

osimeoni's answer worked out for me

@wugoukanle
Copy link

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
Copy link

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
Copy link

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

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

@jht0664
Copy link

jht0664 commented Jul 16, 2019

@PowerOfCreation Thanks, it really helps. It works!

@mrshinest
Copy link

mrshinest commented Apr 23, 2021

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.

mslwang added a commit to darwinai/pl-covidnet-grad-cam that referenced this issue Jul 12, 2021
mslwang added a commit to darwinai/pl-covidnet-grad-cam that referenced this issue Aug 26, 2021
Initial commit

Add initial Grad-CAM implementation and other initial files

Add requirements.txt for faster environment setup.

Fixes on top of https://github.com/jacobgil/keras-grad-cam (as of
today, July 12th 2021):
- jacobgil/keras-grad-cam#17 (comment)
- jacobgil/keras-grad-cam#21 (comment)

Add modifications to add thresholding

Add yapf config for vscode, move requirements.txt, and format code

Fix links to show sample images in keras-grad-cam

Move files src folder and modify gitignore

Convert folders into packages

Add preprocessing functions

Remove guided backprop code from grad-cam

Re-format grad-cam folder and add first version of algo

Remove keras_grad_cam examples and information
mslwang added a commit to darwinai/pl-covidnet-grad-cam that referenced this issue Aug 27, 2021
rahmatkhah pushed a commit to darwinai/pl-covidnet-grad-cam that referenced this issue Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants