File size: 6,099 Bytes
a1af790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Check official split counts, every referenced image, and sampled JPEG decoding."""
from pathlib import Path
import collections
import csv
import hashlib
import json
import os
import sys
import time

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "code/vendor"))
import ijson
from PIL import Image

EXPECTED = {"train": 1361062, "val": 380125, "test": 849160}


def main():
    parts = json.loads((ROOT / "audit/image_parts_verified.json").read_text())
    annotations = json.loads((ROOT / "audit/annotations_verification.json").read_text())
    extraction = json.loads((ROOT / "audit/images_extraction.json").read_text())
    assert parts["complete"] and len(parts["verified"]) == 44
    assert annotations["complete"] and len(annotations["verified"]) == 14
    assert extraction["status"] == "complete"
    rows, errors = [], []
    for split, expected in EXPECTED.items():
        count = missing = zero = decoded = mismatched = 0
        annotated_captures, subjects, cameras, sequences = set(), set(), set(), set()
        source = ROOT / "annotations" / split / f"InterHand2.6M_{split}_data.json"
        sample_every = max(1, expected // 128)
        with source.open("rb") as f:
            for item in ijson.items(f, "images.item"):
                path = ROOT / "images" / split / item["file_name"]
                count += 1
                annotated_captures.add(item["capture"])
                subjects.add(item["subject"])
                cameras.add(item["camera"])
                sequences.add((item["capture"], item["seq_name"]))
                try:
                    size = path.stat().st_size
                except FileNotFoundError:
                    missing += 1
                    if len(errors) < 100:
                        errors.append({"type": "missing_image", "path": str(path)})
                    continue
                if size == 0:
                    zero += 1
                if (count - 1) % sample_every == 0:
                    try:
                        with Image.open(path) as image:
                            image.load()
                            if image.size != (item["width"], item["height"]):
                                mismatched += 1
                                errors.append({"type": "image_dimension_mismatch", "path": str(path)})
                        decoded += 1
                    except Exception as exc:
                        errors.append({"type": "image_decode_failure", "path": str(path), "error": str(exc)})
        row = {"split": split, "official_expected_images": expected, "annotation_images": count,
               "missing_referenced_images": missing, "empty_referenced_images": zero,
               "jpeg_samples_decoded": decoded, "sample_dimension_mismatches": mismatched,
               "annotated_captures": len(annotated_captures), "subjects": len(subjects),
               "camera_ids": len(cameras), "capture_sequence_pairs": len(sequences)}
        rows.append(row)
        print("CHECKED", json.dumps(row), flush=True)
        if count != expected:
            errors.append({"type": "unexpected_split_count", "split": split, "count": count})
    # Count the extracted tree independently of the annotation index.
    raw_counts = collections.Counter()
    raw_bytes = 0
    for split in EXPECTED:
        for base, dirs, files in os.walk(ROOT / "images" / split):
            for name in files:
                if name.lower().endswith((".jpg", ".jpeg", ".png")):
                    raw_counts[split] += 1
                    raw_bytes += (Path(base) / name).stat().st_size
    for row in rows:
        row["extracted_image_files"] = raw_counts[row["split"]]
    with (ROOT / "audit/split_inventory.csv").open("w") as f:
        writer = csv.DictWriter(f, fieldnames=list(rows[0]))
        writer.writeheader()
        writer.writerows(rows)
    # Checksums also cover annotation files and the official source documents.
    lines = []
    for entry in parts["verified"]:
        lines.append(entry["sha256"] + "  archives/5fps/" + entry["name"])
    for entry in annotations["verified"]:
        lines.append(entry["sha256"] + "  " + entry["local_path"])
    supplementary_path = ROOT / "audit/human_annotation_ids.json"
    if supplementary_path.exists():
        for entry in json.loads(supplementary_path.read_text()):
            lines.append(entry["sha256"] + "  " + entry["local_path"])
    for folder in ["sources", "code"]:
        for path in sorted((ROOT / folder).iterdir()):
            if path.is_file():
                lines.append(hashlib.sha256(path.read_bytes()).hexdigest() + "  " + str(path.relative_to(ROOT)))
    (ROOT / "audit/SHA256SUMS").write_text("\n".join(lines) + "\n")
    missing_total = sum(row["missing_referenced_images"] for row in rows)
    empty_total = sum(row["empty_referenced_images"] for row in rows)
    report = {"status": "PASS" if not errors and not missing_total and not empty_total else "FAIL",
              "dataset": "InterHand2.6M", "version": "v1.0", "fps": 5,
              "host": os.uname().nodename, "root": str(ROOT), "checked_unix": time.time(),
              "official_md5_parts_verified": len(parts["verified"]),
              "annotation_files_validated": len(annotations["verified"]),
              "referenced_images_checked": sum(row["annotation_images"] for row in rows),
              "extracted_image_files": sum(raw_counts.values()), "extracted_image_bytes": raw_bytes,
              "image_archive_bytes": sum(x["bytes"] for x in parts["verified"]),
              "splits": rows, "errors": errors,
              "scope": "Full official v1.0 5 fps train/val/test with current official annotations; excludes 30 fps expansion.",
              "note": "File integrity and annotation/image correspondence checks are not a GT-accuracy certification."}
    (ROOT / "audit/final_validation.json").write_text(json.dumps(report, indent=2) + "\n")
    print("FINAL", report["status"], flush=True)
    if report["status"] != "PASS":
        raise SystemExit(1)


if __name__ == "__main__":
    main()