Download scripts/scan_recap_torchcodec.py from myx160/rl-post-train-artifacts: direct link, hf CLI and curl.
- Browser
- Download file 2.79 kB
-
https://huggingface.co/datasets/myx160/rl-post-train-artifacts/resolve/main/scripts/scan_recap_torchcodec.py
- Command line
-
hf download hf://datasets/myx160/rl-post-train-artifacts/scripts/scan_recap_torchcodec.py
-
curl -L -o scan_recap_torchcodec.py https://huggingface.co/datasets/myx160/rl-post-train-artifacts/resolve/main/scripts/scan_recap_torchcodec.py
2.79 kB
| #!/usr/bin/env python | |
| """Bounded torchcodec scan for canonical RECAP MP4 files. | |
| The official LeRobot path uses torchcodec with approximate seeking. This | |
| utility exercises that same decoder on every frame (in small batches) and | |
| prints only files that raise an exception; it never writes to the dataset. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| from pathlib import Path | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--data-root", type=Path, default=Path("datasets/recap_libero10_task0")) | |
| parser.add_argument("--split", default="libero10_task0_train") | |
| parser.add_argument("--workers", type=int, default=16) | |
| parser.add_argument("--chunk-size", type=int, default=32) | |
| parser.add_argument("--max-files", type=int, default=None) | |
| return parser.parse_args() | |
| def scan_one(path: Path, chunk_size: int) -> str | None: | |
| try: | |
| from torchcodec.decoders import VideoDecoder | |
| decoder = VideoDecoder(path, device="cpu", seek_mode="approximate") | |
| frame_count = len(decoder) | |
| if frame_count <= 0: | |
| return f"{path}\tno_frames" | |
| for start in range(0, frame_count, chunk_size): | |
| decoder.get_frames_at(indices=list(range(start, min(start + chunk_size, frame_count)))) | |
| return None | |
| except Exception as exc: # pragma: no cover - codec/platform dependent | |
| return f"{path}\t{type(exc).__name__}: {exc}" | |
| def main() -> int: | |
| args = parse_args() | |
| if args.workers < 1 or args.chunk_size < 1: | |
| raise ValueError("workers and chunk-size must be positive") | |
| split_path = args.data_root.resolve() / args.split / "videos" | |
| files = sorted(split_path.glob("**/*.mp4")) | |
| if args.max_files is not None: | |
| files = files[: args.max_files] | |
| print(f"SCAN_START\tfiles={len(files)}\tworkers={args.workers}\tchunk_size={args.chunk_size}", flush=True) | |
| failures: list[str] = [] | |
| completed = 0 | |
| with ThreadPoolExecutor(max_workers=args.workers) as executor: | |
| futures = [executor.submit(scan_one, path, args.chunk_size) for path in files] | |
| for future in as_completed(futures): | |
| completed += 1 | |
| result = future.result() | |
| if result is not None: | |
| failures.append(result) | |
| print(f"DECODE_FAILURE\t{result}", flush=True) | |
| if completed == 1 or completed % 100 == 0 or completed == len(files): | |
| print(f"SCAN_PROGRESS\t{completed}/{len(files)}\tfailures={len(failures)}", flush=True) | |
| print(f"SCAN_DONE\tfiles={len(files)}\tfailures={len(failures)}", flush=True) | |
| return 1 if failures else 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |