embeddinggemma-2-coreai / tests /make_preprocess_cases.py
iannedev's picture
v1.1-images
80c945e verified
Raw History Blame Contribute Delete
2.11 kB
#!/usr/bin/env python3
# Copyright 2026 Everpaper. Licensed under the Apache License, Version 2.0 (see ../LICENSE).
"""Write `preprocess-cases.json`: for every image in tests/images/, the SHA-256 of the decoded 8-bit RGB buffer and
of the resized buffer, plus the sizes and the soft-token count, computed with the Python reference preprocessing
(python/eg2_image.py, itself bit-identical to torchvision's uint8 bicubic). `eg2-cli pixcheck` compares the Swift
decoder + resampler against it. Lossy (JPEG) cases are flagged: decoders differ by a few values per sample there.
python3 make_preprocess_cases.py [--images images] [--out preprocess-cases.json]
"""
from __future__ import annotations
import argparse
import hashlib
import json
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE.parent / "python"))
import eg2_image # noqa: E402
def main() -> None:
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("--images", default=str(HERE / "images"))
ap.add_argument("--out", default=str(HERE / "preprocess-cases.json"))
args = ap.parse_args()
items = json.loads((Path(args.images) / "manifest.json").read_text())
cases = []
for it in items:
rgb = eg2_image.load_rgb(Path(args.images) / it["file"])
th, tw = eg2_image.target_size(rgb.shape[0], rgb.shape[1])
res = eg2_image.resize_bicubic_aa_u8(rgb, th, tw)
vi = eg2_image.patchify_cell_major(res)
cases.append(dict(file=f"{Path(args.images).name}/{it['file']}", width=int(rgb.shape[1]), height=int(rgb.shape[0]),
resized_width=tw, resized_height=th, n_soft=vi.n_soft, lossy=bool(it.get("lossy", False)),
decoded_sha256=hashlib.sha256(rgb.tobytes()).hexdigest(), resized_sha256=hashlib.sha256(res.tobytes()).hexdigest()))
print(cases[-1]["file"], f"{rgb.shape[1]}x{rgb.shape[0]} -> {tw}x{th}, n={vi.n_soft}")
Path(args.out).write_text(json.dumps(cases, indent=1) + "\n")
print("wrote", args.out)
if __name__ == "__main__":
main()