image
绘图
本笔记本旨在演示(并记录)如何使用 shap.plots.image
函数。
[1]:
import json
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
import shap
# load pre-trained model and choose two images to explain
model = ResNet50(weights="imagenet")
def f(X):
tmp = X.copy()
preprocess_input(tmp)
return model(tmp)
X, y = shap.datasets.imagenet50()
# load the ImageNet class names as a vectorized mapping function from ids to names
url = "https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json"
with open(shap.datasets.cache(url)) as file:
class_names = [v[1] for v in json.load(file).values()]
# define a masker that is used to mask out partitions of the input image, this one uses a blurred background
masker = shap.maskers.Image("inpaint_telea", X[0].shape)
# By default the Partition explainer is used for all partition explainer
explainer = shap.Explainer(f, masker, output_names=class_names)
# here we use 500 evaluations of the underlying model to estimate the SHAP values
shap_values = explainer(
X[1:3], max_evals=500, batch_size=50, outputs=shap.Explanation.argsort.flip[:1]
)
shap.image_plot(shap_values)
explainers.Partition is still in an alpha state, so use with caution...
Partition explainer: 3it [00:18, 6.10s/it]
有更多有用示例的想法吗?我们鼓励提交增加此文档笔记本的拉取请求!