Robust layer extraction for GradCAM
Browse files- xai_engine/methods.py +5 -1
xai_engine/methods.py
CHANGED
|
@@ -67,10 +67,14 @@ def integrated_gradients(wrapper, input_tensor, steps=20) -> tuple[np.ndarray, f
|
|
| 67 |
|
| 68 |
def vit_grad_cam(wrapper, input_tensor, target_layer_idx=11) -> np.ndarray:
|
| 69 |
"""GradCAM for ViT using pytorch_grad_cam with custom reshape transform."""
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
target_layer = [layer.output if hasattr(layer, "output") else layer]
|
| 72 |
|
| 73 |
|
|
|
|
| 74 |
def reshape_transform(tensor):
|
| 75 |
# tensor shape: (B, 577, 384) -> drop CLS token -> (B, 576, 384) -> (B, 24, 24, 384) -> (B, 384, 24, 24)
|
| 76 |
result = tensor[:, 1:, :].reshape(tensor.size(0), 24, 24, tensor.size(2))
|
|
|
|
| 67 |
|
| 68 |
def vit_grad_cam(wrapper, input_tensor, target_layer_idx=11) -> np.ndarray:
|
| 69 |
"""GradCAM for ViT using pytorch_grad_cam with custom reshape transform."""
|
| 70 |
+
model = wrapper.model if hasattr(wrapper, "model") else wrapper
|
| 71 |
+
vit_base = model.vit if hasattr(model, "vit") else model
|
| 72 |
+
encoder = vit_base.encoder if hasattr(vit_base, "encoder") else vit_base
|
| 73 |
+
layer = encoder.layer[target_layer_idx]
|
| 74 |
target_layer = [layer.output if hasattr(layer, "output") else layer]
|
| 75 |
|
| 76 |
|
| 77 |
+
|
| 78 |
def reshape_transform(tensor):
|
| 79 |
# tensor shape: (B, 577, 384) -> drop CLS token -> (B, 576, 384) -> (B, 24, 24, 384) -> (B, 384, 24, 24)
|
| 80 |
result = tensor[:, 1:, :].reshape(tensor.size(0), 24, 24, tensor.size(2))
|