File size: 4,461 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
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
"""Display saved EMA teacher predictions from matched distillation runs."""

from __future__ import annotations

import numpy as np

from demo.scale4d_view_count.bundle import validate_scene
from demo.scale4d_view_count.camera import camera_scene_payload
from demo.scale4d_view_count.export import track_layer


def teacher_scene_payload(
    inputs: dict, labels: dict, predictions: dict, record: dict
) -> dict:
    first = predictions["b3_ema"]
    checkpoint_step = first["metadata"]["checkpoint_step"]
    if not isinstance(checkpoint_step, int) or checkpoint_step <= 0:
        raise ValueError("Expected a positive EMA checkpoint step")
    base_record = {
        **record,
        "position_source": "xyz",
        **{
            mode: first["metadata"]["scores"][mode]
            for mode in first["camera_modes"].tolist()
        },
    }
    base_results = {
        "xyz": first["positions"],
        "camera_modes": first["camera_modes"],
        "gt_tracks_target_camera": labels["gt_tracks_target_camera"],
        "gt_visibility": labels["visibility"],
    }
    scene = camera_scene_payload(inputs, base_results, base_record)
    visible_points = labels["gt_tracks_target_camera"][labels["visibility"]]
    scene["extent"] = max(
        float(
            np.percentile(
                np.linalg.norm(visible_points - np.asarray(scene["target"]), axis=-1),
                95,
            )
        ),
        0.06,
    )
    scene["fov"] = 55.0
    scene["states"] = []
    provenance = {}
    for model, values in predictions.items():
        metadata = values["metadata"]
        if (
            metadata["weight_role"] != "ema_teacher"
            or metadata["checkpoint_step"] != checkpoint_step
        ):
            raise ValueError("Expected saved EMA teachers from the same checkpoint step")
        if metadata.get("query_fingerprint") != record.get("query_fingerprint"):
            raise ValueError("Teacher predictions do not match the scored query set")
        provenance[model] = {
            "teacher_state_sha256": metadata["teacher_state_sha256"],
            "checkpoint_step": metadata["checkpoint_step"],
            "weight_role": metadata["weight_role"],
        }
        for index, camera in enumerate(values["camera_modes"].tolist()):
            positions = values["positions"][index]
            valid = np.isfinite(positions).all(-1) & (positions[..., 2] > 0)
            valid &= np.abs(positions).max(-1) < 32
            scores = metadata["scores"][camera]
            state = {
                "model": model,
                "camera": camera,
                "views": 3,
                **track_layer(positions, valid),
                "aj": scores["aj"],
                "apd": scores["apd"],
                "mean_error_cm": scores["epe"],
            }
            if "dense_positions" in values:
                state["dense"] = {
                    **track_layer(
                        values["dense_positions"][index],
                        values["dense_valid"][index],
                    ),
                    "colors": values["dense_colors"].tolist(),
                }
            scene["states"].append(state)
    scene["model_labels"] = {"b2_ema": "EMA teacher B2", "b3_ema": "EMA teacher B3"}
    scene["model_description"] = (
        f"Saved EMA teacher weights at {checkpoint_step:,} fine-tuning steps "
        "from the common pre-Forge 50k initializer"
    )
    scene["comparison_description"] = (
        "B2 and B3 are separately trained EMA teachers. ON/OFF states within each "
        "teacher use the same weights, images, queries, and three views. The default "
        "compares B3 OFF with B2 OFF to evaluate the effect of camera distillation "
        "when camera conditioning is unavailable at inference."
    )
    scene["checkpoint_iteration"] = checkpoint_step
    scene["match_teacher_inputs"] = True
    scene["checkpoint_provenance"] = provenance
    scene["default_comparison"] = {
        "views": 3,
        "primary_model": "b3_ema",
        "primary_camera": "off",
        "comparison_model": "b2_ema",
        "comparison_camera": "off",
    }
    scene["benchmark_scores"] = [
        {
            "model": state["model"],
            "camera": state["camera"],
            "views": 3,
            "aj": state["aj"],
            "apd": state["apd"],
        }
        for state in scene["states"]
    ]
    validate_scene(scene)
    return scene