Datasets:
Download examples/read_one_sample.py from Tome1212/TopoObjaverse: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/datasets/Tome1212/TopoObjaverse/resolve/main/examples/read_one_sample.py
- Command line
-
hf download hf://datasets/Tome1212/TopoObjaverse/examples/read_one_sample.py
-
curl -L -o read_one_sample.py https://huggingface.co/datasets/Tome1212/TopoObjaverse/resolve/main/examples/read_one_sample.py
2.34 kB
| """Read one sample from a shard with nothing but the standard library, safetensors and zstd. | |
| python examples/read_one_sample.py data/validation/validation-00000-of-00001.tar | |
| """ | |
| import io | |
| import json | |
| import sys | |
| import tarfile | |
| import zstandard | |
| from safetensors.numpy import load as st_load | |
| COMPRESSED = {"mesh.safetensors", "queries.safetensors", "visibility.safetensors", | |
| "meta.json"} | |
| def members_of_first_sample(tar_path): | |
| """The tar is written so a sample's members are always adjacent.""" | |
| with tarfile.open(tar_path, "r:") as tf: | |
| key, out = None, {} | |
| for m in tf: | |
| if not m.isfile(): | |
| continue | |
| k = m.name.rsplit("/", 1)[-1].split(".", 1)[0] | |
| if key is None: | |
| key = k | |
| if k != key: | |
| break | |
| name = m.name.rsplit("/", 1)[-1][len(k) + 1:] | |
| out[name] = tf.extractfile(m).read() | |
| return key, out | |
| def decode(name, blob): | |
| if name.endswith(".zst"): | |
| blob, name = zstandard.ZstdDecompressor().decompress(blob), name[:-4] | |
| if name == "meta.json": | |
| return json.loads(blob) | |
| if name.endswith(".safetensors"): | |
| return st_load(blob) | |
| return blob # a PNG, still encoded | |
| if __name__ == "__main__": | |
| uid, raw = members_of_first_sample(sys.argv[1]) | |
| payload = {} | |
| for name, blob in raw.items(): | |
| clean = name[:-4] if name.endswith(".zst") else name | |
| payload[clean] = decode(name, blob) | |
| print(f"uid {uid}") | |
| mesh, q = payload["mesh.safetensors"], payload["queries.safetensors"] | |
| print(f" faces {mesh['faces'].shape[0]:,} vertices {mesh['vertices'].shape[0]:,}") | |
| for fam in ("xatlas", "smart_uv", "partial"): | |
| valid = (q[f"{fam}_valid_mask"] > 0).mean() | |
| print(f" {fam:9s} texture {q[f'{fam}_gt_texture'].shape} " | |
| f"valid {valid:.1%}") | |
| vis = payload["visibility.safetensors"]["xatlas_vis"] | |
| print(f" visibility {vis.shape} visible texels per view " | |
| f"{[int((vis[i] == 2).sum()) for i in range(vis.shape[0])]}") | |
| views = [k for k in payload if k.endswith(".png")] | |
| print(f" views {sorted(views)}") | |
| meta = payload["meta.json"] | |
| print(f" release {meta.get('release_version')} schema {meta.get('schema_version')}") | |