TypeError: unbind(): argument 'input' (position 1) must be Tensor, not list

Paul_Reiners: <p>I have the following code that uses the <a href="https://captum.ai/" rel="noopener nofollow ugc">Captum</a> library:</p>
<pre><code>import numpy as np
import torch.utils.data
import torchio as tio
from captum.attr import Saliency

from dcan.data_sets.dsets import LoesScoreDataset
from reprex.models import AlexNet3D


def normalize_array(array):
    new_array = (array - array.min()) / (array.max() - array.min())

    return new_array


net = AlexNet3D(4608)
model_save_location = '/home/models/loes_scoring_03.pt'
net.load_state_dict(torch.load(model_save_location,
                               map_location='cpu'))
net.eval()

example_file = \
    '/home/feczk001/shared/projects/S1067_Loes/data/MNI-space_Loes_data/' \
    'sub-4750MASZ_ses-20080220_space-MNI_mprageGd.nii.gz'
image = tio.ScalarImage(example_file)

image_tensor = image.data

image_tensor = torch.unsqueeze(image_tensor, dim=0)
image_tensor = normalize_array(image_tensor)

with torch.no_grad():
    output = net(image_tensor)
    print(output)

print("Using existing trained model")
net.load_state_dict(torch.load('models/loes_scoring_03.pt'))

csv_data_file = "./data/MNI-space_Loes_data.csv"
testset = LoesScoreDataset(
    csv_data_file,
    use_gd_only=False,
    val_stride=10,
    is_val_set_bool=True,
)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)

dataiter = iter(testloader)
images, labels = next(dataiter)

ind = 3

input = images[ind].unsqueeze(0)
input.requires_grad = True

net.eval()

saliency = Saliency(net)
grads = saliency.attribute(input)
</code></pre>
<p>One the last line of code, I am getting this error:</p>
<pre><code>Traceback (most recent call last):
  File "/home/miran045/reine097/projects/loes-scoring-explainability/src/my_captum_test.py", line 60, in &lt;module&gt;
    grads = saliency.attribute(input)
  File "/home/miran045/reine097/projects/loes-scoring-explainability/venv/lib/python3.6/site-packages/captum/log/__init__.py", line 42, in wrapper
    return func(*args, **kwargs)
  File "/home/miran045/reine097/projects/loes-scoring-explainability/venv/lib/python3.6/site-packages/captum/attr/_core/saliency.py", line 131, in attribute
    self.forward_func, inputs, target, additional_forward_args
  File "/home/miran045/reine097/projects/loes-scoring-explainability/venv/lib/python3.6/site-packages/captum/_utils/gradient.py", line 119, in compute_gradients
    grads = torch.autograd.grad(torch.unbind(outputs), inputs)
TypeError: unbind(): argument 'input' (position 1) must be Tensor, not list
</code></pre>
<p>The variable <code>input</code> on the last line of the code has the type <code>torch.Tensor</code>.</p>
<p>What am I doing wrong here?  I realize this isn’t a stand-alone example, so please let me know if you want to know the values or types of any variables at run-time.</p>

ptrblck: <p>Based on the error message it seems an <code>outputs</code> object is computed inside <code>saliency.attribute(input)</code>, so check if your model is returning a <code>list</code> while a tensor is expected.</p>

Paul_Reiners: <p>I solved it by replacing this line:</p>
<p><code>saliency = Saliency(net)</code></p>
<p>by these lines:</p>
<pre><code class="lang-auto">def wrapped_model(inp):
    return net(inp)[0]

saliency = Saliency(wrapped_model)
</code></pre>