File size: 10,980 Bytes
ebcb290
 
 
 
 
 
 
 
 
 
05f720a
ebcb290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
943d418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcb290
 
943d418
f920f48
 
ebcb290
304ab36
943d418
ebcb290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eea112
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcb290
9eea112
abba219
 
 
 
 
 
 
 
 
 
 
 
 
9eea112
 
ebcb290
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d3c44
 
 
 
ebcb290
 
 
 
 
 
 
 
 
 
 
93d3c44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcb290
93d3c44
 
 
 
 
 
 
 
 
 
ebcb290
05f720a
ebcb290
 
 
 
 
 
93d3c44
 
 
 
 
 
 
 
 
 
abba219
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import time
from dataclasses import dataclass
import numpy as np
import torch
from PIL import Image
from captum.attr import IntegratedGradients, Occlusion, DeepLift
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.model_targets import BinaryClassifierOutputTarget

from .chefer import chefer_gradient_rollout
from .viz import create_overlay, render_heatmap_raw, to_b64_png, normalize_heatmap, fast_resize, to_b64_jpeg


@dataclass
class AttributionResult:
    name: str
    description: str
    heatmap: np.ndarray          # 2D or 3D numpy array
    heatmap_norm: np.ndarray     # Normalized [0, 1] 2D heatmap (384, 384)
    overlay_b64: str             # Base64 PNG data URL
    raw_heatmap_b64: str         # Base64 PNG data URL
    metadata: dict
    compute_time_ms: float


def attention_rollout(wrapper, input_tensor) -> np.ndarray:
    """Attention Rollout for ViT across 12 transformer layers."""
    model = wrapper.model
    model.eval()

    with torch.no_grad():
        outputs = model(input_tensor, output_attentions=True)
        attentions = outputs.attentions

    device = attentions[0].device
    seq_len = attentions[0].size(-1)  # 577
    result = torch.eye(seq_len, device=device)

    for attn in attentions:
        attn_heads = attn.mean(dim=1)  # Average over 6 heads -> (B, 577, 577)
        attn_heads = attn_heads + torch.eye(attn_heads.size(-1), device=attn_heads.device)
        attn_heads = attn_heads / attn_heads.sum(dim=-1, keepdim=True)
        result = torch.matmul(attn_heads[0], result)

    patch_attention = result[0, 1:].reshape(24, 24).cpu().numpy()
    return patch_attention


def integrated_gradients(wrapper, input_tensor, steps=20) -> tuple[np.ndarray, float]:
    """Integrated Gradients using Captum with memory-safe mini-batching."""
    ig = IntegratedGradients(wrapper)
    baseline = torch.zeros_like(input_tensor)

    attributions, delta = ig.attribute(
        input_tensor,
        baselines=baseline,
        target=0,
        n_steps=steps,
        internal_batch_size=2,  # Prevents memory spike on CPU/RAM
        return_convergence_delta=True
    )
    heatmap = attributions[0].sum(dim=0).detach().cpu().numpy()
    delta_val = float(delta[0].item()) if hasattr(delta, "__getitem__") else float(delta.item())
    return heatmap, delta_val


def get_vit_encoder_layer(wrapper, layer_idx: int):
    """Safely traverse module hierarchy to find ViT encoder layer list regardless of HF transformers version."""
    obj = wrapper
    if hasattr(obj, "model"):
        obj = obj.model
    if hasattr(obj, "vit"):
        obj = obj.vit
    if hasattr(obj, "encoder"):
        obj = obj.encoder

    for attr in ["layer", "layers", "block", "blocks"]:
        if hasattr(obj, attr):
            return getattr(obj, attr)[layer_idx]
    raise AttributeError(f"Could not locate transformer layers list in object of type {type(obj)}")


def vit_grad_cam(wrapper, input_tensor, target_layer_idx=11) -> np.ndarray:
    """GradCAM for ViT using pytorch_grad_cam with custom reshape transform."""
    layer = get_vit_encoder_layer(wrapper, target_layer_idx)
    target_layer = [layer.output if hasattr(layer, "output") else layer]




    def reshape_transform(tensor):
        # tensor shape: (B, 577, 384) -> drop CLS token -> (B, 576, 384) -> (B, 24, 24, 384) -> (B, 384, 24, 24)
        result = tensor[:, 1:, :].reshape(tensor.size(0), 24, 24, tensor.size(2))
        result = result.permute(0, 3, 1, 2)
        return result

    cam = GradCAM(
        model=wrapper,
        target_layers=target_layer,
        reshape_transform=reshape_transform,
    )
    targets = [BinaryClassifierOutputTarget(0)]
    grayscale_cam = cam(input_tensor=input_tensor, targets=targets)
    return grayscale_cam[0]  # (24, 24) or (384, 384)


def occlusion_sensitivity(wrapper, input_tensor, patch_size=32, stride=16) -> np.ndarray:
    """Occlusion Sensitivity attribution using Captum with memory-safe mini-batching."""
    occ = Occlusion(wrapper)
    attributions = occ.attribute(
        input_tensor,
        target=0,
        strides=(3, stride, stride),
        sliding_window_shapes=(3, patch_size, patch_size),
        baselines=0.0,
        perturbations_per_eval=4,  # Limits memory allocation per evaluation batch
    )
    heatmap = attributions[0].sum(dim=0).detach().cpu().numpy()
    return heatmap


def _cleanup_captum_hooks(model):
    """Remove all forward/backward hooks Captum registers on model modules.

    Captum's DeepLift (and LayerGradCam, etc.) attach hooks via
    register_forward_hook / register_full_backward_hook. If attribute()
    throws mid-execution these hooks survive on the singleton model and
    corrupt every subsequent attribution call on the same process.
    """
    for module in model.modules():
        module._forward_hooks.clear()
        module._forward_pre_hooks.clear()
        module._backward_hooks.clear()


def deeplift(wrapper, input_tensor) -> np.ndarray:
    """DeepLIFT attribution using Captum with hook cleanup to prevent cross-method contamination."""
    try:
        dl = DeepLift(wrapper)
        baseline = torch.zeros_like(input_tensor)
        attributions = dl.attribute(input_tensor, baselines=baseline, target=0)
        heatmap = attributions[0].sum(dim=0).detach().cpu().numpy()
        return heatmap
    except Exception as e:
        print(f"[deeplift] Warning: Captum DeepLIFT failed ({e}), falling back to Input x Gradient.")
        input_tensor_copy = input_tensor.clone().detach().requires_grad_(True)
        logits = wrapper(input_tensor_copy)
        logits[0, 0].backward()
        heatmap = (input_tensor_copy * input_tensor_copy.grad).sum(dim=1)[0].detach().cpu().numpy()
        return heatmap
    finally:
        _cleanup_captum_hooks(wrapper)


def run_per_head_attention(wrapper, image: Image.Image, layer_idx: int = 11, cmap: str = "jet") -> dict:
    """Extract and render attention maps for all 6 heads at a specific layer."""
    input_tensor = wrapper.preprocess(image)
    device = next(wrapper.model.parameters()).device
    input_tensor = input_tensor.to(device)

    with torch.no_grad():
        outputs = wrapper.model(input_tensor, output_attentions=True)
        attns = outputs.attentions[layer_idx][0]  # (6, 577, 577)

    head_results = []
    for h in range(6):
        head_map = attns[h, 0, 1:].reshape(24, 24).cpu().numpy()
        overlay = create_overlay(image, head_map, alpha=0.6, cmap=cmap)
        head_results.append({
            "head": h + 1,
            "overlay_b64": to_b64_png(overlay),
            "raw_b64": to_b64_png(render_heatmap_raw(head_map, cmap=cmap)),
            "min": round(float(head_map.min()), 4),
            "max": round(float(head_map.max()), 4),
        })

    return {
        "layer": layer_idx + 1,
        "heads": head_results,
    }


import threading

_method_lock = threading.Lock()

def run_single_method(
    wrapper,
    image: Image.Image,
    method_name: str,
    alpha: float = 0.6,
    cmap: str = "jet",
    steps: int = 20,
    patch_size: int = 32,
    stride: int = 16,
    target_layer_idx: int = 11,
) -> AttributionResult:
    """Execute a single specified XAI method with thread safety on model state."""
    with _method_lock:
        input_tensor = wrapper.preprocess(image)
        device = next(wrapper.model.parameters()).device
        input_tensor = input_tensor.to(device)

        t0 = time.perf_counter()
        metadata = {}

        if method_name == "rollout":
            disp_name = "Attention Rollout"
            desc = "Global multi-layer attention flow across all 12 transformer blocks."
            heatmap = attention_rollout(wrapper, input_tensor)

        elif method_name == "integrated":
            disp_name = "Integrated Gradients"
            desc = "Path-integral gradient attribution relative to a black baseline."
            heatmap, delta = integrated_gradients(wrapper, input_tensor, steps=steps)
            metadata["delta"] = delta
            metadata["steps"] = steps

        elif method_name == "gradcam":
            disp_name = "GradCAM"
            desc = f"Gradient-weighted activation map targeting layer {target_layer_idx + 1}."
            heatmap = vit_grad_cam(wrapper, input_tensor, target_layer_idx=target_layer_idx)
            metadata["target_layer"] = target_layer_idx + 1

        elif method_name == "chefer":
            disp_name = "Gradient Attention Rollout (Chefer)"
            desc = "Gradient-weighted attention rollout combining gradients and self-attention (CVPR 2021)."
            heatmap = chefer_gradient_rollout(wrapper, input_tensor)

        elif method_name == "occlusion":
            disp_name = "Occlusion Sensitivity"
            desc = f"Sliding window perturbation ({patch_size}x{patch_size} patch, stride {stride})."
            heatmap = occlusion_sensitivity(wrapper, input_tensor, patch_size=patch_size, stride=stride)
            metadata["patch_size"] = patch_size
            metadata["stride"] = stride

        elif method_name == "deeplift":
            disp_name = "DeepLIFT"
            desc = "Non-linear feature attribution backpropagating differences from baseline."
            heatmap = deeplift(wrapper, input_tensor)

        else:
            raise ValueError(f"Unknown XAI method: {method_name}")

        elapsed_ms = (time.perf_counter() - t0) * 1000

        # Resize heatmap to 384x384 if needed
        if heatmap.shape != (384, 384):
            heatmap_resized = fast_resize(heatmap, (384, 384))
        else:
            heatmap_resized = heatmap

        norm_h = normalize_heatmap(heatmap_resized)
        overlay = create_overlay(image, heatmap, alpha=alpha, cmap=cmap)
        raw_render = render_heatmap_raw(heatmap, cmap=cmap)

        metadata.update({
            "min": round(float(heatmap.min()), 5),
            "max": round(float(heatmap.max()), 5),
            "mean": round(float(heatmap.mean()), 5),
            "std": round(float(heatmap.std()), 5),
        })

        return AttributionResult(
            name=disp_name,
            description=desc,
            heatmap=heatmap,
            heatmap_norm=norm_h,
            overlay_b64=to_b64_jpeg(overlay),
            raw_heatmap_b64=to_b64_jpeg(raw_render),
            metadata=metadata,
            compute_time_ms=round(elapsed_ms, 1),
        )


def run_all_methods(
    wrapper,
    image: Image.Image,
    alpha: float = 0.6,
    cmap: str = "jet",
) -> list[AttributionResult]:
    """Run all 6 primary spatial attribution methods sequentially with per-method exception handling."""
    methods = ["rollout", "chefer", "gradcam", "integrated", "deeplift", "occlusion"]
    results = []
    for m in methods:
        try:
            res = run_single_method(wrapper, image, m, alpha=alpha, cmap=cmap)
            results.append(res)
        except Exception as err:
            print(f"[xai_engine] Error running method {m}: {err}")
    return results