RawMoment / verify_release.py
yscript's picture
Organize datasets and begin verified full release upload
e9b4ca5 verified
Raw History Blame
6.76 kB
"""Verify the two-dataset release without extracting images or writing temporary files."""
import argparse
import hashlib
import json
import multiprocessing
import tarfile
from concurrent.futures import ProcessPoolExecutor
from pathlib import Path, PurePosixPath
def rows(path):
return [json.loads(line) for line in path.read_text().splitlines() if line.strip()]
def digest(path):
value = hashlib.sha256()
with path.open("rb") as stream:
for block in iter(lambda: stream.read(4 * 1024 * 1024), b""):
value.update(block)
return value.hexdigest()
def check_archive(task):
root, archive, expected, full = task
path = root / archive["path"]
assert path.stat().st_size == archive["bytes"]
if full:
assert digest(path) == archive["sha256"]
with tarfile.open(path) as stream:
member = stream.next()
assert member.name == expected["path"] and member.isfile()
assert not (member.uid or member.gid or member.mtime or member.uname or member.gname)
content = stream.extractfile(member).read()
assert hashlib.sha256(content).hexdigest() == expected["sha256"]
assert len(content) == expected["bytes"] and content[:8] == b"\x89PNG\r\n\x1a\n"
return archive["path"]
def verify(root, full=False, checksums=True, workers=2):
assert 1 <= workers <= 16
summary = json.loads((root / "dataset.json").read_text())
assert summary["complete"] and summary["layout_version"] == 2
allowed_roots = {"rawmoment_data", "realblur_processed_data"}
assert {p.name for p in root.iterdir() if p.is_dir()} == allowed_roots
if checksums:
for line in (root / "SHA256SUMS").read_text().splitlines():
expected, relative = line.split(" ", 1)
path = PurePosixPath(relative)
assert not path.is_absolute() and ".." not in path.parts
if path.suffix != ".tar":
assert digest(root / relative) == expected
catalog_rows = rows(root / "files.jsonl")
files = {r["path"]: r for r in catalog_rows}
assert len(files) == len(catalog_rows) == summary["unique_images"]
referenced, groups, scenes, counts = set(), {}, {}, {}
manifests = [
(f"rawmoment_data/{split}/{stage}.jsonl", split, stage)
for split in ("train", "val") for stage in ("stage1", "stage2")
] + [("rawmoment_data/test/manifest.jsonl", "test", "test")]
manifests += [(f"realblur_processed_data/{split}/manifest.jsonl", split, "realblur")
for split in ("train", "test")]
for name, split, kind in manifests:
records = rows(root / name)
assert len({r["sample_id"] for r in records}) == len(records)
parent = PurePosixPath(name).parent
for row in records:
assert row["split"] == split
for relative in row["files"].values():
path = PurePosixPath(relative)
assert not path.is_absolute() and ".." not in path.parts
public_path = (parent / path).as_posix()
assert public_path in files
referenced.add(public_path)
if kind == "realblur":
scenes.setdefault(split, set()).add(row["scene"])
assert row["official_split"] == split
assert (row["black_level"], row["white_level"]) == (512, 16383)
assert row["bin_factor"] == 4 and row["noise_scale"] == 1 / 16
height, width = row["size_hw"]
y, x, h, w = row["eval_crop_yxhw"]
assert h == w == 512 and y >= 0 and x >= 0
assert y + h <= height and x + w <= width and y % 16 == x % 16 == 0
assert [y, x] in row["valid_crop_origins_512"]
else:
groups.setdefault(split, set()).add(row["group"])
if kind == "stage1":
assert set(row["files"]) == {"BLUR_RAW", "BLUR_RGB", "CLEAN_RAW", "CLEAN_RGB"}
elif kind == "stage2":
assert set(row["files"]) == {"RAW", "RGB"}
counts[name] = len(records)
assert referenced == set(files)
assert not groups["train"] & groups["val"]
assert not (groups["train"] | groups["val"]) & groups["test"]
assert not scenes["train"] & scenes["test"]
datasets = summary["datasets"]
assert {k: len(v) for k, v in scenes.items()} == datasets["realblur_processed_data"]["scene_counts"]
raw_counts = datasets["rawmoment_data"]["records"]
real_counts = datasets["realblur_processed_data"]["records"]
expected_counts = {
"rawmoment_data/train/stage1.jsonl": raw_counts["train"],
"rawmoment_data/train/stage2.jsonl": raw_counts["train"],
"rawmoment_data/val/stage1.jsonl": raw_counts["val"],
"rawmoment_data/val/stage2.jsonl": raw_counts["val"],
"rawmoment_data/test/manifest.jsonl": raw_counts["test"],
"realblur_processed_data/train/manifest.jsonl": real_counts["train"],
"realblur_processed_data/test/manifest.jsonl": real_counts["test"],
}
assert counts == expected_counts
archives = json.loads((root / "archives.json").read_text())
assert len(archives) == summary["archives"]
assert sum(a["bytes"] for a in archives) == summary["archive_bytes"]
assert sum(f["bytes"] for f in files.values()) == summary["image_bytes"]
assert {f["archive"] for f in files.values()} == {a["path"] for a in archives}
first_images = {}
for row in catalog_rows:
first_images.setdefault(row["archive"], row)
tasks = [(root, archive, first_images[archive["path"]], full) for archive in archives]
if full and workers > 1:
with ProcessPoolExecutor(max_workers=workers, mp_context=multiprocessing.get_context("spawn")) as pool:
verified = list(pool.map(check_archive, tasks))
else:
verified = [check_archive(task) for task in tasks]
assert len(verified) == len(archives)
return {"passed": True, "layout_version": 2, "archives": len(archives),
"full_archive_hashes": full, "sampled_images": len(archives),
"verification_workers": workers if full else 1,
"unique_images": len(files), "referenced_images": len(referenced),
"records": counts, "rawmoment_video_group_overlap": 0, "realblur_scene_overlap": 0}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", type=Path, default=Path(__file__).resolve().parent)
parser.add_argument("--full", action="store_true")
parser.add_argument("--workers", type=int, default=2)
options = parser.parse_args()
print(json.dumps(verify(options.root, options.full, workers=options.workers), indent=2))