Need help using SHAP's DeepExplainer

I’m trying to use the shap explainer but I’m having trouble assembling the inputs properly. If there’s an example notebook that someone already setup that would be easiest but I’ll explain my issues below specifically.

First to initialize shap.DeepExplainer, we need to give it the model and a tensor that gets fed into the model. The tensor should contains the pixel values of multiple values stacked on top of each other, so the shape would be (n, 3, 224, 224), with n being number of images. One issue I’m having is that each image is a slightly different size. Is there a way I can get the transformed image tensors from the dataloader/Imagelist so that they are all the same size?

To get around that problem, temporarily, I just passed in a single image.
Then I had another problem, generating the shap_values. I pass in a single tensor from an image in the validation set but I get an error:

RuntimeError: Expected object of backend CPU but got backend CUDA for sequence element 1 in sequence argument at position #1 ‘tensors’

Not sure what to do about this.

Like I said it might be easiest if there was an example notebook using the shap library to point me to.

Edit:

So I solved the Runtime error mentioned above. I had to call cuda() on the tensor I was passing into shap_values(…). However I need to pass in the x AND y data so I’m working on that. I also still need a way to pass in tensor of images, but they have to be the same size to work.

Edit:
So just for reference, I wanted to add what code I had so far. This is mostly from the example code on the github link so look there for more details.

background = data.train_ds.x[np.random.choice(data.train_ds.x.items.size, 100, replace=False)][0].resize(224).data
e = shap.DeepExplainer(learn.model, background.unsqueeze(0).cuda())
shap_values = e.shap_values(data.valid_ds.x[0].resize(224).data.unsqueeze(0).cuda())

This last line is where I’m getting an error now:

AttributeError: 'ReLU' object has no attribute 'y'

Edit:
Sorry if I’m spamming, just wanna make sure there’s a reference for anyone else that has the same issue. I found an example notebook (I cloned the repo and opened it in jupyter to view it properly) in the shap github repo that uses pytorch and it was pretty easy to apply that to use with the fastai library.

batch = next(iter(data.train_dl))
images, _ = batch
background = images[:20]
test_images = images[20:24]
e = shap.DeepExplainer(learn.model, background)
shap_values = e.shap_values(test_images)

At that last line I get the relu/y error mentioned above which was referenced in a pull request and a fix applied, but not merged into a release yet. I think I’ll have to install it from clone version of the repo instead of conda.

2 Likes