<jupyter_start><jupyter_text>TCAV for Model Explainability<jupyter_code>#!pip3 install torch torchvision
#!pip install captum
!wget https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt
import torch
model = torch.hub.load('pytorch/vision:v0.10.0', 'inception_v3',
                       pretrained=True)
model.eval()
import PIL.Image
import numpy as np
import matplotlib.pyplot as plt

def LoadImage(file_path, model='VGG'):
    im = PIL.Image.open(file_path)
    if model == 'Inception':
        im = im.resize((299,299))
    else: 
        im = im.resize((224,224))
    im = np.asarray(im)
    return im

def PreprocessVGGImage(im):
    im = tf.keras.applications.vgg16.preprocess_input(im)
    return im

def PreprocessInceptionImage(im):
    im = tf.keras.applications.inception_v3.preprocess_input(im)
    return im
# Load the image
im_orig = LoadImage('./images/cockatoo.jpeg')

plt.imshow(im_orig)
plt.show()
from torchvision import transforms
input_image = PIL.Image.open('./images/cockatoo.jpeg')
preprocess = transforms.Compose([
    transforms.Resize(299),
    transforms.CenterCrop(299),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_tensor = torch.Tensor(input_tensor)
input_batch = input_tensor.unsqueeze(0)
# move the input and model to GPU for speed if available
if torch.cuda.is_available():
    input_batch = input_batch.to('cuda')
    model.to('cuda')

with torch.no_grad():
    output = model(input_batch)

# Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
# print(output[0])
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
probabilities = torch.nn.functional.softmax(output[0], dim=0)
# print(probabilities)
# Read the categories
with open("imagenet_classes.txt", "r") as f:
    categories = [s.strip() for s in f.readlines()]
    
# Show top categories per image
top5_prob, top5_catid = torch.topk(probabilities, 5)
for i in range(top5_prob.size(0)):
    print(categories[top5_catid[i]], top5_prob[i].item())
from captum.attr import GuidedBackprop

gbp = GuidedBackprop(model)
attribution = gbp.attribute(input_batch, target=89)
input_batch.requires_grad=True
gbp = GuidedBackprop(model)
# Computes Guided Backprop attribution scores for class 89
attribution = gbp.attribute(input_batch, target=top5_catid[0])
from captum.attr import visualization as viz
from matplotlib.colors import LinearSegmentedColormap

default_cmap = LinearSegmentedColormap.from_list('custom blue', 
                                                 [(0, '#ffffff'),
                                                  (0.25, '#000000'),
                                                  (1, '#000000')], N=256)

_ = viz.visualize_image_attr_multiple(np.transpose(attribution.squeeze().cpu().detach().numpy(), (1,2,0)),
                                      im_orig,
                                      ["original_image", "heat_map"],
                                      ["all", "absolute_value"],
                                      cmap=default_cmap,
                                      show_colorbar=True)<jupyter_output><empty_output><jupyter_text>Guided Grad-CAM<jupyter_code>from captum.attr import GuidedGradCam

guided_gc = GuidedGradCam(model, model.Mixed_7c)
attribution = guided_gc.attribute(input_batch, target=top5_catid[0])
default_cmap = LinearSegmentedColormap.from_list('custom blue', 
                                                 [(0, '#ffffff'),
                                                  (0.25, '#000000'),
                                                  (1, '#000000')], N=256)

_ = viz.visualize_image_attr_multiple(np.transpose(attribution.squeeze().cpu().detach().numpy(), (1,2,0)),
                                      im_orig,
                                      ["original_image", "heat_map"],
                                      ["all", "absolute_value"],
                                      cmap=default_cmap,
                                      show_colorbar=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11459589867310012, 'fraction_numerical': 0.030156815440289506, 'mean_word_length': 2.4694560669456065, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '138516', 'license': 'Apache-2.0', 'n_tokens_mistral': 1351, 'n_tokens_neox': 1240, 'n_words': 249, 'path': '04-image/guided_backprop_pytorch.ipynb', 'repo_name': 'munnm/XAI-for-practitioners', 'size': '4145'}