File size: 3,312 Bytes
b7e49b8 | 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 | # SPDX-License-Identifier: Apache-2.0
"""CPU only: float32 clean-room reference of a 16-frame 480x640 clip (and of its first ``--n`` frames as a shorter
clip) for the VGA T=16 regression test of the device programs. The 16 distinct frames are built from the golden
480x640 images (img_vga, clip2_480x640) by flips and horizontal / vertical rolls; inputs are uint8 like real video.
python bundles/depthpro2/code/tt_depthpro2/tests/make_clip16_refs.py --sizes S,B,L,H --n 5 [--taps]
Writes ``assets/depthpro2/baseline_ref/clip16_480x640__<size>.safetensors`` (+ .json): ``input_u8`` [16, 480, 640, 3],
``x_norm``, ``inverse_depth`` [16, 480, 640], ``inverse_depth_n<k>`` [k, 480, 640] (the k-frame clip), and with
``--taps`` the 4 taps of the 16-frame clip (stage-level bisection).
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import time
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import torch # noqa: E402
from safetensors.torch import save_file # noqa: E402
import baseline_helpers as H # noqa: E402
from tt_depthpro2.reference import model as ref # noqa: E402
OUT = os.path.join(H.ROOT, "assets/depthpro2/baseline_ref")
def clip16_u8() -> torch.Tensor:
"""16 distinct uint8 frames [16, 480, 640, 3] from the golden 480x640 images."""
a = H.load_golden("img_vga", "S")[0]["input_u8"].round().to(torch.uint8).reshape(480, 640, 3)
c = H.load_golden("clip2_480x640", "S")[0]["input_u8"].round().to(torch.uint8)
base = [a, c[0], c[1]]
frames = []
for i in range(16):
f = base[i % 3]
if (i // 3) % 2:
f = torch.flip(f, dims=[1])
f = torch.roll(f, shifts=(4 * (i % 5), 24 * i), dims=(0, 1))
frames.append(f)
return torch.stack(frames).contiguous()
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--sizes", default="S")
ap.add_argument("--n", default="5")
ap.add_argument("--taps", action="store_true")
a = ap.parse_args()
os.makedirs(OUT, exist_ok=True)
torch.set_grad_enabled(False)
u8 = clip16_u8()
x = ref.preprocess(u8.numpy())
for size in a.sizes.split(","):
t0 = time.time()
m = ref.DepthPro2Reference(size, ref.load_safetensors(os.path.join(H.size_dir(size), "model.safetensors")))
taps, grid = m.encode(x)
rec = {"input_u8": u8, "x_norm": x.contiguous(), "inverse_depth": m.decode(taps, grid)[:, 0].contiguous()}
if a.taps:
for i, t in enumerate(taps):
rec[f"tap{i}"] = t.contiguous()
for k in (int(v) for v in a.n.split(",") if v):
tk, _ = m.encode(x[:k])
rec[f"inverse_depth_n{k}"] = m.decode(tk, grid)[:, 0].contiguous()
base = os.path.join(OUT, f"clip16_480x640__{size}")
save_file(rec, base + ".safetensors")
meta = {"case": "clip16_480x640", "size": size, "source": "clean-room reference (float32 CPU)",
"seconds": round(time.time() - t0, 1), "keys": {k: list(v.shape) for k, v in rec.items()}}
with open(base + ".json", "w") as f:
json.dump(meta, f, indent=1)
print(size, meta, flush=True)
if __name__ == "__main__":
main()
|