title: Does anyone know why SHAP's Deep Explainer fails on ResNet-50 pretrained model?
body: <p>I have trained a ResNet-50 model and I am attempting to implement the explainability method SHAP. I have found this has been an issue for others in <a href="https://github.com/slundberg/shap/issues/1479" rel="nofollow noreferrer">https://github.com/slundberg/shap/issues/1479</a>.</p>
  <p>My code closely follows the instructions in the SHAP pytorch example in <a href="https://github.com/slundberg/shap/blob/master/notebooks/image_examples/image_classification/PyTorch%20Deep%20Explainer%20MNIST%20example.ipynb" rel="nofollow noreferrer">https://github.com/slundberg/shap/blob/master/notebooks/image_examples/image_classification/PyTorch%20Deep%20Explainer%20MNIST%20example.ipynb</a>:</p>
  <pre><code>def main():
      output_format = Pupils()
  
      model_path = &quot;.../trained_model_30_epochs&quot;
      ellipse_overlay_path = &quot;.../ellipse_overlay.png&quot;
  
      path_to_dataset = [DatasetPath(path='...',
                                     image_type='png')]
  
      path_to_dataset_to_explain = [DatasetPath(path='...',
                                     image_type='png')]
  
      dataset = RealDataset(path_to_dataset,
                            output_format.ground_truth_from_annotations,
                            transform=get_transforms(is_training=False, is_synthetic=False),
                            load_into_ram=True
                            )
  
      dataset_to_explain = RealDataset(path_to_dataset_to_explain,
                                       output_format.ground_truth_from_annotations,
                                       transform=get_transforms(is_training=False, is_synthetic=False),
                                       load_into_ram=True
                                       )
      dataloader = DataLoader(dataset,
                              batch_size=100,
                              shuffle=False)
  
      dataloader_to_explain = DataLoader(dataset_to_explain,
                              batch_size=32,
                              shuffle=False)
  
      resnet = models.resnet50(pretrained=True)
      resnet.fc = torch.nn.Linear(2048, 5)
      resnet.load_state_dict(torch.load(model_path))
      resnet.eval()
  
      batch = next(iter(dataloader))
      images = batch['image'].expand(-1, 3, -1, -1)
  
      # select a set of background examples to take an expectation over
      background = images[:100]  # dataset[0]['image']
      # background = background.expand(3, -1, -1).unsqueeze(0)
  
      batch = next(iter(dataloader_to_explain))
      test_images = batch['image'].expand(-1, 3, -1, -1) # dataset_to_explain[1]['image']
      # test_images = test_images.expand(3, -1, -1).unsqueeze(0)
  
      e = shap.DeepExplainer(resnet, background)
      shap_values = e.shap_values(test_images)
  
      shap_numpy = [np.swapaxes(np.swapaxes(s, 1, -1), 1, 2) for s in shap_values]
      test_numpy = np.swapaxes(np.swapaxes(test_images[:3].numpy(), 1, -1), 1, 2)
  
      # plot the feature attributions
      shap.image_plot(shap_numpy, -test_numpy)
  </code></pre>
  <p>and the traceback error is:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;/.../PycharmProjects/thesis/SHAP.py&quot;, line 133, in &lt;module&gt;
      main()
    File &quot;.../PycharmProjects/thesis/SHAP.py&quot;, line 123, in main
      shap_values = e.shap_values(test_images)
    File &quot;.../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/__init__.py&quot;, line 124, in shap_values
      return self.explainer.shap_values(X, ranked_outputs, output_rank_order, check_additivity=check_additivity)
    File &quot;.../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py&quot;, line 185, in shap_values
      sample_phis = self.gradient(feature_ind, joint_x)
    File &quot;.../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py&quot;, line 121, in gradient
      grad = torch.autograd.grad(selected, x,
    File &quot;.../anaconda3/lib/python3.8/site-packages/torch/autograd/__init__.py&quot;, line 202, in grad
      return Variable._execution_engine.run_backward(
    File &quot;.../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py&quot;, line 226, in deeplift_grad
      return op_handler[module_type](module, grad_input, grad_output)
    File &quot;.../anaconda3/lib/python3.8/site-packages/shap/explainers/_deep/deep_pytorch.py&quot;, line 358, in nonlinear_1d
      grad_output[0] * (delta_out / delta_in).repeat(dup0))
  RuntimeError: The size of tensor a (512) must match the size of tensor b (2048) at non-singleton dimension 1
  </code></pre>
  <p>Does anyone have any ideas what could be causing this? I have reviewed the code so many times and I cannot find where it goes wrong... There shouldn't be mismatches in Tensor sizes on a trained network architecture.</p>
  <p>Note that I have intentionally removed any personally identifying paths :).</p>
  <p>Many thanks for any input!</p>