shap.KernelExplainer, change 'mask_image' function for images with many zeros in the background

Viewed 115

I am new to python and this is my first time posting a question on stack overflow so I apologize if this will not be phrased correctly. I am trying to compute the SHAP values with KernelExplainer following the example 'ImageNet VGG16 Model with Keras' found at https://shap-lrjball.readthedocs.io/en/latest/example_notebooks/kernel_explainer/ImageNet%20VGG16%20Model%20with%20Keras.html . I am using medical type rgb images with a lot of zeros in the background. In the example, the author defines 'mask_image' as a function that depends on a binary mask representing if an image region is hidden. How can i modify the 'mask_image' function below such that i can keep the background values of the image as zeros ?

def mask_image(zs, segmentation, image, background=None):
    if background is None:
        background = image.mean((0,1))
    out = np.zeros((zs.shape[0], image.shape[0], image.shape[1], image.shape[2]))
    for i in range(zs.shape[0]):
        out[i,:,:,:] = image
        for j in range(zs.shape[1]):
            if zs[i,j] == 0:
                out[i][segmentation == j,:] = background
    return out
0 Answers
Related