Upload code/faz3_v6/meshai_train/faz3_dataset.py with huggingface_hub
Browse files
code/faz3_v6/meshai_train/faz3_dataset.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Preprocessed-4K loader for Faz3: many views + optional normalized.glb path."""
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
import numpy as np
|
| 9 |
+
import torch
|
| 10 |
+
from PIL import Image
|
| 11 |
+
from torch.utils.data import Dataset
|
| 12 |
+
|
| 13 |
+
from meshai_train.dataset import HF_PREPROCESSED_DEFAULT, PreprocessedMeshDataset, _uid_hf_prefix
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class Faz3PreprocessedDataset(PreprocessedMeshDataset):
|
| 17 |
+
"""20-view textured renders + voxel + HF path to normalized.glb."""
|
| 18 |
+
|
| 19 |
+
def __init__(
|
| 20 |
+
self,
|
| 21 |
+
*,
|
| 22 |
+
token: str | None,
|
| 23 |
+
data_root: Path | None = None,
|
| 24 |
+
hf_repo: str = HF_PREPROCESSED_DEFAULT,
|
| 25 |
+
limit: int | None = None,
|
| 26 |
+
render_size: int = 256,
|
| 27 |
+
max_views: int = 20,
|
| 28 |
+
prefer_tex: bool = True,
|
| 29 |
+
) -> None:
|
| 30 |
+
self.prefer_tex = prefer_tex
|
| 31 |
+
super().__init__(
|
| 32 |
+
token=token,
|
| 33 |
+
data_root=data_root,
|
| 34 |
+
hf_repo=hf_repo,
|
| 35 |
+
limit=limit,
|
| 36 |
+
render_size=render_size,
|
| 37 |
+
max_views=max_views,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def resolve_glb(self, uid: str) -> Path:
|
| 41 |
+
return self._resolve_file(uid, "normalized.glb")
|
| 42 |
+
|
| 43 |
+
def _getitem_one(self, idx: int) -> dict:
|
| 44 |
+
row = self.objects[idx]
|
| 45 |
+
uid = str(row.get("uid") or row.get("object_id"))
|
| 46 |
+
if uid in self._missing_uids:
|
| 47 |
+
raise FileNotFoundError(f"cached missing uid: {uid}")
|
| 48 |
+
try:
|
| 49 |
+
z = self._load_npz(uid)
|
| 50 |
+
except FileNotFoundError:
|
| 51 |
+
self._missing_uids.add(uid)
|
| 52 |
+
raise
|
| 53 |
+
hist = z["vertex_hist"].astype(np.float32)
|
| 54 |
+
stats = z["normal_stats"].astype(np.float32)
|
| 55 |
+
voxel = z["voxel_occ_32"].astype(np.float32).reshape(-1) / 255.0
|
| 56 |
+
|
| 57 |
+
views: list[np.ndarray] = []
|
| 58 |
+
for i in range(self.max_views):
|
| 59 |
+
suffixes = (
|
| 60 |
+
(f"view_{i:02d}_tex.png", f"view_{i:02d}.png")
|
| 61 |
+
if self.prefer_tex
|
| 62 |
+
else (f"view_{i:02d}.png", f"view_{i:02d}_tex.png")
|
| 63 |
+
)
|
| 64 |
+
for suffix in suffixes:
|
| 65 |
+
try:
|
| 66 |
+
img_path = self._resolve_file(uid, f"renders/{suffix}")
|
| 67 |
+
img = Image.open(img_path).convert("RGB").resize(
|
| 68 |
+
(self.render_size, self.render_size), Image.BILINEAR
|
| 69 |
+
)
|
| 70 |
+
arr = np.asarray(img, dtype=np.float32) / 255.0
|
| 71 |
+
views.append(arr.transpose(2, 0, 1))
|
| 72 |
+
break
|
| 73 |
+
except Exception:
|
| 74 |
+
continue
|
| 75 |
+
if not views:
|
| 76 |
+
views.append(np.zeros((3, self.render_size, self.render_size), dtype=np.float32))
|
| 77 |
+
while len(views) < self.max_views:
|
| 78 |
+
views.append(views[-1].copy())
|
| 79 |
+
views = views[: self.max_views]
|
| 80 |
+
|
| 81 |
+
glb_rel = f"{_uid_hf_prefix(uid)}/normalized.glb"
|
| 82 |
+
return {
|
| 83 |
+
"uid": uid,
|
| 84 |
+
"idx": idx,
|
| 85 |
+
"geom_in": np.concatenate([hist, stats]),
|
| 86 |
+
"voxel_tgt": voxel,
|
| 87 |
+
"views": np.stack(views, axis=0),
|
| 88 |
+
"quality_score": float(row.get("quality_score", 0.0)),
|
| 89 |
+
"glb_rel": glb_rel,
|
| 90 |
+
"n_views": float(len(views)),
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def collate_faz3(batch: list[dict[str, Any]]) -> dict[str, Any]:
|
| 95 |
+
return {
|
| 96 |
+
"uid": [b["uid"] for b in batch],
|
| 97 |
+
"glb_rel": [b["glb_rel"] for b in batch],
|
| 98 |
+
"geom_in": torch.from_numpy(np.stack([b["geom_in"] for b in batch])),
|
| 99 |
+
"voxel_tgt": torch.from_numpy(np.stack([b["voxel_tgt"] for b in batch])),
|
| 100 |
+
"views": torch.from_numpy(np.stack([b["views"] for b in batch])),
|
| 101 |
+
"quality_score": torch.tensor([b["quality_score"] for b in batch], dtype=torch.float32),
|
| 102 |
+
}
|