-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb_ui.py
More file actions
328 lines (256 loc) · 10.5 KB
/
web_ui.py
File metadata and controls
328 lines (256 loc) · 10.5 KB
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
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
"""Web UI for Real vs Fake face detection with Grad-CAM visualization.
This script loads three trained models (EfficientNet-B3, FasterViT-2-224,
EfficientFormerV2-S1), performs inference on an input image, produces
Grad-CAM overlays for each model, and displays the results side-by-side
via a Gradio interface. It also exports a high-resolution composite image
to disk.
"""
from __future__ import annotations
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
import gradio as gr
import numpy as np
import torch
import torch.nn.functional as f
from PIL import Image, ImageDraw, ImageFont
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.image import show_cam_on_image
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from torch import nn
from torchvision import transforms
from orchestration.orchestrator import (
build_eval_transforms,
load_config,
load_model,
resolve_transform_mapping,
)
# ---------------------------------------------------------------------
# Device, config, and export settings
# ---------------------------------------------------------------------
DEFAULT_CONFIG_PATH = Path("config/inference.yaml")
EXPORT_SCALE = 2
EXPORT_DIR = Path("outputs") / "cam_exports"
EXPORT_DIR.mkdir(parents=True, exist_ok=True)
@dataclass
class ModelBundle:
"""Container for model-specific inference resources."""
name: str
display_label: str
model: nn.Module
transform: transforms.Compose
normalize: bool
device: torch.device
target_layer: nn.Module
CLASS_LABELS: dict[int, str] = {0: "fake", 1: "real"}
MODEL_CACHE: list[ModelBundle] = []
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
CONFIG_METADATA: dict[str, Any] = {}
def _resolve_weights_path(path_value: str | None) -> Path | None:
"""Resolve model weights path like orchestrator: relative to CWD."""
if not path_value:
return None
p = Path(path_value).expanduser()
if not p.is_absolute():
p = (Path.cwd() / p).resolve()
return p
def _tensor_to_rgb(tensor: torch.Tensor, *, normalize: bool) -> np.ndarray:
"""Convert a (C,H,W) tensor to an RGB float image in [0, 1]."""
if tensor.ndim == 4:
if tensor.size(0) != 1:
msg = "Expected batch of size 1 for visualization."
raise ValueError(msg)
tensor = tensor[0]
if tensor.ndim != 3:
msg = "Expected a 3D tensor for visualization."
raise ValueError(msg)
arr = tensor.detach().clone()
if normalize:
mean = torch.tensor([0.485, 0.456, 0.406], dtype=arr.dtype, device=arr.device)
std = torch.tensor([0.229, 0.224, 0.225], dtype=arr.dtype, device=arr.device)
arr = arr * std.view(-1, 1, 1) + mean.view(-1, 1, 1)
arr = arr.clamp(0.0, 1.0)
rgb = arr.permute(1, 2, 0).cpu().numpy().astype(np.float32)
return rgb
def _find_last_conv_layer(module: nn.Module) -> nn.Module:
"""Pick the last Conv2d for Grad-CAM target."""
last: nn.Module | None = None
for m in module.modules():
if isinstance(m, nn.Conv2d):
last = m
if last is None:
msg = "No Conv2d layer found for Grad-CAM target."
raise RuntimeError(msg)
return last
def _resolve_cam_target(module: nn.Module) -> nn.Module:
"""Return a target layer for Grad-CAM, preferring explicit conv heads."""
conv_head = getattr(module, "_conv_head", None)
if isinstance(conv_head, nn.Module):
return conv_head
return _find_last_conv_layer(module)
def _add_label(img_rgb_uint8: np.ndarray, text: str) -> np.ndarray:
"""Overlay a readable text label onto the top-left of an image."""
img = Image.fromarray(img_rgb_uint8)
draw = ImageDraw.Draw(img)
font = ImageFont.load_default()
draw.text(
(6, 6),
text,
fill=(255, 255, 255),
stroke_width=2,
stroke_fill=(0, 0, 0),
font=font,
)
return np.asarray(img)
def _coerce_device(device_str: str | None) -> torch.device:
"""Return a :class:`torch.device` for the requested string."""
if device_str:
requested = torch.device(device_str)
if requested.type == "cuda" and not torch.cuda.is_available():
print(
"[UI] CUDA requested in config but unavailable. Falling back to CPU.",
)
return torch.device("cpu")
return requested
return torch.device("cuda" if torch.cuda.is_available() else "cpu")
def _detect_normalization(transform: transforms.Compose) -> bool:
"""Check whether a composed transform includes normalization."""
for op in getattr(transform, "transforms", []):
if isinstance(op, transforms.Normalize):
return True
return False
def initialize_from_config(config_path: Path) -> None:
"""Load orchestrator config and populate inference resources."""
global CLASS_LABELS, MODEL_CACHE, DEVICE, CONFIG_METADATA
config = load_config(config_path)
CONFIG_METADATA = {
"config_path": config_path,
"raw": config,
}
device_str = config.get("device")
DEVICE = _coerce_device(device_str)
data_cfg: dict[str, Any] = config.get("data", {})
num_classes = int(data_cfg.get("num_classes", 2))
image_size = int(data_cfg.get("img_size", 224))
labels_cfg = data_cfg.get("class_labels")
if isinstance(labels_cfg, dict):
CLASS_LABELS = {int(k): str(v) for k, v in labels_cfg.items()}
models_cfg: dict[str, dict[str, Any]] = config.get("models", {})
selection: list[str] = config.get("selection") or list(models_cfg.keys())
bundles: list[ModelBundle] = []
for model_name in selection:
model_cfg = models_cfg.get(model_name)
if not isinstance(model_cfg, dict):
print(f"[UI] Skipping unknown model '{model_name}' in selection.")
continue
toggles = resolve_transform_mapping(model_cfg, phase="eval")
transform = build_eval_transforms(image_size, toggles=toggles)
normalize = _detect_normalization(transform)
inference_cfg = model_cfg.get("inference", {})
weights_path = _resolve_weights_path(inference_cfg.get("weights"))
model = load_model(model_name, num_classes, weights_path, DEVICE)
target_layer = _resolve_cam_target(model)
display_label = (
str(model_cfg.get("display_name")
or model_cfg.get("label")
or model_name)
)
bundles.append(
ModelBundle(
name=model_name,
display_label=display_label,
model=model,
transform=transform,
normalize=normalize,
device=DEVICE,
target_layer=target_layer,
),
)
if not bundles:
msg = "No valid models configured for inference."
raise RuntimeError(msg)
MODEL_CACHE = bundles
def build_interface(config_path: Path = DEFAULT_CONFIG_PATH) -> gr.Interface:
"""Create a Gradio interface configured via orchestrator settings."""
initialize_from_config(config_path)
return gr.Interface(
fn=predict_and_visualize,
inputs=gr.Image(type="pil"),
outputs=[gr.Image(type="numpy"), "text"],
title="Real vs Fake Face Detection",
description="Upload an image to determine if the face is real or fake.",
)
# ---------------------------------------------------------------------
# Inference + Grad-CAM (show CAM from each model side by side)
# ---------------------------------------------------------------------
def predict_and_visualize(image: Image.Image) -> tuple[np.ndarray, str]:
"""Run inference with three models and visualize Grad-CAM panels side-by-side.
Returns a high-resolution concatenated image (as a NumPy array) and a
summary string with predicted labels and confidences for each model.
"""
panels: list[np.ndarray] = []
summary_lines: list[str] = []
for bundle in MODEL_CACHE:
tensor = bundle.transform(image)
if not isinstance(tensor, torch.Tensor):
msg = f"Transform for {bundle.name} must return a tensor."
raise TypeError(msg)
if tensor.ndim == 3:
batch = tensor.unsqueeze(0)
elif tensor.ndim == 4:
batch = tensor
else:
msg = f"Unexpected tensor rank {tensor.ndim} for model {bundle.name}."
raise ValueError(msg)
batch = batch.to(bundle.device)
with torch.inference_mode():
logits = bundle.model(batch)
probs = f.softmax(logits, dim=1)
cls_idx = int(probs.argmax(1))
confidence = float(probs[0, cls_idx] * 100.0)
label = CLASS_LABELS.get(cls_idx, f"class_{cls_idx}")
summary_lines.append(f"{bundle.display_label}: {label} ({confidence:.2f}% confidence)")
with GradCAM(model=bundle.model, target_layers=[bundle.target_layer]) as cam:
grayscale = cam(
input_tensor=batch,
targets=[ClassifierOutputTarget(cls_idx)],
)[0]
rgb = _tensor_to_rgb(tensor, normalize=bundle.normalize)
overlay = show_cam_on_image(rgb, grayscale, use_rgb=True)
panel = _add_label(overlay, f"{bundle.display_label} {label} ({confidence:.1f}%)")
panels.append(panel)
if not panels:
msg = "No models available for inference."
raise RuntimeError(msg)
side_by_side = np.concatenate(panels, axis=1)
# Export high-res image
h, w, _ = side_by_side.shape
export_img = Image.fromarray(side_by_side).resize(
(w * EXPORT_SCALE, h * EXPORT_SCALE),
resample=Image.BICUBIC,
)
out_path = (
EXPORT_DIR
/ f"cam_triptych_{datetime.now(tz=UTC).strftime('%Y%m%d_%H%M%S')}.png"
)
export_img.save(out_path, format="PNG", optimize=True)
# Return upscaled image to Gradio as well
summary = "\n".join(summary_lines + [f"Saved: {out_path.resolve()}"])
return np.asarray(export_img), summary
# ---------------------------------------------------------------------
# Gradio UI
# ---------------------------------------------------------------------
iface = build_interface()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Deepfake detection UI")
parser.add_argument(
"--config",
type=Path,
default=DEFAULT_CONFIG_PATH,
help="Path to an orchestrator inference YAML config.",
)
args = parser.parse_args()
iface = build_interface(args.config)
iface.launch()