自定义 CNN 模型问题上 SHAP 值的可解释性

发布于 2025-01-16 06:27:49 字数 3367 浏览 2 评论 0原文

我试图解释使用 SHAP 训练模型的预测。我遵循以下源代码,它可以在 Imagenet 数据集中使用 RESNET50 正常工作。

from tensorflow.keras.applications.resnet50 import ResNet50, 
preprocess_input

import json
import shap
import tensorflow as tf

# load pre-trained model and choose two images to explain
model = ResNet50(weights='imagenet')
def f(X):
    tmp = X.copy()
    print(tmp.shape)
    input()
    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()]
    
print(len(class_names))
print(X.shape)   
input()    

# 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)

这正是可以找到的示例 这里

现在我有另一个 RESNET50,但针对考虑 12 个类的另一个多类分类进行了训练。我修改了上面的源代码,但运行时遇到问题。

import numpy as np
import tensorflow.keras.models import load_model, preprocess_input
import shap

def f(data_to_explain):
    tmp = data_to_explain.copy()
    preprocess_input(tmp)
    return model(tmp)

reconstructed_model = load_model("my_model")
data_to_explain=np.load("data_to_use.npy")
class_names = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]

# 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", data_to_explain[0].shape)

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(data_to_explain[1:3], max_evals=500, batch_size=50, outputs=shap.Explanation.argsort.flip[:1])
shap.image_plot(shap_values)

我收到的错误是:

 shap_values = explainer(data_to_explain[1:3], max_evals=500, batch_size=50, outputs=shap.Explanation.argsort.flip[:1])
  File "/usr/local/lib/python3.8/dist-packages/shap/explainers/_partition.py", line 135, in __call__
    return super().__call__(
  File "/usr/local/lib/python3.8/dist-packages/shap/explainers/_explainer.py", line 310, in __call__
    sliced_labels = [labels[index_list] for index_list in output_indices]
  File "/usr/local/lib/python3.8/dist-packages/shap/explainers/_explainer.py", line 310, in <listcomp>
    sliced_labels = [labels[index_list] for index_list in output_indices]
IndexError: index 852 is out of bounds for axis 0 with size 12

代码与前一个基本相同,所有形状都匹配,唯一的区别是类的数量。那么,我的问题可能是什么?

Ps=您可以重现我的错误此处

I am trying to explain the predictions of a trained model using SHAP. I was following the following source code, that works correctly using RESNET50 in an Imagenet DATASET

from tensorflow.keras.applications.resnet50 import ResNet50, 
preprocess_input

import json
import shap
import tensorflow as tf

# load pre-trained model and choose two images to explain
model = ResNet50(weights='imagenet')
def f(X):
    tmp = X.copy()
    print(tmp.shape)
    input()
    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()]
    
print(len(class_names))
print(X.shape)   
input()    

# 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)

This is the exactly example that can be found HERE

Now I have another RESNET50, but trained for another multi-class classification considering 12 classes. I adapted the source code above, but I have issues when running it.

import numpy as np
import tensorflow.keras.models import load_model, preprocess_input
import shap

def f(data_to_explain):
    tmp = data_to_explain.copy()
    preprocess_input(tmp)
    return model(tmp)

reconstructed_model = load_model("my_model")
data_to_explain=np.load("data_to_use.npy")
class_names = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]

# 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", data_to_explain[0].shape)

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(data_to_explain[1:3], max_evals=500, batch_size=50, outputs=shap.Explanation.argsort.flip[:1])
shap.image_plot(shap_values)

the error that I receive is:

 shap_values = explainer(data_to_explain[1:3], max_evals=500, batch_size=50, outputs=shap.Explanation.argsort.flip[:1])
  File "/usr/local/lib/python3.8/dist-packages/shap/explainers/_partition.py", line 135, in __call__
    return super().__call__(
  File "/usr/local/lib/python3.8/dist-packages/shap/explainers/_explainer.py", line 310, in __call__
    sliced_labels = [labels[index_list] for index_list in output_indices]
  File "/usr/local/lib/python3.8/dist-packages/shap/explainers/_explainer.py", line 310, in <listcomp>
    sliced_labels = [labels[index_list] for index_list in output_indices]
IndexError: index 852 is out of bounds for axis 0 with size 12

The code is basically identical to the previous one, all shapes match and the only difference is the number of classes. So, what could be my problem?

P.s= you can reproduce my error HERE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2025-01-23 06:27:49
#Try it
for i in range(len(data_to_explain)):
    shap_values = explainer(np.asarray([data_to_explain[i]]), max_evals=500, outputs=shap.Explanation.argsort.flip[:1])
    shap.image_plot(shap_values)
#Try it
for i in range(len(data_to_explain)):
    shap_values = explainer(np.asarray([data_to_explain[i]]), max_evals=500, outputs=shap.Explanation.argsort.flip[:1])
    shap.image_plot(shap_values)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文