Skip to content

Commit a165a40

Browse files
committed
clip, kernel pca
1 parent 3018ba3 commit a165a40

File tree

7 files changed

+21
-183
lines changed

7 files changed

+21
-183
lines changed

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ The aim is also to serve as a benchmark of algorithms and metrics for research o
4444
| EigenGradCAM | Like EigenCAM but with class discrimination: First principle component of Activations*Grad. Looks like GradCAM, but cleaner |
4545
| LayerCAM | Spatially weight the activations by positive gradients. Works better especially in lower layers |
4646
| FullGrad | Computes the gradients of the biases from all over the network, and then sums them |
47-
| Deep Feature Factorizations | Non Negative Matrix Factorization on the 2D activations |
48-
47+
| Deep Feature Factorizations | Non Negative Matrix Factorization on the 2D activations |
48+
| KPCA-CAM | Like EigenCAM but with Kernel PCA instead of PCA
4949
## Visual Examples
5050

5151
| What makes the network think the image label is 'pug, pug-dog' | What makes the network think the image label is 'tabby, tabby cat' | Combining Grad-CAM with Guided Backpropagation for the 'pug, pug-dog' class |
@@ -68,6 +68,11 @@ The aim is also to serve as a benchmark of algorithms and metrics for research o
6868
<img src="./examples/dff1.png">
6969
<img src="./examples/dff2.png">
7070

71+
## CLIP
72+
| Explaining the text prompt "a dog" | "a cat" |
73+
| ---------------------------------------------------------------|--------------------|-----------------------------------------------------------------------------|
74+
<img src="https://github.com/jacobgil/pytorch-grad-cam/blob/master/examples/clip_dog.jpg?raw=true" width="256" height="256"> | <img src="https://github.com/jacobgil/pytorch-grad-cam/blob/master/examples/clip_cat.jpg?raw=true" width="256" height="256"> |
75+
7176
## Classification
7277

7378
#### Resnet50:
@@ -348,3 +353,8 @@ Suraj Srinivas, Francois Fleuret`
348353
https://arxiv.org/abs/1806.10206 <br>
349354
`Deep Feature Factorization For Concept Discovery
350355
Edo Collins, Radhakrishna Achanta, Sabine Süsstrunk`
356+
357+
https://arxiv.org/abs/2410.00267 <br>
358+
`KPCA-CAM: Visual Explainability of Deep Computer Vision Models using Kernel PCA
359+
360+
Sachin Karmani, Thanushon Sivakaran, Gaurav Prasad, Mehmet Ali, Wenbo Yang, Sheyang Tang`

examples/clip_cat.jpg

18.8 KB
Loading

examples/clip_dog.jpg

19.9 KB
Loading

requirements.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ ttach
66
tqdm
77
opencv-python
88
matplotlib
9-
scikit-learn
10-
transformers
9+
scikit-learn

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setuptools.setup(
1010
name='grad-cam',
11-
version='1.5.2',
11+
version='1.5.4',
1212
author='Jacob Gildenblat',
1313
author_email='[email protected]',
1414
description='Many Class Activation Map methods implemented in Pytorch for classification, segmentation, object detection and more',

usage_examples/clip_example

-168
This file was deleted.

usage_examples/vit_example.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121

2222
def get_args():
2323
parser = argparse.ArgumentParser()
24-
parser.add_argument('--use-cuda', action='store_true', default=False,
25-
help='Use NVIDIA GPU acceleration')
24+
parser.add_argument('--device', type=str, default='cpu',
25+
help='Torch device to use')
26+
2627
parser.add_argument(
2728
'--image-path',
2829
type=str,
@@ -43,9 +44,8 @@ def get_args():
4344
help='Can be gradcam/gradcam++/scorecam/xgradcam/ablationcam')
4445

4546
args = parser.parse_args()
46-
args.use_cuda = args.use_cuda and torch.cuda.is_available()
47-
if args.use_cuda:
48-
print('Using GPU for acceleration')
47+
if args.device:
48+
print(f'Using device "{args.device}" for acceleration')
4949
else:
5050
print('Using CPU for computation')
5151

@@ -84,11 +84,8 @@ def reshape_transform(tensor, height=14, width=14):
8484
raise Exception(f"method should be one of {list(methods.keys())}")
8585

8686
model = torch.hub.load('facebookresearch/deit:main',
87-
'deit_tiny_patch16_224', pretrained=True)
88-
model.eval()
87+
'deit_tiny_patch16_224', pretrained=True).to(torch.device(args.device)).eval()
8988

90-
if args.use_cuda:
91-
model = model.cuda()
9289

9390
target_layers = [model.blocks[-1].norm1]
9491

@@ -109,7 +106,7 @@ def reshape_transform(tensor, height=14, width=14):
109106
rgb_img = cv2.resize(rgb_img, (224, 224))
110107
rgb_img = np.float32(rgb_img) / 255
111108
input_tensor = preprocess_image(rgb_img, mean=[0.5, 0.5, 0.5],
112-
std=[0.5, 0.5, 0.5])
109+
std=[0.5, 0.5, 0.5]).to(args.device)
113110

114111
# If None, returns the map for the highest scoring category.
115112
# Otherwise, targets the requested category.

0 commit comments

Comments
 (0)