MODEL INTERPRETABILITY USING CAPTUM

Captum可帮助大家了解数据特征如何影响模型预测或神经元激活,从而阐明模型的运行方式。

使用Captum,可以以统一方式应用广泛的最新功能归因算法,例如Guided GradCam 和 ntegrated Gradients

接下来将学习如何使用Captum来:*将图像分类器的预测归因于其相应的图像特征。*可视化attribution 结果

Before you begin

确保Captum已安装在活动的Python环境中。Captum既可以在GitHub上作为pip软件包使用,也可以作为conda 软件包使用。有关详细说明,请参阅https://captum.ai/上的安装指南。

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

import torchvisionfrom torchvision import transformsfrom PIL import Imageimport requestsfrom io import BytesIOmodel = torchvision.models.resnet18(pretrained=True).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)

Computing Attribution

在模型的前三项预测中,有208和283类对应于狗和猫。

让我们使用CaptumOcclusion算法将这些预测中的每一个归因于输入的相应部分。

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 Gradients,Deconvolution,GuidedBackprop,Guided GradCam,DeepLift 和GradientShap 。所有这些算法都是其子类,这些子类Attribution期望模型forward_func在初始化时是可调用的,并且具有一种以统一格式返回归因结果的方法。

 

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

 

Visualizing the Results

Captum的visualization实用程序提供了开箱即用的方法来可视化图形输入和文本输入的归因结果。

 

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,
                                      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,
                                      center_crop(img),
                                      ["heat_map", "original_image"],
                                      ["all", "all"], # positive/negative attribution or all
                                      ["attribution for cat", "image"],
                                      show_colorbar = True
                                     )

如果数据是文本数据visualization.visualize_text(),请在输入文本的上方提供专用视图以探讨归因。在http://captum.ai/tutorials/IMDB_TorchText_Interpret中查找更多信息

 

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

有关支持的方法的完整API和教程列表,请访问网站http://captum.ai

​接下来,给大家介绍一下租用GPU做实验的方法,我们是在智星云租用的GPU,使用体验很好。具体大家可以参考:智星云官网: http://www.ai-galaxy.cn/,淘宝店:https://shop36573300.taobao.com/公众号: 智星AI

 

接下来,给大家介绍一下租用GPU做实验的方法,我们是在智星云租用的GPU,使用体验很好。具体大家可以参考:智星云官网: http://www.ai-galaxy.cn/,淘宝店:https://shop36573300.taobao.com/公众号: 智星AI

 

                                      

             

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值