| |
| """Verify an ICW WebDataset release against its manifest.""" |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import hashlib |
| import json |
| import tarfile |
| from collections import Counter, defaultdict |
| from pathlib import Path |
|
|
|
|
| def sha256_file(path: Path) -> str: |
| digest = hashlib.sha256() |
| with path.open("rb") as handle: |
| for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): |
| digest.update(chunk) |
| return digest.hexdigest() |
|
|
|
|
| def main() -> None: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("root", nargs="?", type=Path, default=Path(__file__).resolve().parent) |
| args = parser.parse_args() |
| root = args.root.resolve() |
| manifest = json.loads((root / "manifest.json").read_text(encoding="utf-8")) |
|
|
| split_samples: Counter[str] = Counter() |
| split_identities: dict[str, set[str]] = defaultdict(set) |
| total_samples = 0 |
|
|
| for record in manifest["shards"]: |
| path = root / record["path"] |
| if path.stat().st_size != record["size_bytes"]: |
| raise SystemExit(f"Size mismatch: {path}") |
| if sha256_file(path) != record["sha256"]: |
| raise SystemExit(f"SHA-256 mismatch: {path}") |
|
|
| sample_count = 0 |
| identities: set[str] = set() |
| expected_json_key: str | None = None |
| with tarfile.open(path, "r:") as archive: |
| for member in archive: |
| if not member.isfile(): |
| raise SystemExit(f"Unexpected non-file member: {path}:{member.name}") |
| member_path = Path(member.name) |
| key = member_path.stem |
| if member_path.suffix == ".jpg": |
| if expected_json_key is not None: |
| raise SystemExit(f"Missing JSON after {expected_json_key} in {path}") |
| expected_json_key = key |
| elif member_path.suffix == ".json": |
| if key != expected_json_key: |
| raise SystemExit(f"JPG/JSON ordering mismatch in {path}: {key}") |
| extracted = archive.extractfile(member) |
| if extracted is None: |
| raise SystemExit(f"Cannot read {path}:{member.name}") |
| metadata = json.load(extracted) |
| if metadata["split"] != record["split"]: |
| raise SystemExit(f"Split mismatch in {path}:{member.name}") |
| if metadata["identity_id"] not in key: |
| raise SystemExit(f"Identity/key mismatch in {path}:{member.name}") |
| identities.add(metadata["identity_id"]) |
| sample_count += 1 |
| expected_json_key = None |
| else: |
| raise SystemExit(f"Unexpected suffix in {path}:{member.name}") |
| if expected_json_key is not None: |
| raise SystemExit(f"Missing final JSON in {path}") |
| if sample_count != record["samples"]: |
| raise SystemExit(f"Sample count mismatch in {path}") |
| if len(identities) != record["identities"]: |
| raise SystemExit(f"Identity count mismatch in {path}") |
|
|
| split = record["split"] |
| split_samples[split] += sample_count |
| overlap = split_identities[split].intersection(identities) |
| if overlap: |
| raise SystemExit(f"Identity split across shards: {next(iter(overlap))}") |
| split_identities[split].update(identities) |
| total_samples += sample_count |
| print(f"verified {record['path']}") |
|
|
| split_sets = list(split_identities.items()) |
| for index, (left_name, left_ids) in enumerate(split_sets): |
| for right_name, right_ids in split_sets[index + 1 :]: |
| overlap = left_ids.intersection(right_ids) |
| if overlap: |
| raise SystemExit( |
| f"Identity overlap between {left_name} and {right_name}: {next(iter(overlap))}" |
| ) |
|
|
| if total_samples != manifest["total_samples"]: |
| raise SystemExit("Total sample count mismatch") |
| for split, expected in manifest["splits"].items(): |
| if split_samples[split] != expected["samples"]: |
| raise SystemExit(f"Manifest sample mismatch for {split}") |
| if len(split_identities[split]) != expected["identities"]: |
| raise SystemExit(f"Manifest identity mismatch for {split}") |
| print( |
| f"OK: {total_samples} samples, " |
| f"{sum(len(values) for values in split_identities.values())} identities, " |
| f"{len(manifest['shards'])} shards" |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|