File size: 2,721 Bytes
9c890d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Compare camera conditioning ON/OFF with the same frozen SCALE4D checkpoint."""

from __future__ import annotations

import argparse
import json
from pathlib import Path


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--input", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--checkpoint", default="facebook/SCALE4D")
    parser.add_argument("--revision")
    parser.add_argument("--device", default="cuda:0")
    parser.add_argument("--views", type=int, choices=range(1, 11), default=3)
    parser.add_argument("--labels", type=Path)
    parser.add_argument("--no-flash-attn", action="store_true")
    args = parser.parse_args()
    if args.output.exists():
        parser.error("The output directory already exists")

    import numpy as np

    from evaluation.scale4d import SCALE4D
    from evaluation.scale4d_inputs import npz_clip
    from evaluation.ucpe_inputs import ClipLabels
    from evaluation.ucpe_scores import score_tracks

    clip = npz_clip(args.input)
    if clip.rgbs.shape[0] < args.views:
        parser.error("The input does not contain enough calibrated camera videos")
    labels = None
    if args.labels:
        with np.load(args.labels, allow_pickle=False) as data:
            labels = ClipLabels(
                data["gt_tracks_target_camera"],
                data["visibility"],
                data.get("visibility_known", None),
            )
    model = SCALE4D(
        args.checkpoint,
        revision=args.revision,
        device=args.device,
        use_flash_attn=not args.no_flash_attn,
        max_input_views=max(3, args.views),
    )
    args.output.mkdir(parents=True)
    scores = {}
    evaluation_mask = (
        np.arange(clip.rgbs.shape[1])[:, None] > clip.query_times.numpy()[None]
        if clip.metadata.get("query_policy")
        in ("first_visible_left", "first_visible_anchor")
        else None
    )
    for camera in ("on", "off"):
        prediction = model.predict_clip(clip, views=args.views, camera=camera)
        path = prediction.save(args.output / f"camera_{camera}.npz")
        if labels is not None:
            scores[camera] = score_tracks(
                labels,
                prediction.uv_depth.numpy(),
                prediction.visibility.numpy(),
                evaluation_mask=evaluation_mask,
            )["metric"]
        print(f"Saved camera {camera.upper()}: {path}", flush=True)
    if scores:
        scores["aj_gain_points"] = scores["on"]["aj"] - scores["off"]["aj"]
        (args.output / "scores.json").write_text(json.dumps(scores, indent=2) + "\n")


if __name__ == "__main__":
    main()