Open jbug13 opened 1 year ago
So basically IntegratedGradients
expects your forward_fun
to output a tensor as target to calculate gradients against. We don't have any assumptions of your model architecture or purpose. So return the right tensor to attribute based on what you want to do.
Based on your code, seems you want to explain the most confident detection. I noticed several issues in your code:
First, IntegratedGradients
requires gradients, so you should not use output = prediction["scores"].detach()
to remove the target from computing graph.
Second, output3 = torch.topk(output2, 1)
returns 2 tensors as a tuple, the 1st is the value you may want to attribute to. The 2nd is the indices.
Third, you should omit the target in integrated_gradients.attribute(x, target = 0, n_steps=100)
. The target
is only useful in classification model to make target selection easier. You have already manually selected the target in your forward torch.topk(output2, 1)
.
Thank you very much for your reply and help! I plan on working on this some more today. This is all very new to me so I really appreciate you taking the time to respond.
I was able to get some output from the IntegratedGradients and the Occulsion attributes as well. Your comments helped me a great deal. I still have a long ways to go to understanding the data output and if I am employing the functions as intended. I will spend some time in the near future reading the Integrated Gradients paper. Thank you very much for your help!
❓ Questions and Help
Hello! I am trying to learn about about Captum and how to use it. I am trying to use some of the Captum model interpretation tools such as Integrated Gradients (and eventually Occlusion). I was able to execute the example provided from the following link:
https://captum.ai/tutorials/Resnet_TorchVision_Interpret
I was then trying to swap out the resnet used for Resnet50: https://pytorch.org/vision/main/models/generated/torchvision.models.detection.fasterrcnn_resnet50_fpn_v2.html
I see that the input to Integrated Gradients requires a single tensor, but the Resnet50 outputs a list[Dict[Tensor]]. I came across some other posts saying that a forward function for the model may be required. I have been unable to get this to work thus far. This where I am a bit stuck most likely due to a combination of lack of experience and operator error on my part. Currently I am getting an error saying: "Selected k out of index range".
Any advice or direction pointing would be most appreciated.
Below is the code I modified from the original working example for reference.