Download run_stream.py from Yakimask/Toru3D: direct link, hf CLI and curl.
- Browser
- Download file 4.42 kB
-
https://huggingface.co/Yakimask/Toru3D/resolve/main/run_stream.py
- Command line
-
hf download hf://Yakimask/Toru3D/run_stream.py
-
curl -L -o run_stream.py https://huggingface.co/Yakimask/Toru3D/resolve/main/run_stream.py
4.42 kB
| # つけっぱなしで動かす(stream.py)。写真の並びを何周もつなげて流し、地図の大きさ・VRAM・速さが増えないかを、途中の進みとして出す | |
| # 使い方: python run_stream.py --images path/to/rgb --fx .. --fy .. --cx .. --cy .. --out out [--loops 3] [--gt groundtruth.txt] | |
| import os, sys, glob, time, argparse, json, numpy as np, cv2, torch | |
| HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE); sys.path.insert(0, os.path.join(HERE, "third_party")) | |
| import frontend as TE | |
| from stream import Toru3D | |
| from run import write_ply | |
| ap = argparse.ArgumentParser(); ap.add_argument("--images", required=True); ap.add_argument("--out", required=True); ap.add_argument("--loops", type=int, default=1) | |
| for k in ("fx", "fy", "cx", "cy"): ap.add_argument("--" + k, type=float, required=True) | |
| ap.add_argument("--map_mb", type=float, default=100.0); ap.add_argument("--win", type=int, default=12); ap.add_argument("--gt") | |
| ap.add_argument("--depth_res", type=int, default=518); ap.add_argument("--half", action="store_true"); ap.add_argument("--q8", action="store_true") | |
| a = ap.parse_args(); W = os.path.join(HERE, "weights") | |
| fs = sorted(glob.glob(os.path.join(a.images, "*.png")) + glob.glob(os.path.join(a.images, "*.jpg"))); h, w = cv2.imread(fs[0]).shape[:2] | |
| sx, sy = TE.W / w, TE.H / h; K = [[a.fx * sx, 0, a.cx * sx], [0, a.fy * sy, a.cy * sy], [0, 0, 1]] | |
| st = np.array([float(os.path.splitext(os.path.basename(f))[0]) if os.path.splitext(os.path.basename(f))[0].replace(".", "").isdigit() else k / 30.0 for k, f in enumerate(fs)]) | |
| sys_ = Toru3D(K, os.path.join(W, "da3-small", "model.safetensors"), os.path.join(W, "posenet.pt"), os.path.join(W, "finenet.pt"), os.path.join(W, "metric_video_depth_anything_vits.pth"), a.out, WIN=a.win, MAP_MB=a.map_mb, depth_res=a.depth_res, half=a.half, q8=a.q8) | |
| log = open(os.path.join(a.out, "progress.log"), "w", encoding="utf-8") | |
| def say(m): print(m, flush=True); log.write(m + "\n"); log.flush() | |
| say(f"=== {time.strftime('%m-%d %H:%M')} {len(fs)} コマ × {a.loops} 周・作業場 {a.win} 枚・地図の上限 {a.map_mb:.0f}MB") | |
| t0 = time.time(); fid = 0; span = st[-1] - st[0] + 1.0; hist = [] | |
| for lp in range(a.loops): | |
| for k, f in enumerate(fs): | |
| sys_.step(fid, st[k] + lp * span, cv2.imread(f)); fid += 1 | |
| if fid % 200 == 0: | |
| torch.cuda.synchronize(); el = time.time() - t0 | |
| r = dict(frame=fid, loop=lp + 1, sec=el, fps=fid / el, map_mb=sys_.map_bytes() / 1e6, places=len(sys_.places), shape_vox=len(sys_.shape_k), win=len(sys_.win), | |
| vram_now=torch.cuda.memory_allocated() / 2**20, vram_max=torch.cuda.max_memory_allocated() / 2**20, held=len(sys_.S.feats), **sys_.stat) | |
| hist.append(r) | |
| say(f"[{fid} コマ・{lp+1} 周目] 地図 {r['map_mb']:.1f}MB(場所の札 {r['places']} 枚・形 {r['shape_vox']:,} ます) VRAM 今 {r['vram_now']:.0f}MB・最大 {r['vram_max']:.0f}MB " | |
| f"持っている名札 {r['held']} 枚 基準のコマ 計 {r['kfs']}・戻った {r['loops']}・当て直し {r['relocs']} {r['fps']:.1f} コマ/秒") | |
| sz = sys_.save_map(); sys_.traj.close() | |
| if sys_.use_mesh: nv, nf = sys_.write_mesh_ply(os.path.join(a.out, "mesh.ply")); say(f"形の地図(ポリゴン): 区画 {len(sys_.meshes)}・頂点 {nv:,}・三角形 {nf:,}") | |
| else: P, C = sys_.shape_points(); write_ply(os.path.join(a.out, "points.ply"), P, C) | |
| say(f"--- 終わり: {fid} コマ・{time.time()-t0:.0f} 秒 地図のファイル 形 {sz['shape_map.npz']/1e6:.1f}MB・場所の札 {sz['place_map.npz']/1e6:.1f}MB(合計 {(sz['shape_map.npz']+sz['place_map.npz'])/1e6:.1f}MB) VRAM の最大 {torch.cuda.max_memory_allocated()/2**20:.0f}MB") | |
| if a.gt: # 1 周目の道を正解と比べる(ずれの測り方は eval_util と同じ) | |
| import eval_util as EU | |
| from scipy.spatial.transform import Rotation as Rot | |
| E = np.loadtxt(os.path.join(a.out, "trajectory.txt"))[:len(fs)]; G = np.loadtxt(a.gt, comments="#"); j = np.abs(G[:, 0][None] - st[:, None]).argmin(1) | |
| c2w = {} | |
| for k in range(len(E)): | |
| T = np.eye(4); T[:3, :3] = Rot.from_quat(E[k, 4:8]).as_matrix(); T[:3, 3] = E[k, 1:4]; c2w[k] = T | |
| r = EU.evaluate(c2w, G[j, 1:4], G[j, 4:8], np.abs(G[j, 0] - st), st); say("1 周目の道: " + EU.line(r)) | |
| json.dump(hist, open(os.path.join(a.out, "progress.json"), "w"), indent=1) | |