File size: 12,375 Bytes
112139c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
"""Causal global/high branch ablation for a RenderMe INS checkpoint."""

from __future__ import annotations

import argparse
import json
import sys
from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace
from typing import Iterator, Optional

import torch
import torch.nn.functional as F
from torch import Tensor
from torchvision.utils import make_grid, save_image

REPOSITORY_ROOT = Path(__file__).resolve().parents[1]
if str(REPOSITORY_ROOT) not in sys.path:
    sys.path.insert(0, str(REPOSITORY_ROOT))

from datasets.renderme_ins_dataset import RenderMeINSViewDataset
from scripts.evaluate_ins_stage1_renderme import (
    build_model_from_checkpoint,
    foreground_metrics,
    read_checkpoint,
)
from scripts.train_ins_stage1 import (
    DistributedContext,
    build_auxiliary_losses,
    resize_target,
)


CONDITIONS = ("baseline", "global_zero", "high_zero")


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Render baseline/global=0/high=0 from one INS checkpoint."
    )
    parser.add_argument("--checkpoint", type=Path, required=True)
    parser.add_argument("--data-root", type=Path, default=None)
    parser.add_argument("--frame-index", type=int, default=None)
    parser.add_argument("--camera-id", default=None)
    parser.add_argument("--source-camera-id", default=None)
    parser.add_argument(
        "--mode",
        choices=("auto", "same-view", "fixed-source-reuse"),
        default="auto",
    )
    parser.add_argument("--resolution", type=int, default=512)
    parser.add_argument("--amp", choices=("auto", "none", "bf16"), default="auto")
    parser.add_argument("--lpips", action=argparse.BooleanOptionalAction, default=True)
    parser.add_argument(
        "--lpips-net", choices=("alex", "squeeze", "vgg"), default="alex"
    )
    parser.add_argument("--device", default="cuda:0")
    parser.add_argument(
        "--output-dir",
        type=Path,
        default=Path("/workspace/cpfs-data/tmp/ins_branch_ablation"),
    )
    args = parser.parse_args()
    if not args.checkpoint.is_file():
        raise FileNotFoundError(args.checkpoint)
    if args.data_root is not None and not args.data_root.is_dir():
        raise FileNotFoundError(args.data_root)
    if args.frame_index is not None and args.frame_index < 0:
        raise ValueError("--frame-index cannot be negative.")
    if args.resolution <= 0:
        raise ValueError("--resolution must be positive.")
    return args


def saved_args(checkpoint: dict[str, object]) -> dict[str, object]:
    value = checkpoint.get("args", {})
    return value if isinstance(value, dict) else {}


def resolve_config(
    args: argparse.Namespace,
    checkpoint: dict[str, object],
) -> dict[str, object]:
    saved = saved_args(checkpoint)
    root = args.data_root or Path(
        saved.get(
            "data_root",
            "/workspace/cpfs-data/data/renderme_all/0026/0026_e0",
        )
    )
    frame_index = (
        int(saved.get("frame_index", 0))
        if args.frame_index is None
        else args.frame_index
    )
    mode = str(saved.get("mode", "same-view")) if args.mode == "auto" else args.mode
    if mode not in {"same-view", "fixed-source-reuse"}:
        raise ValueError(f"Unsupported checkpoint mode: {mode}")
    source_camera_id = args.source_camera_id
    if source_camera_id is None:
        source_camera_id = saved.get("source_camera_id")
    checkpoint_amp = str(saved.get("amp", "none"))
    amp = checkpoint_amp if args.amp == "auto" else args.amp
    if amp not in {"none", "bf16"}:
        raise ValueError(
            f"Ablation supports none/bf16, but checkpoint requested {amp!r}."
        )
    return {
        "root": Path(root),
        "frame_index": frame_index,
        "mode": mode,
        "source_camera_id": (
            None if source_camera_id is None else str(source_camera_id)
        ),
        "canonical_radius": float(saved.get("canonical_radius", 2.7)),
        "canonical_focus": tuple(
            float(value)
            for value in saved.get("canonical_focus", (0.0, 0.0, 0.2))
        ),
        "background": tuple(
            float(value) for value in saved.get("background", (1.0, 1.0, 1.0))
        ),
        "amp": amp,
    }


@contextmanager
def ablate_branch(model, condition: str) -> Iterator[None]:
    """Temporarily replace one branch output with an all-zero tensor."""
    if condition not in CONDITIONS:
        raise ValueError(f"Unknown branch condition: {condition}")
    if condition == "baseline":
        yield
        return
    module = (
        model.encoder.f_encoder
        if condition == "global_zero"
        else model.encoder.high_encoder
    )

    def zero_output(_module, _inputs, output):
        if not torch.is_tensor(output):
            raise TypeError("INS encoder branch output must be a tensor.")
        return torch.zeros_like(output)

    handle = module.register_forward_hook(zero_output)
    try:
        yield
    finally:
        handle.remove()


def masked_mean(value: Tensor, mask: Tensor) -> Tensor:
    if mask.shape[1] == 1 and value.shape[1] != 1:
        mask = mask.expand(-1, value.shape[1], -1, -1)
    return (value * mask).sum() / mask.sum().clamp_min(1.0)


@torch.inference_mode()
def evaluate(args: argparse.Namespace) -> dict[str, object]:
    device = torch.device(args.device)
    if device.type != "cuda" or not torch.cuda.is_available():
        raise RuntimeError("INS branch ablation requires CUDA.")
    torch.cuda.set_device(device)
    checkpoint = read_checkpoint(args.checkpoint)
    config = resolve_config(args, checkpoint)
    model = build_model_from_checkpoint(checkpoint, args.resolution, device)

    dataset = RenderMeINSViewDataset(
        root=config["root"],
        frame_index=config["frame_index"],
        image_size=512,
        camera_ids=None if args.camera_id is None else [args.camera_id],
        source_camera_id=config["source_camera_id"],
        canonical_radius=config["canonical_radius"],
        canonical_focus=config["canonical_focus"],
        background=config["background"],
    )
    target_camera_id = str(args.camera_id or dataset.source_camera_id)
    target_sample = dataset.get_view(target_camera_id)
    source_camera_id = str(dataset.source_camera_id)
    source_sample = dataset.get_view(source_camera_id)
    if config["mode"] == "same-view":
        encoder_sample = target_sample
    else:
        encoder_sample = source_sample

    encoder_image = encoder_sample["image"].unsqueeze(0).to(device)
    encoder_camera = encoder_sample["camera"].unsqueeze(0).to(device)
    render_camera = target_sample["camera"].unsqueeze(0).to(device)
    target_source = target_sample["image"].unsqueeze(0).to(device)
    target = resize_target(target_source, args.resolution)
    foreground_mask = target_sample["foreground_mask"].unsqueeze(0).to(device)
    background = torch.tensor(config["background"], device=device)

    context = DistributedContext(
        rank=0,
        local_rank=device.index or 0,
        world_size=1,
        distributed=False,
        device=device,
    )
    metric_args = SimpleNamespace(
        lpips_weight=1.0 if args.lpips else 0.0,
        id_weight=0.0,
        lpips_net=args.lpips_net,
    )
    lpips_metric, _ = build_auxiliary_losses(metric_args, context)

    predictions: dict[str, Tensor] = {}
    masks: dict[str, Tensor] = {}
    metrics: dict[str, dict[str, float]] = {}
    for condition in CONDITIONS:
        with ablate_branch(model, condition):
            with torch.autocast(
                device_type="cuda",
                dtype=torch.bfloat16,
                enabled=config["amp"] == "bf16",
            ):
                output = model(
                    encoder_image,
                    render_camera,
                    sampling_camera=encoder_camera,
                    encoder_camera=encoder_camera,
                    render_resolution=args.resolution,
                    deterministic_sampling=True,
                )
        raw_prediction = output["image"].float().clamp(0.0, 1.0)
        condition_metrics, composited, resized_mask = foreground_metrics(
            raw_prediction,
            target,
            foreground_mask,
            background,
            lpips_metric,
            identity_metric=None,
        )
        gaussians = output["gaussians"]
        condition_metrics.update(
            {
                "opacity_mean": float(gaussians["opacities"].float().mean()),
                "scale_mean": float(gaussians["scales"].float().mean()),
                "scale_max": float(gaussians["scales"].float().max()),
            }
        )
        predictions[condition] = composited
        masks[condition] = resized_mask
        metrics[condition] = condition_metrics

    baseline = predictions["baseline"]
    mask = masks["baseline"]
    for condition in ("global_zero", "high_zero"):
        metrics[condition]["foreground_render_change_mae"] = float(
            masked_mean((predictions[condition] - baseline).abs(), mask)
        )
        metrics[condition]["delta_foreground_psnr"] = (
            metrics[condition]["foreground_psnr"]
            - metrics["baseline"]["foreground_psnr"]
        )
        if "lpips" in metrics[condition]:
            metrics[condition]["delta_lpips"] = (
                metrics[condition]["lpips"] - metrics["baseline"]["lpips"]
            )

    args.output_dir.mkdir(parents=True, exist_ok=True)
    encoder_01 = resize_target(encoder_image, args.resolution).cpu()
    target_cpu = target.cpu()
    mask_rgb = mask.expand(-1, 3, -1, -1).cpu()
    prediction_row = [predictions[name].cpu() for name in CONDITIONS]
    error_row = [(prediction - target_cpu).abs() * mask_rgb for prediction in prediction_row]
    overview = make_grid(
        torch.cat(
            [
                encoder_01,
                target_cpu,
                prediction_row[0],
                prediction_row[1],
                mask_rgb,
                error_row[0],
                error_row[1],
                error_row[2],
                prediction_row[2],
            ],
            dim=0,
        ),
        nrow=4,
        padding=3,
    )
    # The final high-zero prediction occupies a third row because the first two
    # rows are kept as four aligned columns. Individual renders below are the
    # unambiguous source for numerical/visual comparison.
    overview_path = args.output_dir / "ablation_overview.png"
    save_image(overview, overview_path)
    for name, prediction in predictions.items():
        save_image(prediction.cpu(), args.output_dir / f"{name}.png")
        save_image(
            (prediction.cpu() - target_cpu).abs() * mask_rgb,
            args.output_dir / f"{name}_error.png",
        )

    result: dict[str, object] = {
        "checkpoint": str(args.checkpoint.resolve()),
        "checkpoint_step": int(checkpoint.get("global_step", 0)),
        "protocol": config["mode"],
        "amp": config["amp"],
        "encoder_camera_id": str(encoder_sample["camera_id"]),
        "target_camera_id": target_camera_id,
        "resolution": args.resolution,
        "metrics": metrics,
        "interpretation": {
            "foreground_render_change_mae": (
                "Mean absolute foreground render change relative to baseline."
            ),
            "delta_foreground_psnr": (
                "A large negative value means the ablated branch was important."
            ),
            "delta_lpips": (
                "A large positive value means the ablated branch was important."
            ),
        },
        "outputs": {
            "overview": str(overview_path),
            **{
                name: str(args.output_dir / f"{name}.png")
                for name in CONDITIONS
            },
        },
    }
    metrics_path = args.output_dir / "metrics.json"
    with metrics_path.open("w", encoding="utf-8") as file:
        json.dump(result, file, indent=2)
        file.write("\n")
    print(json.dumps(result, indent=2), flush=True)
    print(f"overview: {overview_path}", flush=True)
    print(f"metrics: {metrics_path}", flush=True)
    return result


def main() -> int:
    evaluate(parse_args())
    return 0


if __name__ == "__main__":
    raise SystemExit(main())