Giter VIP home page Giter VIP logo

Comments (4)

nsthorat avatar nsthorat commented on June 13, 2024 1

Well, it depends on what you're looking for. You could take the sum, which would say which pixels are most sensitive to all outputs, or you can focus on a single one of the output dimensions.

You could take the gradient of each and do a weighted average -- or you could take the weighted average of the output values and use that as your y node. I would try both and see what happens :)

from saliency.

nsthorat avatar nsthorat commented on June 13, 2024

On a regression task it should be the same technique.

In the classification task we choose a "neuron", which is actually just one dimension of the logits vector, to compute gradients with respect to. Intuitively this says "if we look at a single class, let's say a deer, and we wiggle the values of a fixed image X, how much do they each affect the prediction of that deer?".

In a regression task, the continuous value output that your model predicts can also be used in the same way. The same reasoning holds: "if we look at the value we're predicting, if I wiggle the pixels of a fixed image X, how do each of those pixels affect the output value".

I think this code can be used out of the box: y in your case would be the predicted value: https://github.com/PAIR-code/saliency/blob/master/saliency/base.py#L85

from saliency.

ClaudioCimarelli avatar ClaudioCimarelli commented on June 13, 2024

@nsthorat thanks for the quick reply and the clarification. My doubt is more related to the calculation of the gradient.
More precisely, in the case of classification (as you noted), the label is one dimensional; hence, the gradient is one value per input dimension. Instead, in the case of regression, I have n values (one for each dimension of the output).
After you pointed me to the code I see that you use tf.gradients(), which does the sum along the output dimensions; is this the correct way? I don't know if it's possible to take the gradients of each dimension separately and take a weighted average for example...

from saliency.

Aishuvenkat09 avatar Aishuvenkat09 commented on June 13, 2024

Beautiful work. Thank you. I have a question regarding adapting your work to my use case.
I have my custom model which takes gender and image as input to predict age (Image regression) I am interested to improve performance using guided integrated gradients and also generate saliency maps to see what features my current model is focusing on.
How can I make changes to the call_model_function to give me the regression score (age in this case)?
I have changed the layer is relu. I see output tensors, but I'm interested in generating Mean absolute values (nn.L1Loss).


target_label = data[0]['label'] #190

def call_model_function(images, call_model_args=None, expected_keys=None):
    images = PreprocessImages(images)
    target_class_idx =  call_model_args[class_idx_str]
    output = model(images)
    m = torch.nn.ReLU()
    output = m(output)

    if saliency.base.INPUT_OUTPUT_GRADIENTS in expected_keys:
        outputs = output[:,target_class_idx]
        grads = torch.autograd.grad(outputs, images, grad_outputs=torch.ones_like(outputs))
        grads = torch.movedim(grads[0], 1, 3)
        gradients = grads.detach().numpy()
        return {saliency.base.INPUT_OUTPUT_GRADIENTS: gradients}
    else:
        one_hot = torch.zeros_like(output)
        one_hot[:,target_class_idx] = 1
        model.zero_grad()
        output.backward(gradient=one_hot, retain_graph=True)
        return conv_layer_outputs

Any help is appreciated :)

from saliency.

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.