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