Code Monkey home page Code Monkey logo

Comments (9)

osimeoni avatar osimeoni commented on August 29, 2024 39

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])

from keras-grad-cam.

shsshs avatar shsshs commented on August 29, 2024 5

experiencing the same issue. Found this as well:

tensorflow/tensorflow#783

from keras-grad-cam.

PowerOfCreation avatar PowerOfCreation commented on August 29, 2024 5

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

from keras-grad-cam.

avnishbm avatar avnishbm commented on August 29, 2024

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

from keras-grad-cam.

ccivit avatar ccivit commented on August 29, 2024

osimeoni's answer worked out for me

from keras-grad-cam.

wugoukanle avatar wugoukanle commented on August 29, 2024

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

from keras-grad-cam.

wugoukanle avatar wugoukanle commented on August 29, 2024

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])   

from keras-grad-cam.

jht0664 avatar jht0664 commented on August 29, 2024

@PowerOfCreation Thanks, it really helps. It works!

from keras-grad-cam.

mrshinest avatar mrshinest commented on August 29, 2024

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.

from keras-grad-cam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.