Download full-q98/export_reader.py from ATDLgroup/SAM3Dvid: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/datasets/ATDLgroup/SAM3Dvid/resolve/main/full-q98/export_reader.py
- Command line
-
hf download hf://datasets/ATDLgroup/SAM3Dvid/full-q98/export_reader.py
-
curl -L -o export_reader.py https://huggingface.co/datasets/ATDLgroup/SAM3Dvid/resolve/main/full-q98/export_reader.py
2.68 kB
| """Reader for the training export (numpy, PIL and the stdlib only). | |
| from sam3dvid.export_reader import iter_samples, load_voxels | |
| for clip_id, rgb, visible, info in iter_samples("export/train/train-000000.tar"): | |
| # rgb (32,512,512,3) uint8, visible (32,512,512) uint8 (0 = background, k = object k) | |
| for obj in info["objects"]: | |
| R_co = np.array(obj["R_co"]) # (32,3,3), camera-frame pose per frame, our convention | |
| t_co, s_co = np.array(obj["t_co"]), np.array(obj["s_co"]) # (32,3), (32,3) | |
| voxels = load_voxels("export/train/voxels_train.npz", obj["asset_id"]) # (64,64,64) bool | |
| Samples come out in tar order. Pass `load_voxels` an open `np.load` result to avoid reopening | |
| the file per object. | |
| """ | |
| from __future__ import annotations | |
| import io | |
| import json | |
| import tarfile | |
| from collections.abc import Iterator | |
| from pathlib import Path | |
| import numpy as np | |
| from PIL import Image | |
| VOXEL_RES = 64 | |
| def _decode(group: dict[str, bytes]) -> tuple[str, np.ndarray, np.ndarray, dict]: | |
| rgb, visible = {}, {} | |
| for ext, data in group.items(): | |
| kind, _, rest = ext.partition(".") | |
| if kind == "rgb": | |
| rgb[int(rest.split(".")[0])] = np.asarray(Image.open(io.BytesIO(data)).convert("RGB")) | |
| elif kind == "visible": | |
| visible[int(rest.split(".")[0])] = np.asarray(Image.open(io.BytesIO(data))) | |
| info = json.loads(group["json"]) | |
| return ( | |
| info["clip_id"], | |
| np.stack([rgb[t] for t in sorted(rgb)]), | |
| np.stack([visible[t] for t in sorted(visible)]), | |
| info, | |
| ) | |
| def iter_samples(shard: str | Path) -> Iterator[tuple[str, np.ndarray, np.ndarray, dict]]: | |
| """Yield (clip_id, rgb, visible, info) for every clip in a shard.""" | |
| key, group = None, {} | |
| with tarfile.open(shard, "r|") as tar: | |
| for member in tar: | |
| if not member.isfile(): | |
| continue | |
| name = member.name.rsplit("/", 1)[-1] | |
| member_key, _, ext = name.partition(".") | |
| if member_key != key and group: | |
| yield _decode(group) | |
| group = {} | |
| key = member_key | |
| group[ext] = tar.extractfile(member).read() | |
| if group: | |
| yield _decode(group) | |
| def load_voxels(voxels_npz, asset_id: str) -> np.ndarray: | |
| """Unpack one asset's bool (64,64,64) grid from `voxels_<split>.npz` (a path or np.load result).""" | |
| if isinstance(voxels_npz, (str, Path)): | |
| with np.load(voxels_npz) as f: | |
| return load_voxels(f, asset_id) | |
| bits = np.unpackbits(voxels_npz[asset_id], count=VOXEL_RES**3) | |
| return bits.reshape(VOXEL_RES, VOXEL_RES, VOXEL_RES).astype(bool) | |