### Example Script Usage Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md Run the example script with a specified image path, CAM method, and output directory. ```bash python cam.py --image-path --method --output-dir ``` -------------------------------- ### Example Script Usage with Device Specification Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md Run the example script specifying a device like CPU, CUDA, MPS, or HPU. ```bash python cam.py --image-path --device cuda --output-dir ``` -------------------------------- ### Guided Backpropagation with PyTorch Grad-CAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md This snippet demonstrates how to use GuidedBackpropReLUModel for guided backpropagation. It requires the model, input tensor, and optionally a target category. ```python from pytorch_grad_cam import GuidedBackpropReLUModel from pytorch_grad_cam.utils.image import ( show_cam_on_image, deprocess_image, preprocess_image ) gb_model = GuidedBackpropReLUModel(model=model, device=model.device()) gb = gb_model(input_tensor, target_category=None) cam_mask = cv2.merge([grayscale_cam, grayscale_cam, grayscale_cam]) cam_gb = deprocess_image(cam_mask * gb) result = deprocess_image(gb) ``` -------------------------------- ### BibTeX Citation Example Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md Example BibTeX entry for citing the PyTorch Grad-CAM library in research. ```bibtex @misc{jacobgilpytorchcam, title={PyTorch library for CAM methods}, author={Jacob Gildenblat and contributors}, year={2021}, publisher={GitHub}, howpublished={\url{https://github.com/jacobgil/pytorch-grad-cam}}, } ``` -------------------------------- ### SegFormer Grad-CAM and DFF Example Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb Shows how to use Grad-CAM and DFF with a SegFormer model. A specific reshape transform is defined to handle the model's output format. ```python from transformers import SegformerForImageClassification from functools import partial def segformer_reshape_transform_huggingface(tensor, width, height): result = tensor.reshape(tensor.size(0), height, width, tensor.size(2)) # Bring the channels to the first dimension, # like in CNNs. result = result.transpose(2, 3).transpose(1, 2) return result reshape_transform = partial(segformer_reshape_transform_huggingface, width=img_tensor.shape[2]//32, height=img_tensor.shape[1]//32) model = SegformerForImageClassification.from_pretrained("nvidia/mit-b0") targets_for_gradcam = [ClassifierOutputTarget(category_name_to_index(model, "Egyptian cat")), ClassifierOutputTarget(category_name_to_index(model, "remote control, remote"))] target_layer = model.segformer.encoder.layer_norm[-1] display(Image.fromarray(run_dff_on_image(model=model, target_layer=target_layer, classifier=model.classifier, img_pil=image, img_tensor=img_tensor, reshape_transform=reshape_transform, n_components=4, top_k=2))) display(Image.fromarray(run_grad_cam_on_image(model=model, target_layer=target_layer, targets_for_gradcam=targets_for_gradcam, reshape_transform=reshape_transform))) print_top_categories(model, img_tensor) ``` -------------------------------- ### Swin Transformer Grad-CAM and DFF Example Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb Demonstrates Grad-CAM and DFF with a Swin Transformer model from Hugging Face. Includes a custom reshape transform function for the model's output. ```python from transformers import SwinForImageClassification from functools import partial def swinT_reshape_transform_huggingface(tensor, width, height): result = tensor.reshape(tensor.size(0), height, width, tensor.size(2)) result = result.transpose(2, 3).transpose(1, 2) return result model = SwinForImageClassification.from_pretrained("microsoft/swin-large-patch4-window12-384-in22k") target_layer = model.swin.layernorm targets_for_gradcam = [ClassifierOutputTarget(category_name_to_index(model, "Egyptian_cat")), ClassifierOutputTarget(category_name_to_index(model, "remote_control, remote"))] reshape_transform = partial(swinT_reshape_transform_huggingface, width=img_tensor.shape[2]//32, height=img_tensor.shape[1]//32) display(Image.fromarray(run_dff_on_image(model=model, target_layer=target_layer, classifier=model.classifier, img_pil=image, img_tensor=img_tensor, reshape_transform=reshape_transform, n_components=4, top_k=2))) display(Image.fromarray(run_grad_cam_on_image(model=model, target_layer=target_layer, targets_for_gradcam=targets_for_gradcam, reshape_transform=reshape_transform))) print_top_categories(model, img_tensor) ``` -------------------------------- ### ResNet Grad-CAM and DFF Example Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb Applies Grad-CAM and DFF to a ResNet model. Ensure the model, image, and tensor are properly loaded and defined. ```python target_layer = model.resnet.encoder.stages[-1].layers[-1] display(Image.fromarray(run_dff_on_image(model=model, target_layer=target_layer, classifier=model.classifier, img_pil=image, img_tensor=img_tensor, reshape_transform=None, n_components=4, top_k=2))) display(Image.fromarray(run_grad_cam_on_image(model=model, target_layer=target_layer, targets_for_gradcam=targets_for_gradcam, reshape_transform=None))) print_top_categories(model, img_tensor) ``` -------------------------------- ### Grad-CAM and DFF on RegNet (facebook/regnet-y-040) Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb Demonstrates applying DFF and Grad-CAM to a RegNet model. This example uses a different target layer and assumes the image and tensor are already prepared. ```python from transformers import RegNetForImageClassification model = RegNetForImageClassification.from_pretrained("facebook/regnet-y-040") target_layer = model.regnet.encoder.stages[-1] targets_for_gradcam = [ClassifierOutputTarget(category_name_to_index(model, "Egyptian cat")), ClassifierOutputTarget(category_name_to_index(model, "remote control, remote"))] display(Image.fromarray(run_dff_on_image(model=model, target_layer=target_layer, classifier=model.classifier, img_pil=image, img_tensor=img_tensor, reshape_transform=None, n_components=4, top_k=2))) display(Image.fromarray(run_grad_cam_on_image(model=model, target_layer=target_layer, targets_for_gradcam=targets_for_gradcam, reshape_transform=None))) print_top_categories(model, tensor_resized) ``` -------------------------------- ### Grad-CAM and ScoreCAM on MobileViT (apple/mobilevit-small) Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb Applies DFF, Grad-CAM, and ScoreCAM to a MobileViT model. This example highlights the flexibility of using different CAM methods and target layers within the same model architecture. ```python from transformers import MobileViTForImageClassification from pytorch_grad_cam import ScoreCAM model = MobileViTForImageClassification.from_pretrained("apple/mobilevit-small") target_layer = model.mobilevit.conv_1x1_exp targets_for_gradcam = [ClassifierOutputTarget(category_name_to_index(model, "Egyptian cat")), ClassifierOutputTarget(category_name_to_index(model, "remote control, remote"))] display(Image.fromarray(run_dff_on_image(model=model, target_layer=target_layer, classifier=model.classifier, img_pil=image, img_tensor=img_tensor, reshape_transform=None, n_components=4, top_k=2))) display(Image.fromarray(run_grad_cam_on_image(model=model, target_layer=target_layer, targets_for_gradcam=targets_for_gradcam, reshape_transform=None))) print('Now with ScoreCAM instead of GradCAM:') display(Image.fromarray(run_grad_cam_on_image(model=model, target_layer=model.mobilevit.encoder.layer[-1], targets_for_gradcam=targets_for_gradcam, reshape_transform=None, method=ScoreCAM))) print_top_categories(model, img_tensor) ``` -------------------------------- ### Load Dataset and Image Tensor Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb Loads the 'cats-image' dataset and converts the first image to a PyTorch tensor. Ensure you have the 'datasets' and 'torchvision' libraries installed. ```python import warnings warnings.filterwarnings('ignore') from torchvision import transforms from datasets import load_dataset from pytorch_grad_cam import run_dff_on_image, GradCAM from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget from pytorch_grad_cam.utils.image import show_cam_on_image from PIL import Image import numpy as np import cv2 import torch from typing import List, Callable, Optional dataset = load_dataset("huggingface/cats-image") image = dataset["test"]["image"][0] img_tensor = transforms.ToTensor()(image) ``` -------------------------------- ### Import necessary libraries for PyTorch Grad-CAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Imports common libraries used in PyTorch for image processing, model manipulation, and the Grad-CAM utility. Ensure these are installed before running. ```python import warnings warnings.filterwarnings('ignore') warnings.simplefilter('ignore') from torchvision.models.segmentation import deeplabv3_resnet50 import torch import torch.functional as F import numpy as np import requests import cv2 import torchvision from PIL import Image from pytorch_grad_cam.utils.image import show_cam_on_image, preprocess_image from pytorch_grad_cam import GradCAM ``` -------------------------------- ### Get Image from URL Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Downloads an image from a URL, converts it to a NumPy array, resizes it, and preprocesses it into a PyTorch tensor suitable for model input. Requires `numpy`, `PIL`, `requests`, and `cv2`. ```python def get_image_from_url(url): """A function that gets a URL of an image, and returns a numpy image and a preprocessed torch tensor ready to pass to the model """ img = np.array(Image.open(requests.get(url, stream=True).raw)) img = cv2.resize(img, (512, 512)) rgb_img_float = np.float32(img) / 255 input_tensor = preprocess_image(rgb_img_float, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) return img, rgb_img_float, input_tensor ``` -------------------------------- ### Define Grad-CAM Targets Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb Sets up the target categories for Grad-CAM analysis. This example specifies 'Egyptian cat' and 'remote control, remote' as targets, using the `category_name_to_index` helper function. ```python # We will show GradCAM for the "Egyptian Cat" and the 'Remote Control" categories: targets_for_gradcam = [ClassifierOutputTarget(category_name_to_index(model, "Egyptian cat")), ClassifierOutputTarget(category_name_to_index(model, "remote control, remote"))] ``` -------------------------------- ### Generate Grad-CAM for Non-Car Regions Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Apply Grad-CAM to identify regions in an image that are not cars. This snippet is similar to the non-cloud example but targets different objects. ```python with GradCAM(model=model, target_layers=target_layers, use_cuda=False) as cam: not_car_grayscale_cam = cam(input_tensor=input_tensor, targets=not_car_targets)[0, :] cam_image = show_cam_on_image(image_float, not_car_grayscale_cam, use_rgb=True) Image.fromarray(cam_image) ``` -------------------------------- ### Initialize and Run EigenCAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/EigenCAM for YOLO5.ipynb Initializes the EigenCAM model with the YOLOv5 model and target layers. It then computes the grayscale activation map for the input tensor. ```python cam = EigenCAM(model, target_layers) grayscale_cam = cam(tensor)[0, :, :] ``` -------------------------------- ### Run Benchmark with Default Settings Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Executes the benchmark function with default settings and a fixed random seed. ```python np.random.seed(42) benchmark(input_tensor, target_layers, eigen_smooth=False, aug_smooth=False) ``` -------------------------------- ### Initialize and Run AblationCAM for Faster R-CNN Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Class Activation Maps for Object Detection With Faster RCNN.ipynb Initializes AblationCAM with the Faster R-CNN ablation layer and computes the grayscale CAM. This method is useful for understanding feature importance in object detection models. ```python from pytorch_grad_cam import AblationCAM from pytorch_grad_cam.ablation_layer import AblationLayerFasterRCNN cam = AblationCAM(model, target_layers, use_cuda=torch.cuda.is_available(), reshape_transform=fasterrcnn_reshape_transform, ablation_layer=AblationLayerFasterRCNN()) grayscale_cam = cam(input_tensor, targets=targets)[0, :] Image.fromarray(renormalize_cam_in_bounding_boxes(boxes, image_float_np, grayscale_cam)) ``` -------------------------------- ### Apply Thresholded CAM and Visualize Perturbation Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb This snippet demonstrates how to apply a threshold to a grayscale CAM, create a binary mask, and then use this mask to perturb the input image. It calculates the change in model confidence and visualizes the perturbed image. Requires NumPy and PIL.Image. ```python thresholded_cam = grayscale_cams < np.percentile(grayscale_cams, 75) scores, visualizations = CamMultImageConfidenceChange()(input_tensor, thresholded_cam, targets, model, return_visualization=True) score = scores[0] visualization = visualizations[0].cpu().numpy().transpose((1, 2, 0)) visualization = deprocess_image(visualization) print(f"The confidence increase: {score}") print("The visualization of the pertubated image for the metric:") Image.fromarray(visualization) ``` -------------------------------- ### Importing Various CAM Methods Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Imports multiple Class Activation Mapping (CAM) techniques from the pytorch_grad_cam library, including GradCAM, GradCAMPlusPlus, EigenGradCAM, AblationCAM, and RandomCAM, for comparison and evaluation. ```python from pytorch_grad_cam import GradCAM, GradCAMPlusPlus, EigenGradCAM, AblationCAM, RandomCAM ``` -------------------------------- ### Load image and model for detection Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Class Activation Maps for Object Detection With Faster RCNN.ipynb Loads an image from a URL, preprocesses it, and sets up a pre-trained Faster R-CNN model for object detection. It also moves the model and input tensor to the appropriate device (GPU or CPU). ```python import requests import torchvision from PIL import Image image_url = "https://raw.githubusercontent.com/jacobgil/pytorch-grad-cam/master/examples/both.png" image = np.array(Image.open(requests.get(image_url, stream=True).raw)) image_float_np = np.float32(image) / 255 # define the torchvision image transforms transform = torchvision.transforms.Compose([ torchvision.transforms.ToTensor(), ]) input_tensor = transform(image) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') input_tensor = input_tensor.to(device) # Add a batch dimension: input_tensor = input_tensor.unsqueeze(0) model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) model.eval().to(device) # Run the model and display the detections boxes, classes, labels, indices = predict(input_tensor, model, device, 0.9) image = draw_boxes(boxes, labels, classes, image) # Show the image: Image.fromarray(image) ``` -------------------------------- ### Generate CAM with GradCAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md This snippet shows how to initialize and use the GradCAM class to generate a Class Activation Map. It includes model loading, target layer specification, input tensor preparation, and visualization of the CAM on the input image. Use this for standard CAM generation. ```python from pytorch_grad_cam import GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget from pytorch_grad_cam.utils.image import show_cam_on_image from torchvision.models import resnet50, ResNet50_Weights model = resnet50(weights=ResNet50_Weights.DEFAULT) target_layers = [model.layer4[-1]] input_tensor = # Create an input tensor image for your model.. # Note: input_tensor can be a batch tensor with several images! # We have to specify the target we want to generate the CAM for. targets = [ClassifierOutputTarget(281)] # Construct the CAM object once, and then re-use it on many images. with GradCAM(model=model, target_layers=target_layers) as cam: # You can also pass aug_smooth=True and eigen_smooth=True, to apply smoothing. grayscale_cam = cam(input_tensor=input_tensor, targets=targets) # In this example grayscale_cam has only one image in the batch: grayscale_cam = grayscale_cam[0, :] visualization = show_cam_on_image(rgb_img, grayscale_cam, use_rgb=True) # You can also get the model outputs without having to redo inference model_outputs = cam.outputs ``` -------------------------------- ### Visualize Image with n_components=10 Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Deep Feature Factorizations.ipynb Visualizes image features using PyTorch Grad-CAM with 10 components. A higher number of components may lead to over-segmentation and more esoteric concepts. ```python display(Image.fromarray(visualize_image(model, "https://th.bing.com/th/id/R.94b33a074b9ceeb27b1c7fba0f66db74?rik=wN27mvigyFlXGg&riu=http%3a%2f%2fimages5.fanpop.com%2fimage%2fphotos%2f31400000%2fBear-Wallpaper-bears-31446777-1600-1200.jpg&ehk=oD0JPpRVTZZ6yizZtGQtnsBGK2pAap2xv3sU3A4bIMc%3d&risl=&pid=ImgRaw&r=0", n_components=10))) ``` -------------------------------- ### Generate and Visualize Grad-CAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb This snippet shows how to load a pre-trained ResNet50 model, preprocess an image, generate a Grad-CAM, and visualize it overlaid on the original image. ```python import warnings warnings.filterwarnings('ignore') from torchvision import models import numpy as np import cv2 import requests from pytorch_grad_cam import GradCAM from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget from pytorch_grad_cam.utils.image import show_cam_on_image, \ deprocess_image, \ preprocess_image from PIL import Image model = models.resnet50(pretrained=True) model.eval() image_url = "https://th.bing.com/th/id/R.94b33a074b9ceeb27b1c7fba0f66db74?rik=wN27mvigyFlXGg&riu=http%3a%2f%2fimages5.fanpop.com%2fimage%2fphotos%2f31400000%2fBear-Wallpaper-bears-31446777-1600-1200.jpg&ehk=oD0JPpRVTZZ6yizZtGQtnsBGK2pAap2xv3sU3A4bIMc%3d&risl=&pid=ImgRaw&r=0" img = np.array(Image.open(requests.get(image_url, stream=True).raw)) img = cv2.resize(img, (224, 224)) img = np.float32(img) / 255 input_tensor = preprocess_image(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # The target for the CAM is the Bear category. # As usual for classication, the target is the logit output # before softmax, for that category. targets = [ClassifierOutputTarget(295)] target_layers = [model.layer4] with GradCAM(model=model, target_layers=target_layers) as cam: grayscale_cams = cam(input_tensor=input_tensor, targets=targets) cam_image = show_cam_on_image(img, grayscale_cams[0, :], use_rgb=True) cam = np.uint8(255*grayscale_cams[0, :]) cam = cv2.merge([cam, cam, cam]) images = np.hstack((np.uint8(255*img), cam , cam_image)) Image.fromarray(images) ``` -------------------------------- ### Load and Preprocess Image Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Loads an image from a URL, converts it to a NumPy array, and preprocesses it for model input. Requires Pillow and Requests. ```python cat_and_dog_image_url = "https://raw.githubusercontent.com/jacobgil/pytorch-grad-cam/master/examples/both.png" cat_and_dog = np.array(Image.open(requests.get(cat_and_dog_image_url, stream=True).raw)) cat_and_dog = np.float32(cat_and_dog) / 255 input_tensor = preprocess_image(cat_and_dog, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ``` -------------------------------- ### Evaluating Explanations with ROAD Metrics Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md This snippet demonstrates using ROAD metrics (MostRelevantFirst, LeastRelevantFirst) for evaluating explanations. It requires the input tensor, grayscale CAMs, targets, and the model. The percentile parameter can be adjusted. ```python # State of the art metric: Remove and Debias from pytorch_grad_cam.metrics.road import ROADMostRelevantFirst, ROADLeastRelevantFirst cam_metric = ROADMostRelevantFirst(percentile=75) scores, perturbation_visualizations = cam_metric(input_tensor, grayscale_cams, targets, model, return_visualization=True) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Class Activation Maps for Object Detection With Faster RCNN.ipynb Imports essential libraries for image processing, deep learning, and Grad-CAM. Ensures warnings are suppressed for cleaner output. ```python import warnings warnings.filterwarnings('ignore') warnings.simplefilter('ignore') import cv2 import numpy as np import torch import torchvision from pytorch_grad_cam import AblationCAM, EigenCAM from pytorch_grad_cam.ablation_layer import AblationLayerFasterRCNN from pytorch_grad_cam.utils.model_targets import FasterRCNNBoxScoreTarget from pytorch_grad_cam.utils.reshape_transforms import fasterrcnn_reshape_transform from pytorch_grad_cam.utils.image import show_cam_on_image, scale_accross_batch_and_channels, scale_cam_image ``` -------------------------------- ### Visualize Image with n_components=2 Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Deep Feature Factorizations.ipynb Visualizes image features using PyTorch Grad-CAM with 2 components. This setting aims for a balance between interpretability and detail. ```python display(Image.fromarray(visualize_image(model, "https://th.bing.com/th/id/R.94b33a074b9ceeb27b1c7fba0f66db74?rik=wN27mvigyFlXGg&riu=http%3a%2f%2fimages5.fanpop.com%2fimage%2fphotos%2f31400000%2fBear-Wallpaper-bears-31446777-1600-1200.jpg&ehk=oD0JPpRVTZZ6yizZtGQtnsBGK2pAap2xv3sU3A4bIMc%3d&risl=&pid=ImgRaw&r=0", n_components=2))) ``` -------------------------------- ### YOLOv5 Boilerplate and Detection Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/EigenCAM for YOLO5.ipynb Sets up necessary imports, defines utility functions for parsing and drawing detections, loads an image, preprocesses it, and performs object detection using the YOLOv5 model. Displays the detected objects on the image. ```python import warnings warnings.filterwarnings('ignore') warnings.simplefilter('ignore') import torch import cv2 import numpy as np import requests import torchvision.transforms as transforms from pytorch_grad_cam import EigenCAM from pytorch_grad_cam.utils.image import show_cam_on_image, scale_cam_image from PIL import Image COLORS = np.random.uniform(0, 255, size=(80, 3)) def parse_detections(results): detections = results.pandas().xyxy[0] detections = detections.to_dict() boxes, colors, names = [], [], [] for i in range(len(detections["xmin"]))...[0] # Truncated for brevity def draw_detections(boxes, colors, names, img): for box, color, name in zip(boxes, colors, names): xmin, ymin, xmax, ymax = box cv2.rectangle( img, (xmin, ymin), (xmax, ymax), color, 2) cv2.putText(img, name, (xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2, lineType=cv2.LINE_AA) return img image_url = "https://upload.wikimedia.org/wikipedia/commons/f/f1/Puppies_%284984818141%29.jpg" img = np.array(Image.open(requests.get(image_url, stream=True).raw)) img = cv2.resize(img, (640, 640)) rgb_img = img.copy() img = np.float32(img) / 255 transform = transforms.ToTensor() tensor = transform(img).unsqueeze(0) model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) model.eval() model.cpu() target_layers = [model.model.model.model[-2]] results = model([rgb_img]) boxes, colors, names = parse_detections(results) detections = draw_detections(boxes, colors, names, rgb_img.copy()) Image.fromarray(detections) ``` -------------------------------- ### Load and Preprocess Image for DeepLabV3 Segmentation Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Class Activation Maps for Semantic Segmentation.ipynb Loads an image from a URL, preprocesses it for the DeepLabV3 model, and sets up the model for evaluation. Includes handling for CUDA availability. ```python import warnings warnings.filterwarnings('ignore') warnings.simplefilter('ignore') from torchvision.models.segmentation import deeplabv3_resnet50 import torch import torch.functional as F import numpy as np import requests import torchvision from PIL import Image from pytorch_grad_cam.utils.image import show_cam_on_image, preprocess_image image_url = "https://farm1.staticflickr.com/6/9606553_ccc7518589_z.jpg" image = np.array(Image.open(requests.get(image_url, stream=True).raw)) rgb_img = np.float32(image) / 255 input_tensor = preprocess_image(rgb_img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Taken from the torchvision tutorial # https://pytorch.org/vision/stable/auto_examples/plot_visualization_utils.html model = deeplabv3_resnet50(pretrained=True, progress=False) model = model.eval() if torch.cuda.is_available(): model = model.cuda() input_tensor = input_tensor.cuda() output = model(input_tensor) print(type(output), output.keys()) ``` -------------------------------- ### Load and Display Query Image Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Loads the main query image from a URL and displays it using PIL. This is the image that will be analyzed for concept presence. ```python image, image_float, input_tensor = get_image_from_url("https://th.bing.com/th/id/R.c65135374de94dea2e2bf8fe0a4818e7?rik=Z75HF5uFr56PAw&pid=ImgRaw&r=0") Image.fromarray(image) ``` -------------------------------- ### Benchmark Dog Category with Grad-CAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Runs a benchmark using a specific dog category (index 246) with Grad-CAM. Requires numpy and benchmark function to be defined. ```python import numpy as np np.random.seed(0) benchmark(input_tensor, target_layers, category=246) ``` -------------------------------- ### Benchmark Grad-CAM Methods Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Benchmarks various Grad-CAM methods (GradCAM, GradCAM++, EigenGradCAM, AblationCAM, RandomCAM) on a given input tensor and target layers. Requires PyTorch, OpenCV, and Pillow. ```python def benchmark(input_tensor, target_layers, eigen_smooth=False, aug_smooth=False, category=281): methods = [("GradCAM", GradCAM(model=model, target_layers=target_layers, use_cuda=True)), ("GradCAM++", GradCAMPlusPlus(model=model, target_layers=target_layers, use_cuda=True)), ("EigenGradCAM", EigenGradCAM(model=model, target_layers=target_layers, use_cuda=True)), ("AblationCAM", AblationCAM(model=model, target_layers=target_layers, use_cuda=True)), ("RandomCAM", RandomCAM(model=model, target_layers=target_layers, use_cuda=True))] cam_metric = ROADCombined(percentiles=[20, 40, 60, 80]) targets = [ClassifierOutputTarget(category)] metric_targets = [ClassifierOutputSoftmaxTarget(category)] visualizations = [] percentiles = [10, 50, 90] for name, cam_method in methods: with cam_method: attributions = cam_method(input_tensor=input_tensor, targets=targets, eigen_smooth=eigen_smooth, aug_smooth=aug_smooth) attribution = attributions[0, :] scores = cam_metric(input_tensor, attributions, metric_targets, model) score = scores[0] visualization = show_cam_on_image(cat_and_dog, attribution, use_rgb=True) visualization = visualize_score(visualization, score, name, percentiles) visualizations.append(visualization) return Image.fromarray(np.hstack(visualizations)) ``` -------------------------------- ### Combine and Display Images Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Horizontally stack multiple images, including original images and Grad-CAM visualizations, for comparison. Requires NumPy and PIL. ```python Image.fromarray(np.hstack((cloud_img, car_img, image, car_cam_image, cloud_cam_image))) ``` -------------------------------- ### Calculate and Visualize CAM with Sobel Edge Detection Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Applies Sobel edge detection to generate a grayscale CAM, thresholds it, and then uses a CamMultImageConfidenceChange metric to evaluate the impact on model confidence. Visualizes the original and perturbed images. ```python from pytorch_grad_cam.sobel_cam import sobel_cam sobel_cam_grayscale = sobel_cam(np.uint8(img * 255)) thresholded_cam = sobel_cam_grayscale < np.percentile(sobel_cam_grayscale, 75) cam_metric = CamMultImageConfidenceChange() scores, visualizations = cam_metric(input_tensor, [thresholded_cam], targets, model, return_visualization=True) score = scores[0] visualization = visualizations[0].cpu().numpy().transpose((1, 2, 0)) visualization = deprocess_image(visualization) print(f"The confidence increase: {score}") print("The visualization of the pertubated image for the metric:") sobel_cam_rgb = cv2.merge([sobel_cam_grayscale, sobel_cam_grayscale, sobel_cam_grayscale]) Image.fromarray(np.hstack((sobel_cam_rgb, visualization))) ``` -------------------------------- ### Run Grad-CAM on Image Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/HuggingFace.ipynb A helper function to execute Grad-CAM on an input image and generate a visualization. It can handle multiple target categories and supports different Grad-CAM methods. ```python def run_grad_cam_on_image(model: torch.nn.Module, target_layer: torch.nn.Module, targets_for_gradcam: List[Callable], reshape_transform: Optional[Callable], input_tensor: torch.nn.Module=img_tensor, input_image: Image=image, method: Callable=GradCAM): with method(model=HuggingfaceToTensorModelWrapper(model), target_layers=[target_layer], reshape_transform=reshape_transform) as cam: # Replicate the tensor for each of the categories we want to create Grad-CAM for: repeated_tensor = input_tensor[None, :].repeat(len(targets_for_gradcam), 1, 1, 1) batch_results = cam(input_tensor=repeated_tensor, targets=targets_for_gradcam) results = [] for grayscale_cam in batch_results: visualization = show_cam_on_image(np.float32(input_image)/255, grayscale_cam, use_rgb=True) # Make it weight less in the notebook: visualization = cv2.resize(visualization, (visualization.shape[1]//2, visualization.shape[0]//2)) results.append(visualization) return np.hstack(results) ``` -------------------------------- ### Generate and Visualize Cloud Concept CAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Generates a Grad-CAM visualization for the 'cloud' concept in the query image. Similar to the car visualization, it uses the SimilarityToConceptTarget and overlays the CAM on the image. ```python # Where is the cloud in the image with GradCAM(model=model, target_layers=target_layers, use_cuda=False) as cam: cloud_grayscale_cam = cam(input_tensor=input_tensor, targets=cloud_targets)[0, :] cloud_cam_image = show_cam_on_image(image_float, cloud_grayscale_cam, use_rgb=True) Image.fromarray(cloud_cam_image) ``` -------------------------------- ### Directly Use Confidence Metrics Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb This snippet shows how to directly use the DropInConfidence and IncreaseInConfidence metrics to quantify changes in model confidence based on CAM. ```python from pytorch_grad_cam.metrics.cam_mult_image import DropInConfidence, IncreaseInConfidence print("Drop in confidence", DropInConfidence()(input_tensor, grayscale_cams, targets, model)) print("Increase in confidence", IncreaseInConfidence()(input_tensor, grayscale_cams, targets, model)) ``` -------------------------------- ### AblationCAM with 1% Channel Ablation Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Class Activation Maps for Object Detection With Faster RCNN.ipynb Applies AblationCAM with a `ratio_channels_to_ablate` set to 0.01, ablating only 1% of the channels. This configuration significantly speeds up the process, offering a substantial performance gain with minimal loss of detail. ```python ratio_channels_to_ablate = 0.01 cam = AblationCAM(model, target_layers, use_cuda=torch.cuda.is_available(), reshape_transform=fasterrcnn_reshape_transform, ablation_layer=AblationLayerFasterRCNN(), ratio_channels_to_ablate=ratio_channels_to_ablate) grayscale_cam = cam(input_tensor, targets=targets)[0, :] Image.fromarray(renormalize_cam_in_bounding_boxes(boxes, image_float_np, grayscale_cam)) ``` -------------------------------- ### Run Benchmark with Earlier Layer and Seed 0 Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Executes the benchmark function with an earlier target layer and a fixed random seed of 0. ```python np.random.seed(0) benchmark(input_tensor, target_layers) ``` -------------------------------- ### Load YOLOv5 Model Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/EigenCAM for YOLO5.ipynb Loads the pre-trained YOLOv5s model from PyTorch Hub. Ensure you have the 'ultralytics/yolov5' repository available. ```python model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) ``` -------------------------------- ### Visualize Image with Deep Feature Factorization Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Deep Feature Factorizations.ipynb Applies Deep Feature Factorization to an image and visualizes the factorized features on top of the original image. Requires the model, image URL, and optional parameters for number of components and top-k concepts. ```python from pytorch_grad_cam.utils.image import show_factorization_on_image def visualize_image(model, img_url, n_components=5, top_k=2): img, rgb_img_float, input_tensor = get_image_from_url(img_url) classifier = model.fc dff = DeepFeatureFactorization(model=model, target_layer=model.layer4, computation_on_concepts=classifier) concepts, batch_explanations, concept_outputs = dff(input_tensor, n_components) concept_outputs = torch.softmax(torch.from_numpy(concept_outputs), axis=-1).numpy() concept_label_strings = create_labels(concept_outputs, top_k=top_k) visualization = show_factorization_on_image(rgb_img_float, batch_explanations[0], image_weight=0.3, concept_labels=concept_label_strings) result = np.hstack((img, visualization)) # Just for the jupyter notebook, so the large images won't weight a lot: if result.shape[0] > 500: result = cv2.resize(result, (result.shape[1]//4, result.shape[0]//4)) return result ``` -------------------------------- ### Evaluating Explanations with ARCC Metric Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md This snippet demonstrates using the ARCC metric for evaluating explanations. It requires the input tensor, grayscale CAMs, targets, the model, and a base method (e.g., a CAM object). ```python # You can also use aggregate metrics such as ARCC from pytorch_grad_cam.metrics.ARCC import ARCC cam_metric = ARCC(base_method=cam) arcc_score = cam_metric(input_tensor, grayscale_cams, targets, model) ``` -------------------------------- ### Evaluating Explanations with Combined ROAD Metrics Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md This snippet shows how to use a combined ROAD metric by averaging across different percentiles. It requires the input tensor, grayscale CAMs, targets, and the model. Percentiles can be specified as a list. ```python # You can also average across different percentiles, and combine # (LeastRelevantFirst - MostRelevantFirst) / 2 from pytorch_grad_cam.metrics.road import ROADMostRelevantFirstAverage, ROADLeastRelevantFirstAverage, ROADCombined cam_metric = ROADCombined(percentiles=[20, 40, 60, 80]) scores = cam_metric(input_tensor, grayscale_cams, targets, model) ``` -------------------------------- ### Generate EigenCAM for Faster R-CNN Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Class Activation Maps for Object Detection With Faster RCNN.ipynb Initializes and runs EigenCAM on the Faster R-CNN model to generate class activation maps for detected objects. It then overlays the CAM on the original image and redraws bounding boxes. ```python target_layers = [model.backbone] targets = [FasterRCNNBoxScoreTarget(labels=labels, bounding_boxes=boxes)] cam = EigenCAM(model, target_layers, use_cuda=torch.cuda.is_available(), reshape_transform=fasterrcnn_reshape_transform) grayscale_cam = cam(input_tensor, targets=targets) # Take the first image in the batch: grayscale_cam = grayscale_cam[0, :] cam_image = show_cam_on_image(image_float_np, grayscale_cam, use_rgb=True) # And lets draw the boxes again: image_with_bounding_boxes = draw_boxes(boxes, labels, classes, cam_image) Image.fromarray(image_with_bounding_boxes) ``` -------------------------------- ### Load ResNet50 Model and Set to Evaluation Mode Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Deep Feature Factorizations.ipynb Loads a pre-trained ResNet50 model and sets it to evaluation mode. This is a prerequisite for using the model with Grad-CAM and DFF. ```python import warnings warnings.filterwarnings('ignore') from PIL import Image import numpy as np import requests import cv2 import json import ast import torch from pytorch_grad_cam import DeepFeatureFactorization from pytorch_grad_cam.utils.image import show_cam_on_image, preprocess_image, deprocess_image from pytorch_grad_cam import GradCAM from torchvision.models import resnet50 def get_image_from_url(url): """A function that gets a URL of an image, and returns a numpy image and a preprocessed torch tensor ready to pass to the model """ img = np.array(Image.open(requests.get(url, stream=True).raw)) rgb_img_float = np.float32(img) / 255 input_tensor = preprocess_image(rgb_img_float, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) return img, rgb_img_float, input_tensor def create_labels(concept_scores, top_k=2): """ Create a list with the image-net category names of the top scoring categories""" imagenet_categories_url = \ "https://gist.githubusercontent.com/yrevar/942d3a0ac09ec9e5eb3a/raw/238f720ff059c1f82f368259d1ca4ffa5dd8f9f5/imagenet1000_clsidx_to_labels.txt" labels = ast.literal_eval(requests.get(imagenet_categories_url).text) concept_categories = np.argsort(concept_scores, axis=1)[:, ::-1][:, :top_k] concept_labels_topk = [] for concept_index in range(concept_categories.shape[0]): categories = concept_categories[concept_index, :] concept_labels = [] for category in categories: score = concept_scores[concept_index, category] label = f"{labels[category].split(',')[0]}:{score:.2f}" concept_labels.append(label) concept_labels_topk.append("\n".join(concept_labels)) return concept_labels_topk model = resnet50(pretrained=True) model.eval() print("Loaded model") ``` -------------------------------- ### Evaluate Grad-CAM with Inverse CAM and Confidence Change Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb This snippet demonstrates evaluating the CAM by perturbing the image with an inverse CAM, aiming for a drop in confidence. It uses CamMultImageConfidenceChange with inverse CAM masks. ```python inverse_cams = 1 - grayscale_cams scores, visualizations = CamMultImageConfidenceChange()(input_tensor, inverse_cams, targets, model, return_visualization=True) score = scores[0] visualization = visualizations[0].cpu().numpy().transpose((1, 2, 0)) visualization = deprocess_image(visualization) print(f"The confidence increase percent: {score}") print("The visualization of the pertubated image for the metric:") Image.fromarray(visualization) ``` -------------------------------- ### Display CAM on Image Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/EigenCAM for YOLO5.ipynb Displays the generated CAM heatmap overlaid on the original image. Ensure 'img' and 'grayscale_cam' are pre-computed. ```python cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True) Image.fromarray(cam_image) ``` -------------------------------- ### Generate Grad-CAM for Non-Cloud Regions Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Use Grad-CAM to generate a heatmap highlighting areas that are not clouds. Ensure `use_cuda` is set appropriately for your environment. ```python with GradCAM(model=model, target_layers=target_layers, use_cuda=False) as cam: not_cloud_grayscale_cam = cam(input_tensor=input_tensor, targets=not_cloud_targets)[0, :] cam_image = show_cam_on_image(image_float, not_cloud_grayscale_cam, use_rgb=True) Image.fromarray(cam_image) ``` -------------------------------- ### Display Visualizations for Multiple Images Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Deep Feature Factorizations.ipynb Generates and displays visualizations using the `visualize_image` function for three different image URLs. This demonstrates the application of DFF on various image types. ```python display(Image.fromarray(visualize_image(model, "https://github.com/jacobgil/pytorch-grad-cam/blob/master/examples/both.png?raw=true"))) display(Image.fromarray(visualize_image(model, "https://raw.githubusercontent.com/jacobgil/pytorch-grad-cam/master/tutorials/puppies.jpg"))) display(Image.fromarray(visualize_image(model, "https://th.bing.com/th/id/R.94b33a074b9ceeb27b1c7fba0f66db74?rik=wN27mvigyFlXGg&riu=http%3a%2f%2fimages5.fanpop.com%2fimage%2fphotos%2f31400000%2fBear-Wallpaper-bears-31446777-1600-1200.jpg&ehk=oD0JPpRVTZZ6yizZtGQtnsBGK2pAap2xv3sU3A4bIMc%3d&risl=&pid=ImgRaw&r=0"))) ``` -------------------------------- ### Generate and Visualize Car Concept CAM Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Pixel Attribution for embeddings.ipynb Uses Grad-CAM to generate a Class Activation Map (CAM) highlighting the 'car' concept in the query image. It requires the model, target layer, input tensor, and a SimilarityToConceptTarget. The resulting CAM is then overlaid on the original image. ```python target_layers = [resnet.layer4[-1]] car_targets = [SimilarityToConceptTarget(car_concept_features)] # Where is the car in the image with GradCAM(model=model, target_layers=target_layers, use_cuda=False) as cam: car_grayscale_cam = cam(input_tensor=input_tensor, targets=car_targets)[0, :] car_cam_image = show_cam_on_image(image_float, car_grayscale_cam, use_rgb=True) Image.fromarray(car_cam_image) ``` -------------------------------- ### AblationCAM with 10% Channel Ablation Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/Class Activation Maps for Object Detection With Faster RCNN.ipynb Applies AblationCAM with a `ratio_channels_to_ablate` set to 0.1, which ablates 10% of the channels. This setting aims to speed up computation while retaining significant CAM information. ```python ratio_channels_to_ablate = 0.1 cam = AblationCAM(model, target_layers, use_cuda=torch.cuda.is_available(), reshape_transform=fasterrcnn_reshape_transform, ablation_layer=AblationLayerFasterRCNN(), ratio_channels_to_ablate=ratio_channels_to_ablate) grayscale_cam = cam(input_tensor, targets=targets)[0, :] Image.fromarray(renormalize_cam_in_bounding_boxes(boxes, image_float_np, grayscale_cam)) ``` -------------------------------- ### Evaluating Explanations with CamMultImageConfidenceChange Metric Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/README.md This snippet shows how to use the CamMultImageConfidenceChange metric to evaluate explanations by measuring confidence changes. It requires the input tensor, inverse CAMs, targets, and the model. ```python from pytorch_grad_cam.utils.model_targets import ClassifierOutputSoftmaxTarget from pytorch_grad_cam.metrics.cam_mult_image import CamMultImageConfidenceChange # Create the metric target, often the confidence drop in a score of some category metric_target = ClassifierOutputSoftmaxTarget(281) scores, batch_visualizations = CamMultImageConfidenceChange()(input_tensor, inverse_cams, targets, model, return_visualization=True) visualization = deprocess_image(batch_visualizations[0, :]) ``` -------------------------------- ### Run Benchmark with Earlier Layer Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/CAM Metrics And Tuning Tutorial.ipynb Runs the benchmark function using an earlier layer (model.layer4[-2]) as the target layer. ```python target_layers = [model.layer4[-2]] benchmark(input_tensor, target_layers) ``` -------------------------------- ### Swin Transformer Grad-CAM Initialization Source: https://github.com/jacobgil/pytorch-grad-cam/blob/master/tutorials/vision_transformers.md Initialize GradCAM for Swin Transformers using a custom reshape_transform function. This function reshapes the tensor output to a 7x7 spatial image with channels. ```python GradCAM(model=model, target_layers=target_layers, reshape_transform=reshape_transform) ``` ```python def reshape_transform(tensor, height=7, width=7): result = tensor.reshape(tensor.size(0), height, width, tensor.size(2)) # Bring the channels to the first dimension, # like in CNNs. result = result.transpose(2, 3).transpose(1, 2) return result ```