Download code/tt_depthpro2/tests/make_clip16_refs.py from changh95/DepthPro2: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/make_clip16_refs.py
- Command line
-
hf download hf://changh95/DepthPro2/code/tt_depthpro2/tests/make_clip16_refs.py
-
curl -L -o make_clip16_refs.py https://huggingface.co/changh95/DepthPro2/resolve/main/code/tt_depthpro2/tests/make_clip16_refs.py
3.31 kB
| # 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() | |