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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
from torchvision.transforms.functional import to_pil_image
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import cv2
from timm import create_model
from huggingface_hub import hf_hub_download
import io
import warnings

warnings.filterwarnings("ignore", category=UserWarning)

# Advanced Grad-CAM Implementation
class AdvancedGradCAM:
    def __init__(self, model, target_layer, method="gradcam"):
        self.model = model
        self.target_layer = target_layer
        self.method = method
        self.gradients = None
        self.activations = None
        self.forward_hook_handle = None
        self.backward_hook_handle = None
        self._register_hooks()

    def _register_hooks(self):
        layer = dict([*self.model.named_modules()])[self.target_layer]

        def forward_hook(module, input, output):
            if isinstance(output, tuple):
                for item in output:
                    if isinstance(item, torch.Tensor):
                        self.activations = item.detach()
                        break
            else:
                self.activations = output.detach()

        def backward_hook(module, grad_in, grad_out):
            self.gradients = grad_out[0].detach()

        self.forward_hook_handle = layer.register_forward_hook(forward_hook)
        self.backward_hook_handle = layer.register_backward_hook(backward_hook)

    def remove_hooks(self):
        if self.forward_hook_handle:
            self.forward_hook_handle.remove()
        if self.backward_hook_handle:
            self.backward_hook_handle.remove()
        self.forward_hook_handle = None
        self.backward_hook_handle = None
        self.gradients = None
        self.activations = None

    def generate(self, input_tensor, class_idx, num_samples=5, stdev_spread=0.15):
        if self.forward_hook_handle is None or self.backward_hook_handle is None:
            self._register_hooks()

        self.model.zero_grad()

        try:
            input_tensor.requires_grad_(True)
            output = self.model(input_tensor)
            class_score = output[:, class_idx]
            class_score.backward()

            if self.gradients is None or self.activations is None:
                print(f"Warning: Gradients or activations are None for layer {self.target_layer}. Using fallback CAM.")
                h, w = input_tensor.shape[-2:]
                fallback_h, fallback_w = h // 16, w // 16
                return np.ones((fallback_h, fallback_w), dtype=np.float32) * 0.5

            if self.method == "gradcam":
                cam_result = self._standard_gradcam()
            else:
                raise ValueError(f"Unsupported CAM method: {self.method}")

            self.gradients = None
            self.activations = None
            input_tensor.requires_grad_(False)
            self.model.zero_grad()

            return cam_result

        except Exception as e:
            print(f"Error in AdvancedGradCAM.generate: {str(e)}")
            import traceback
            traceback.print_exc()
            h, w = input_tensor.shape[-2:]
            fallback_h, fallback_w = h // 16, w // 16
            return np.ones((fallback_h, fallback_w), dtype=np.float32) * 0.5

    def _standard_gradcam(self):
        gradients = self.gradients.cpu().numpy()
        activations = self.activations.cpu().numpy()

        if len(gradients.shape) != 4 or len(activations.shape) != 4:
             print(f"Warning: Unexpected shape in GradCAM++. Gradients: {gradients.shape}, Activations: {activations.shape}. Using fallback.")
             fallback_h, fallback_w = activations.shape[-2:] if len(activations.shape) >= 2 else (14, 14)
             return np.ones((fallback_h, fallback_w), dtype=np.float32) * 0.5

        grad_2 = gradients ** 2
        grad_3 = gradients ** 3
        epsilon = 1e-10
        alpha_denom = 2 * grad_2 + np.sum(activations * grad_3, axis=(2, 3), keepdims=True)
        alpha = grad_2 / (alpha_denom + epsilon)
        positive_activations_gradients = np.maximum(gradients, 0)
        weights = np.sum(alpha * positive_activations_gradients, axis=(2, 3))

        cam = np.zeros(activations.shape[2:], dtype=np.float32)
        for i, w in enumerate(weights[0]):
            cam += w * activations[0, i, :, :]

        cam = np.maximum(cam, 0)
        if np.max(cam) > 0:
            cam = cam / np.max(cam)
        return cam 

# Utility Functions
def overlay_cam_on_image(image, cam, face_box=None, alpha=0.5):
    img_np = np.array(image.convert('RGB'))
    h, w = img_np.shape[:2]

    if face_box is not None:
        x, y, fw, fh = map(int, face_box)
        if fw <= 0 or fh <= 0:
            print(f"Warning: Invalid face box dimensions {fw}x{fh}. Applying CAM to full image.")
            face_box = None
        else:
            try:
                face_cam_resized = cv2.resize(cam, (fw, fh))
            except cv2.error as e:
                print(f"Error resizing CAM to face box {fw}x{fh}: {e}. Applying CAM to full image.")
                face_box = None

    if face_box is not None:
        x, y, fw, fh = map(int, face_box)
        full_cam_heatmap = np.zeros((h, w), dtype=np.float32)
        y_end = min(y + fh, h)
        x_end = min(x + fw, w)
        fh_clipped = y_end - y
        fw_clipped = x_end - x
        if fh_clipped > 0 and fw_clipped > 0:
            full_cam_heatmap[y:y_end, x:x_end] = face_cam_resized[:fh_clipped, :fw_clipped]
        else:
            print("Warning: Face box calculation resulted in zero area for heatmap placement.")
        heatmap_colored = cv2.applyColorMap(np.uint8(255 * full_cam_heatmap), cv2.COLORMAP_JET)
        heatmap_colored = cv2.cvtColor(heatmap_colored, cv2.COLOR_BGR2RGB)
    else:
        try:
            cam_resized = cv2.resize(cam, (w, h))
        except cv2.error as e:
            print(f"Error resizing CAM to full image size {w}x{h}: {e}. Skipping overlay.")
            return image
        heatmap_colored = cv2.applyColorMap(np.uint8(255 * cam_resized), cv2.COLORMAP_JET)
        heatmap_colored = cv2.cvtColor(heatmap_colored, cv2.COLOR_BGR2RGB)

    overlayed_img = cv2.addWeighted(img_np, 1 - alpha, heatmap_colored, alpha, 0)
    return Image.fromarray(overlayed_img)

def save_comparison(image, cam, overlay, face_box=None):
    fig, axes = plt.subplots(1, 3, figsize=(15, 5))

    axes[0].imshow(image)
    axes[0].set_title("Original")
    if face_box is not None:
        x, y, w, h = map(int, face_box)
        if w > 0 and h > 0:
            rect = plt.Rectangle((x, y), w, h, edgecolor='lime', linewidth=2, fill=False)
            axes[0].add_patch(rect)
    axes[0].axis("off")

    if face_box is not None:
        x, y, w, h = map(int, face_box)
        if w > 0 and h > 0:
            try:
                cam_display = cv2.resize(cam, (w, h))
                img_h, img_w = np.array(image).shape[:2]
                full_cam_display = np.zeros((img_h, img_w))
                y_end = min(y + h, img_h)
                x_end = min(x + w, img_w)
                h_clipped = y_end - y
                w_clipped = x_end - x
                if h_clipped > 0 and w_clipped > 0:
                    full_cam_display[y:y_end, x:x_end] = cam_display[:h_clipped, :w_clipped]
                axes[1].imshow(full_cam_display, cmap="jet")
            except cv2.error:
                axes[1].imshow(cam, cmap="jet")
        else:
            axes[1].imshow(cam, cmap="jet")
    else:
        axes[1].imshow(cam, cmap="jet")

    axes[1].set_title("CAM")
    axes[1].axis("off")

    axes[2].imshow(overlay)
    axes[2].set_title("Overlay")
    axes[2].axis("off")

    plt.tight_layout()
    
    buf = io.BytesIO()
    plt.savefig(buf, format="png", bbox_inches="tight")
    plt.close()
    buf.seek(0)
    return Image.open(buf)

#load xception model
def load_xception_model(model_repo="drg31/xception", model_filename="final_xception_model.pth", num_classes=2):
    try:
        model_path = hf_hub_download(repo_id=model_repo, filename=model_filename)
        print(f"Model downloaded to: {model_path}")
    except Exception as e:
        print(f"Error downloading model from Hugging Face Hub ({model_repo}/{model_filename}): {e}")
        raise

    model = create_model("xception", pretrained=False, num_classes=num_classes)
    print(f"Created Xception model with {num_classes} output classes.")

    try:
        checkpoint = torch.load(model_path, map_location=torch.device('cpu'), weights_only=False)
        print(f"Checkpoint loaded successfully from {model_path} (with weights_only=False).")
    except Exception as e:
        print(f"Error loading checkpoint from {model_path}: {e}")
        raise

    if isinstance(checkpoint, dict) and 'state_dict' in checkpoint:
        checkpoint_state_dict = checkpoint['state_dict']
        print("Extracted state_dict from checkpoint dictionary.")
    else:
        checkpoint_state_dict = checkpoint
        print("Using checkpoint directly as state_dict.")

    cleaned_state_dict = {}
    for k, v in checkpoint_state_dict.items():
        name = k.replace('module.', '')
        cleaned_state_dict[name] = v
    print(f"Cleaned state_dict contains {len(cleaned_state_dict)} keys (after removing 'module.' prefix).")

    print("Loading state_dict with strict=False...")
    report = model.load_state_dict(cleaned_state_dict, strict=False)
    print(f"Load report - Missing keys: {report.missing_keys}")
    print(f"Load report - Unexpected keys: {report.unexpected_keys}")

    print("Model state loaded.")
    model.eval()
    return model

def get_target_layer_xception(model):
    target_layer_name = "block12.rep.6"  # Deeper layer for semantic features
    if target_layer_name not in dict([*model.named_modules()]):
        print(f"Warning: Target layer '{target_layer_name}' not found. Trying 'block11.rep.2'.")
        target_layer_name = "block11.rep.2"
        if target_layer_name not in dict([*model.named_modules()]):
            print(f"Warning: Fallback layer '{target_layer_name}' not found. Trying 'act4'.")
            target_layer_name = "act4"
            if target_layer_name not in dict([*model.named_modules()]):
                raise ValueError("Could not find suitable target layer for GradCAM in Xception model.")
    print(f"Using target layer: {target_layer_name}")
    return target_layer_name

# Main Visualization Function
def generate_smoothgrad_visualizations_xception(model, image, target_class=None, face_only=True, num_samples=5, stdev_spread=0.15):
    print("\n--- Starting Prediction and Grad-CAM ---")
    try:
        predicted_class_idx, confidence = predict_image(model, image, face_only)
    except Exception as pred_e:
        print(f"Error during prediction: {pred_e}")
        import traceback
        traceback.print_exc()
        return None, None, None, None

    if target_class is None:
        cam_target_class = predicted_class_idx
        print(f"CAM Target Class: Using predicted class index {cam_target_class} ({'real' if cam_target_class == 0 else 'fake'})")
    elif target_class in [0, 1]:
        cam_target_class = target_class
        print(f"CAM Target Class: Using specified class index {cam_target_class} ({'real' if cam_target_class == 0 else 'fake'})")
    else:
        print(f"Warning: Invalid target_class specified ({target_class}). Defaulting to predicted class index {predicted_class_idx}.")
        cam_target_class = predicted_class_idx

    device = next(model.parameters()).device
    model.eval()
    
    IMAGE_SIZE = 299
    transform = transforms.Compose([
        transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
    ])
    
    dataset = ImageDataset(image, transform=transform, face_only=face_only)
    input_tensor, original_image, face_box = dataset[0]
    input_tensor = input_tensor.unsqueeze(0).to(device)
    print(f"Input tensor for CAM shape: {input_tensor.shape}, Face box: {face_box}")

    raw_cam = None
    try:
        target_layer = get_target_layer_xception(model)
        print(f"Using target layer for CAM: {target_layer}")
        cam_extractor = AdvancedGradCAM(model, target_layer, method="gradcam")
        raw_cam = cam_extractor.generate(input_tensor, cam_target_class, num_samples=num_samples, stdev_spread=stdev_spread)
    except Exception as cam_e:
        print(f"Error during CAM generation: {cam_e}")
        import traceback
        traceback.print_exc()

    cam_heatmap_img, overlay_img, comparison_img = None, None, None
    if raw_cam is None or not isinstance(raw_cam, np.ndarray) or raw_cam.size == 0:
        print("CAM generation failed or produced invalid result. Skipping visualization.")
    else:
        try:
            print("Generating visualizations...")
            img_h, img_w = np.array(original_image).shape[:2]
            heatmap_display_np = np.zeros((img_h, img_w), dtype=np.float32)
            if face_box:
                x, y, w_fb, h_fb = map(int, face_box)
                if w_fb > 0 and h_fb > 0:
                    cam_resized_face = cv2.resize(raw_cam, (w_fb, h_fb), interpolation=cv2.INTER_LINEAR)
                    y_end, x_end = min(y + h_fb, img_h), min(x + w_fb, img_w)
                    h_clip, w_clip = y_end - y, x_end - x
                    if h_clip > 0 and w_clip > 0:
                        heatmap_display_np[y:y_end, x:x_end] = cam_resized_face[:h_clip, :w_clip]
                else:
                    heatmap_display_np = cv2.resize(raw_cam, (img_w, img_h), interpolation=cv2.INTER_LINEAR)
            else:
                heatmap_display_np = cv2.resize(raw_cam, (img_w, img_h), interpolation=cv2.INTER_LINEAR)

            min_h, max_h = np.min(heatmap_display_np), np.max(heatmap_display_np)
            if max_h > min_h:
                heatmap_norm = (heatmap_display_np - min_h) / (max_h - min_h)
            else:
                heatmap_norm = np.zeros_like(heatmap_display_np)
            heatmap_rgb = (plt.cm.jet(heatmap_norm)[:, :, :3] * 255).astype(np.uint8)
            cam_heatmap_img = Image.fromarray(heatmap_rgb)
            print("  Heatmap generated.")

            overlay_img = overlay_cam_on_image(original_image, raw_cam, face_box)
            print("  Overlay generated.")

            if overlay_img:
                comparison_img = save_comparison(original_image, raw_cam, overlay_img, face_box)
                print("  Comparison generated.")
            else:
                print("  Skipping comparison image because overlay failed.")

        except Exception as vis_e:
            print(f"Error during visualization generation: {vis_e}")
            import traceback
            traceback.print_exc()

    print("--- Prediction and Grad-CAM Finished ---")
    return raw_cam, cam_heatmap_img, overlay_img, comparison_img

# Face Detection Dataset
class ImageDataset(torch.utils.data.Dataset):
    def __init__(self, image, transform=None, face_only=True):
        self.image = image
        self.transform = transform
        self.face_only = face_only
        try:
            self.face_detector = cv2.CascadeClassifier(
                cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
            )
            if self.face_detector.empty():
                raise IOError("Failed to load cascade file")
        except Exception as e:
            print(f"Error loading Haar Cascade: {e}. Face detection might fail.")
            class DummyDetector:
                def detectMultiScale(self, *args, **kwargs): return []
            self.face_detector = DummyDetector()

    def __len__(self):
        return 1

    def detect_face(self, image_np):
        gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
        faces = self.face_detector.detectMultiScale(gray, 1.1, 5)
        if len(faces) == 0:
            print("No face detected, using full image as fallback.")
            h, w = image_np.shape[:2]
            return (0, 0, w, h), image_np
        areas = [w * h for (x, y, w, h) in faces]
        idx = np.argmax(areas)  # Select largest face
        x, y, w, h = faces[idx]
        pad_x, pad_y = int(w * 0.05), int(h * 0.05)
        x1, y1 = max(0, x - pad_x), max(0, y - pad_y)
        x2, y2 = min(image_np.shape[1], x + w + pad_x), min(image_np.shape[0], y + h + pad_y)
        face_img = image_np[y1:y2, x1:x2]
        return (x1, y1, x2 - x1, y2 - y1), face_img

    def __getitem__(self, idx):
        image_np = np.array(self.image)
        original_image = self.image.copy()
        face_box_final = None
        processed_image = original_image

        if self.face_only:
            try:
                face_box, face_img_np = self.detect_face(image_np)
                if face_img_np.size == 0 or face_box[2] <= 0 or face_box[3] <= 0:
                    print("Warning: Face detection returned empty or invalid region. Using full image.")
                    face_box_final = None
                    processed_image = original_image
                else:
                    processed_image = Image.fromarray(face_img_np)
                    face_box_final = face_box
            except Exception as e:
                print(f"Error during face detection: {e}. Using full image.")
                face_box_final = None
                processed_image = original_image
        else:
            face_box_final = None
            processed_image = original_image

        if self.transform:
            tensor = self.transform(processed_image)
        else:
            tensor = transforms.ToTensor()(processed_image)

        return tensor, original_image, face_box_final

def predict_image(model, image, face_only=True):
    device = next(model.parameters()).device
    model.eval()

    IMAGE_SIZE = 299
    transform = transforms.Compose([
        transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
    ])

    dataset = ImageDataset(image, transform=transform, face_only=face_only)
    input_tensor, _, _ = dataset[0]
    input_tensor = input_tensor.unsqueeze(0).to(device)

    with torch.no_grad():
        logits = model(input_tensor)
        probabilities = F.softmax(logits, dim=1)

    pred_prob = probabilities[0].max().item()
    pred_class_idx = probabilities[0].argmax().item()
    pred_label = "real" if pred_class_idx == 0 else "fake"

    if pred_prob < 0.7:  # Example threshold
        print(f"Warning: Low confidence ({pred_prob:.4f}) detected. Model may need fine-tuning.")

    print(f"--- Prediction ---")
    print(f"Input Tensor Shape: {input_tensor.shape}")
    print(f"Logits: {logits.cpu().numpy()}")
    print(f"Probabilities: {probabilities.cpu().numpy()}")
    print(f"Predicted Class: {pred_label} (Index: {pred_class_idx})")
    print(f"Confidence: {pred_prob:.4f}")
    print(f"--------------------")

    return pred_class_idx, pred_prob