Datasets:
Download scripts/shard_utils.py from 1Konny/B-DoPED: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/datasets/1Konny/B-DoPED/resolve/main/scripts/shard_utils.py
- Command line
-
hf download hf://datasets/1Konny/B-DoPED/scripts/shard_utils.py
-
curl -L -o shard_utils.py https://huggingface.co/datasets/1Konny/B-DoPED/resolve/main/scripts/shard_utils.py
3.79 kB
| """ | |
| shard_utils.py — pack rendered pose directories into plain (stored) tar shards. | |
| Each shard `shard_{group:05d}.tar` holds a contiguous block of pose ids; in-tar layout is | |
| `pose_{id:06d}/{modality}/{file}`. Tars are STORED (no compression) since PNG/NPZ are already | |
| compressed. Per-shard sha256 + a `shards_index.csv` are emitted. | |
| Used by render_smal_multiview.py (--shard) to pack rendered output into tar shards. | |
| """ | |
| import os | |
| import csv | |
| import tarfile | |
| import hashlib | |
| from collections import defaultdict | |
| def sha256_file(path, buf=1 << 20): | |
| h = hashlib.sha256() | |
| with open(path, "rb") as f: | |
| for chunk in iter(lambda: f.read(buf), b""): | |
| h.update(chunk) | |
| return h.hexdigest() | |
| def pack_to_shards(items, shard_dir, shard_size, modalities=("rgb", "seg", "npz"), | |
| index_csv=None, log=print): | |
| """ | |
| items : iterable of (pose_id:int, src_pose_dir:str). Need not be sorted. | |
| shard_dir : output directory for shard_*.tar (+ index_csv if given). | |
| shard_size : number of pose ids per shard group (group = pose_id // shard_size). | |
| modalities : subdirectories under each pose dir to include (others, e.g. overlays, are skipped). | |
| Returns the list of index rows (dicts). | |
| """ | |
| os.makedirs(shard_dir, exist_ok=True) | |
| groups = defaultdict(list) | |
| for pid, src in items: | |
| groups[pid // shard_size].append((int(pid), src)) | |
| rows = [] | |
| for g in sorted(groups): | |
| members = sorted(groups[g], key=lambda x: x[0]) | |
| shard_path = os.path.join(shard_dir, f"shard_{g:05d}.tar") | |
| n_files = 0 | |
| with tarfile.open(shard_path, "w") as tar: # "w" = stored, no compression | |
| for pid, src in members: | |
| arc_pose = f"pose_{pid:06d}" | |
| for mod in modalities: | |
| md = os.path.join(src, mod) | |
| if not os.path.isdir(md): | |
| continue | |
| for fn in sorted(os.listdir(md)): | |
| tar.add(os.path.join(md, fn), arcname=f"{arc_pose}/{mod}/{fn}") | |
| n_files += 1 | |
| sz = os.path.getsize(shard_path) | |
| sha = sha256_file(shard_path) | |
| rows.append({"shard": os.path.basename(shard_path), | |
| "pose_start": members[0][0], "pose_end": members[-1][0], | |
| "num_poses": len(members), "num_files": n_files, | |
| "bytes": sz, "sha256": sha}) | |
| log(f"[shard] {os.path.basename(shard_path)} poses {members[0][0]}-{members[-1][0]} " | |
| f"({len(members)}) {sz/1e9:.2f} GB {n_files} files") | |
| if index_csv: | |
| fields = ["shard", "pose_start", "pose_end", "num_poses", "num_files", "bytes", "sha256"] | |
| existing = [] | |
| if os.path.exists(index_csv): | |
| with open(index_csv, newline="") as f: | |
| existing = [r for r in csv.DictReader(f)] | |
| by_shard = {r["shard"]: r for r in existing} | |
| for r in rows: | |
| by_shard[r["shard"]] = {k: str(r[k]) for k in fields} | |
| with open(index_csv, "w", newline="") as f: | |
| w = csv.DictWriter(f, fieldnames=fields) | |
| w.writeheader() | |
| for s in sorted(by_shard): | |
| w.writerow(by_shard[s]) | |
| log(f"[shard] wrote index {index_csv} ({len(by_shard)} shards)") | |
| return rows | |
| def modalities_from_flags(save_rgb=True, save_mask=True, save_npz=True, | |
| save_keypoints=False, save_depth=False, save_canny=False): | |
| mods = [] | |
| if save_rgb: mods.append("rgb") | |
| if save_mask: mods.append("seg") | |
| if save_npz: mods.append("npz") | |
| if save_keypoints: mods.append("rgb_with_keypoints2d") | |
| if save_depth: mods.append("depth") | |
| if save_canny: mods.append("canny") | |
| return tuple(mods) | |