Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobgil committed Oct 7, 2024
1 parent 0f6c56b commit a6f7d2a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
7 changes: 4 additions & 3 deletions cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pytorch_grad_cam import (
GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus,
AblationCAM, XGradCAM, EigenCAM, EigenGradCAM,
LayerCAM, FullGrad, GradCAMElementWise
LayerCAM, FullGrad, GradCAMElementWise, KPCA_CAM
)
from pytorch_grad_cam import GuidedBackpropReLUModel
from pytorch_grad_cam.utils.image import (
Expand Down Expand Up @@ -37,7 +37,7 @@ def get_args():
'gradcam', 'hirescam', 'gradcam++',
'scorecam', 'xgradcam', 'ablationcam',
'eigencam', 'eigengradcam', 'layercam',
'fullgrad', 'gradcamelementwise'
'fullgrad', 'gradcamelementwise', 'kpcacam'
],
help='CAM method')

Expand Down Expand Up @@ -73,7 +73,8 @@ def get_args():
"eigengradcam": EigenGradCAM,
"layercam": LayerCAM,
"fullgrad": FullGrad,
"gradcamelementwise": GradCAMElementWise
"gradcamelementwise": GradCAMElementWise,
'kpcacam': KPCA_CAM
}

model = models.resnet50(pretrained=True).to(torch.device(args.device)).eval()
Expand Down
41 changes: 21 additions & 20 deletions pytorch_grad_cam/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from pytorch_grad_cam.grad_cam import GradCAM
from pytorch_grad_cam.hirescam import HiResCAM
from pytorch_grad_cam.grad_cam_elementwise import GradCAMElementWise
from pytorch_grad_cam.ablation_layer import AblationLayer, AblationLayerVit, AblationLayerFasterRCNN
from pytorch_grad_cam.ablation_cam import AblationCAM
from pytorch_grad_cam.xgrad_cam import XGradCAM
from pytorch_grad_cam.grad_cam_plusplus import GradCAMPlusPlus
from pytorch_grad_cam.score_cam import ScoreCAM
from pytorch_grad_cam.layer_cam import LayerCAM
from pytorch_grad_cam.eigen_cam import EigenCAM
from pytorch_grad_cam.eigen_grad_cam import EigenGradCAM
from pytorch_grad_cam.random_cam import RandomCAM
from pytorch_grad_cam.fullgrad_cam import FullGrad
from pytorch_grad_cam.guided_backprop import GuidedBackpropReLUModel
from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
from pytorch_grad_cam.feature_factorization.deep_feature_factorization import DeepFeatureFactorization, run_dff_on_image
import pytorch_grad_cam.utils.model_targets
import pytorch_grad_cam.utils.reshape_transforms
import pytorch_grad_cam.metrics.cam_mult_image
import pytorch_grad_cam.metrics.road
from pytorch_grad_cam.grad_cam import GradCAM
from pytorch_grad_cam.hirescam import HiResCAM
from pytorch_grad_cam.grad_cam_elementwise import GradCAMElementWise
from pytorch_grad_cam.ablation_layer import AblationLayer, AblationLayerVit, AblationLayerFasterRCNN
from pytorch_grad_cam.ablation_cam import AblationCAM
from pytorch_grad_cam.xgrad_cam import XGradCAM
from pytorch_grad_cam.grad_cam_plusplus import GradCAMPlusPlus
from pytorch_grad_cam.score_cam import ScoreCAM
from pytorch_grad_cam.layer_cam import LayerCAM
from pytorch_grad_cam.eigen_cam import EigenCAM
from pytorch_grad_cam.eigen_grad_cam import EigenGradCAM
from pytorch_grad_cam.kpca_cam import KPCA_CAM
from pytorch_grad_cam.random_cam import RandomCAM
from pytorch_grad_cam.fullgrad_cam import FullGrad
from pytorch_grad_cam.guided_backprop import GuidedBackpropReLUModel
from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
from pytorch_grad_cam.feature_factorization.deep_feature_factorization import DeepFeatureFactorization, run_dff_on_image
import pytorch_grad_cam.utils.model_targets
import pytorch_grad_cam.utils.reshape_transforms
import pytorch_grad_cam.metrics.cam_mult_image
import pytorch_grad_cam.metrics.road
5 changes: 3 additions & 2 deletions pytorch_grad_cam/kpca_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ def __init__(self, model, target_layers,
super(KPCA_CAM, self).__init__(model,
target_layers,
reshape_transform,
uses_gradients=False,
kernel=kernel, gamma=gamma)
uses_gradients=False)
self.kernel=kernel
self.gamma=gamma

def get_cam_image(self,
input_tensor,
Expand Down
7 changes: 5 additions & 2 deletions tests/test_run_all_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
EigenCAM, \
EigenGradCAM, \
LayerCAM, \
FullGrad
FullGrad, \
KPCA_CAM
from pytorch_grad_cam.utils.image import show_cam_on_image, \
preprocess_image
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
Expand Down Expand Up @@ -50,7 +51,9 @@ def numpy_image():
EigenCAM,
EigenGradCAM,
LayerCAM,
FullGrad])
FullGrad,
KPCA_CAM])

def test_all_cam_models_can_run(numpy_image, batch_size, width, height,
cnn_model, target_layer_names, cam_method,
target_category, aug_smooth, eigen_smooth):
Expand Down

0 comments on commit a6f7d2a

Please sign in to comment.