Datasets:
Tasks:
Image-to-3D
Modalities:
Tabular
Formats:
parquet
Languages:
English
Size:
10K - 100K
License:
| #!/usr/bin/env python3 | |
| """Build stratified bucket manifest from princeton-vl/LayeredDepth-Syn.""" | |
| from __future__ import annotations | |
| import argparse | |
| import csv | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from .preprocess import ( | |
| DEFAULT_LAYER_IDS, | |
| compressed_layer_count_per_pixel, | |
| load_depth_layers_from_row, | |
| sort_depth_with_mask, | |
| ) | |
| DEFAULT_BATCH_MIX = {"1": 0.25, "2": 0.25, "3": 0.25, "4": 0.25} | |
| def assign_bucket( | |
| compressed_count: np.ndarray, | |
| valid_fraction: float, | |
| *, | |
| bucket_mode: str, | |
| majority_fraction: float, | |
| min_valid_fraction: float, | |
| ) -> str: | |
| if valid_fraction < min_valid_fraction: | |
| return "1" | |
| if bucket_mode == "scene_max": | |
| scene_value = int(np.max(compressed_count)) | |
| else: | |
| counts = np.arange(1, 5) | |
| fractions = [(compressed_count >= count).mean() for count in counts] | |
| scene_value = 1 | |
| for count, fraction in zip(counts, fractions): | |
| if fraction >= majority_fraction: | |
| scene_value = int(count) | |
| return str(max(1, min(4, scene_value))) | |
| def build_manifest( | |
| *, | |
| dataset_name: str = "princeton-vl/LayeredDepth-Syn", | |
| split: str = "train", | |
| cache_dir: str | None = None, | |
| max_samples: int | None = None, | |
| output_dir: str | Path = "artifacts/layereddepth_stratified", | |
| bucket_mode: str = "scene_max", | |
| majority_fraction: float = 0.10, | |
| min_valid_fraction: float = 0.01, | |
| abs_gap_threshold: float = 1e-4, | |
| rel_gap_threshold: float = 0.0, | |
| ) -> dict: | |
| from datasets import load_dataset | |
| output_dir = Path(output_dir) | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| kwargs = {"split": split, "streaming": False} | |
| if cache_dir: | |
| kwargs["cache_dir"] = cache_dir | |
| dataset = load_dataset(dataset_name, **kwargs) | |
| rows = [] | |
| buckets: dict[str, list[int]] = {str(i): [] for i in range(1, 5)} | |
| for row_index, row in enumerate(dataset): | |
| if max_samples is not None and row_index >= max_samples: | |
| break | |
| depth = load_depth_layers_from_row(row, layer_ids=DEFAULT_LAYER_IDS) | |
| sorted_depth, sorted_mask = sort_depth_with_mask(depth) | |
| compressed = compressed_layer_count_per_pixel( | |
| sorted_depth, | |
| sorted_mask, | |
| abs_gap_threshold=abs_gap_threshold, | |
| rel_gap_threshold=rel_gap_threshold, | |
| ) | |
| raw_count = sorted_mask.sum(axis=-1) | |
| valid_fraction = float((raw_count > 0).mean()) | |
| bucket = assign_bucket( | |
| compressed, | |
| valid_fraction, | |
| bucket_mode=bucket_mode, | |
| majority_fraction=majority_fraction, | |
| min_valid_fraction=min_valid_fraction, | |
| ) | |
| buckets[bucket].append(row_index) | |
| rows.append( | |
| { | |
| "row_index": row_index, | |
| "sample_key": str(row.get("__key__", row_index)), | |
| "valid_fraction": valid_fraction, | |
| "scene_max_compressed": int(np.max(compressed)), | |
| "compressed_ge2_fraction": float((compressed >= 2).mean()), | |
| "compressed_ge3_fraction": float((compressed >= 3).mean()), | |
| "compressed_ge4_fraction": float((compressed >= 4).mean()), | |
| "bucket": bucket, | |
| } | |
| ) | |
| if not rows: | |
| raise RuntimeError("No rows scanned.") | |
| summary = { | |
| "dataset": dataset_name, | |
| "split": split, | |
| "samples_scanned": len(rows), | |
| "bucket_mode": bucket_mode, | |
| "majority_fraction": majority_fraction, | |
| "min_valid_fraction": min_valid_fraction, | |
| "bucket_histogram": {k: len(v) for k, v in sorted(buckets.items(), key=lambda x: int(x[0]))}, | |
| "batch_mix": DEFAULT_BATCH_MIX, | |
| } | |
| manifest = {"meta": summary, "batch_mix": DEFAULT_BATCH_MIX, "buckets": buckets} | |
| (output_dir / "summary.json").write_text(json.dumps(summary, indent=2), encoding="utf-8") | |
| (output_dir / "bucket_manifest.json").write_text(json.dumps(manifest, indent=2), encoding="utf-8") | |
| with (output_dir / "sample_buckets.csv").open("w", newline="", encoding="utf-8") as handle: | |
| writer = csv.DictWriter(handle, fieldnames=list(rows[0].keys())) | |
| writer.writeheader() | |
| writer.writerows(rows) | |
| return summary | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--dataset", default="princeton-vl/LayeredDepth-Syn") | |
| parser.add_argument("--split", default="train") | |
| parser.add_argument("--cache-dir", default=None) | |
| parser.add_argument("--max-samples", type=int, default=None) | |
| parser.add_argument("--output-dir", default="artifacts/layereddepth_stratified") | |
| parser.add_argument("--bucket-mode", choices=("scene_max", "scene_majority"), default="scene_max") | |
| parser.add_argument("--majority-fraction", type=float, default=0.10) | |
| parser.add_argument("--min-valid-fraction", type=float, default=0.01) | |
| args = parser.parse_args() | |
| summary = build_manifest( | |
| dataset_name=args.dataset, | |
| split=args.split, | |
| cache_dir=args.cache_dir, | |
| max_samples=args.max_samples, | |
| output_dir=args.output_dir, | |
| bucket_mode=args.bucket_mode, | |
| majority_fraction=args.majority_fraction, | |
| min_valid_fraction=args.min_valid_fraction, | |
| ) | |
| print(json.dumps(summary, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |