Skip to content

Instantly share code, notes, and snippets.

@giangnguyen2412
Created September 30, 2020 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giangnguyen2412/c5f0d6aa7d1b72f610a8d4c5b0f48324 to your computer and use it in GitHub Desktop.
Save giangnguyen2412/c5f0d6aa7d1b72f610a8d4c5b0f48324 to your computer and use it in GitHub Desktop.
SHAP Deep Explainer with ImageNet datas
import glob
# load the model
model = models.vgg16(pretrained=True).eval().to(device)
# Get the prediction for the input image
test_image_paths = glob.glob('/home/dexter/Downloads/Exp1/Exp1-1 100/*.*')
test_images = list(map(lambda x: Image.open(x), test_image_paths))
test_inputs = [Compose([Resize((224,224)), ToTensor(), image_net_preprocessing])(x).unsqueeze(0) for x in test_images] # add 1 dim for batch
test_inputs = [i.to(device) for i in test_inputs]
# SHAP
import shap
from utils import *
background = torch.cat(test_inputs[:3])
test_images = torch.cat(test_inputs[40:43])
e = shap.DeepExplainer(model, background)
shap_values = e.shap_values(test_images, ranked_outputs=1, output_rank_order='max') # 2xranked_outputsx1x3x224x224
shap_numpy = [np.swapaxes(np.swapaxes(s, 1, -1), 1, 2) for s in shap_values[0]]
test_numpy = np.swapaxes(np.swapaxes(test_images.to('cpu').numpy(), 1, -1), 1, 2)
print(shap_numpy[0].shape)
print(test_numpy.shape)
print(len(test_numpy))
print(len(shap_numpy[0]))
for i in range(len(test_numpy)):
max_val = max(np.min(test_numpy[i]), np.max(test_numpy[i]), key=abs)
test_numpy[i] = test_numpy[i]/max_val
max_val = max(np.min(shap_numpy[0][i]), np.max(shap_numpy[0][i]), key=abs)
shap_numpy[0][i] = shap_numpy[0][i]/max_val
shap.image_plot(shap_values=shap_numpy, pixel_values=test_numpy, show=True)
img = Image.open(test_image_paths[42])
input_image = img.resize((size,size), Image.ANTIALIAS)
fig = plt.figure()
plt.imshow(input_image)
shap.summary_plot(shap_values[0][0][0], test_images[0], show=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment