-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_demo.py
More file actions
59 lines (41 loc) · 1.72 KB
/
Copy pathmain_demo.py
File metadata and controls
59 lines (41 loc) · 1.72 KB
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
# -*- coding: utf-8 -*-
"""
Fast 2DGS — Python API quick start. Run: python main_demo.py
"""
from __future__ import annotations
from pathlib import Path
from torchvision.utils import save_image
from engine import Fast2DGEngine, compute_psnr, load_image
ROOT = Path(__file__).resolve().parent
IMAGE = ROOT / "assets" / "anime-1_2k.png"
OUT = ROOT / "outputs" / "demo"
API_GUIDE = """
# Fast 2DGS inference (Python API)
from engine import Fast2DGEngine, load_image
engine = Fast2DGEngine(K=50000, tune_steps=3000)
imgs = load_image("assets/anime-1_2k.png", image_size=512, crop=True, device=engine.device)
# Option A: step-by-step
pred, heatmap, params = engine.forward(imgs) # encode -> sample -> rasterize
tune = engine.tune(imgs, params, tune_steps=3000) # fine-tune
# Option B: one call
imgs, result = engine.run_image("assets/anime-1_2k.png")
print(result.psnr_init, result.psnr_tuned)
"""
def main() -> None:
print(API_GUIDE.strip())
print("\n>>> Running demo...\n")
engine = Fast2DGEngine(K=50000, tune_steps=3000)
imgs = load_image(IMAGE, image_size=engine.cfg.image_size,
crop=engine.cfg.crop, device=engine.device)
pred, heatmap, params = engine.forward(imgs)
psnr_init = compute_psnr(pred, imgs)
print(f"forward: PSNR={psnr_init:.2f} dB")
tune = engine.tune(imgs, params, tune_steps=engine.cfg.tune_steps)
print(f"tune: PSNR={tune.psnr:.2f} dB ({tune.tune_time:.1f}s)")
OUT.mkdir(parents=True, exist_ok=True)
save_image(imgs[0].clamp(0, 1), OUT / "gt.png")
save_image(pred[0].clamp(0, 1), OUT / "pred_init.png")
save_image(tune.pred[0].clamp(0, 1), OUT / "pred_tune.png")
print(f"saved: {OUT.resolve()}")
if __name__ == "__main__":
main()