评价此页

使用 Captum 进行模型可解释性分析#

创建日期:2020年4月14日 | 最后更新:2023年9月26日 | 最后验证:未验证

Captum 帮助您了解数据特征如何影响模型预测或神经元激活,从而揭示模型的工作原理。

使用 Captum,您可以以统一的方式应用广泛的尖端特征归因算法,例如 Guided GradCamIntegrated Gradients

在本指南中,您将学习如何使用 Captum:

  • 将图像分类器的预测结果归因到相应的图像特征上。

  • 可视化归因结果。

准备工作#

请确保您的活动 Python 环境中已安装 Captum。Captum 可通过 GitHub、pip 包或 conda 包获取。有关详细说明,请参考安装指南 https://captum.meta-pytorch.cn/

对于模型,我们使用 PyTorch 内置的图像分类器。Captum 可以揭示样本图像的哪些部分支持模型所做的特定预测。

import torchvision
from torchvision import models, transforms
from PIL import Image
import requests
from io import BytesIO

model = torchvision.models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1).eval()

response = requests.get("https://image.freepik.com/free-photo/two-beautiful-puppies-cat-dog_58409-6024.jpg")
img = Image.open(BytesIO(response.content))

center_crop = transforms.Compose([
 transforms.Resize(256),
 transforms.CenterCrop(224),
])

normalize = transforms.Compose([
    transforms.ToTensor(),               # converts the image to a tensor with values between 0 and 1
    transforms.Normalize(                # normalize to follow 0-centered imagenet pixel RGB distribution
     mean=[0.485, 0.456, 0.406],
     std=[0.229, 0.224, 0.225]
    )
])
input_img = normalize(center_crop(img)).unsqueeze(0)

计算归因#

在模型的前 3 个预测中,包含对应狗和猫的类别 208 和 283。

让我们使用 Captum 的 Occlusion(遮挡)算法,将这些预测结果归因到输入图像的相应部分。

from captum.attr import Occlusion

occlusion = Occlusion(model)

strides = (3, 9, 9)               # smaller = more fine-grained attribution but slower
target=208,                       # Labrador index in ImageNet
sliding_window_shapes=(3,45, 45)  # choose size enough to change object appearance
baselines = 0                     # values to occlude the image with. 0 corresponds to gray

attribution_dog = occlusion.attribute(input_img,
                                       strides = strides,
                                       target=target,
                                       sliding_window_shapes=sliding_window_shapes,
                                       baselines=baselines)


target=283,                       # Persian cat index in ImageNet
attribution_cat = occlusion.attribute(input_img,
                                       strides = strides,
                                       target=target,
                                       sliding_window_shapes=sliding_window_shapes,
                                       baselines=0)

除了 Occlusion,Captum 还包含许多算法,如 Integrated GradientsDeconvolutionGuidedBackpropGuided GradCamDeepLiftGradientShap。所有这些算法都是 Attribution 的子类,它们在初始化时需要您的模型作为可调用的 forward_func,并拥有一个 attribute(...) 方法,该方法以统一的格式返回归因结果。

让我们可视化图像情况下的计算归因结果。

可视化结果#

Captum 的 visualization 工具提供了现成的方法,用于可视化图像和文本输入的归因结果。

import numpy as np
from captum.attr import visualization as viz

# Convert the compute attribution tensor into an image-like numpy array
attribution_dog = np.transpose(attribution_dog.squeeze().cpu().detach().numpy(), (1,2,0))

vis_types = ["heat_map", "original_image"]
vis_signs = ["all", "all"] # "positive", "negative", or "all" to show both
# positive attribution indicates that the presence of the area increases the prediction score
# negative attribution indicates distractor areas whose absence increases the score

_ = viz.visualize_image_attr_multiple(attribution_dog,
                                      np.array(center_crop(img)),
                                      vis_types,
                                      vis_signs,
                                      ["attribution for dog", "image"],
                                      show_colorbar = True
                                     )


attribution_cat = np.transpose(attribution_cat.squeeze().cpu().detach().numpy(), (1,2,0))

_ = viz.visualize_image_attr_multiple(attribution_cat,
                                      np.array(center_crop(img)),
                                      ["heat_map", "original_image"],
                                      ["all", "all"], # positive/negative attribution or all
                                      ["attribution for cat", "image"],
                                      show_colorbar = True
                                     )

如果您的数据是文本,visualization.visualize_text() 提供了专门的视图来探索输入文本上的归因。详情请查看 https://captum.meta-pytorch.cn/tutorials/IMDB_TorchText_Interpret

总结#

Captum 可以处理 PyTorch 中大多数类型的模型,涵盖视觉、文本等多种模态。使用 Captum,您可以:* 如上所述,将特定输出归因于模型输入。* 将特定输出归因于隐藏层神经元(请参阅 Captum API 参考)。* 将隐藏层神经元响应归因于模型输入(请参阅 Captum API 参考)。

有关受支持方法的完整 API 和教程列表,请访问我们的网站 https://captum.meta-pytorch.cn

Gilbert Tanner 的另一篇有用文章: https://gilberttanner.com/blog/interpreting-pytorch-models-with-captum