Download scripts/r5_rq2/rq2_loader_bench.py from treoa/turkic-ocr-code: direct link, hf CLI and curl.
- Browser
- Download file 3.4 kB
-
https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_rq2/rq2_loader_bench.py
- Command line
-
hf download hf://treoa/turkic-ocr-code/scripts/r5_rq2/rq2_loader_bench.py
-
curl -L -o rq2_loader_bench.py https://huggingface.co/treoa/turkic-ocr-code/resolve/main/scripts/r5_rq2/rq2_loader_bench.py
3.4 kB
| """Data-loader cost of each RQ2 arm on CPU: samples per second of ONE worker (RQ2LineDS.__getitem__ in this process) on the | |
| first 400 samples the real sampler gives seed 20261121 (full mix_v1, the real crop sizes). Used for the wall-time estimate: | |
| an arm's speed is at most min(GPU rate, workers x this rate). Writes JSON (aggregates only).""" | |
| import argparse | |
| import json | |
| import os | |
| import time | |
| DATA = "/path/to/TURKIC_OCR_DATA" | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--out", required=True) | |
| ap.add_argument("--n", type=int, default=400) | |
| a = ap.parse_args() | |
| import linedet.data as D | |
| from kraken.lib.codec import PytorchCodec | |
| from linedet.common import load_json | |
| from rq2 import geom | |
| from rq2.dataset import OrdinalBS, RQ2LineDS | |
| comps = D.load_components(D.load_mix(DATA + "/data/processed/r5/mix_v1.yaml"), "train") | |
| codec = PytorchCodec(load_json(DATA + "/outputs/r5/linedet/codec_union.json")["c2l"]) | |
| flat = D.FlatItems(comps) | |
| keys = [] | |
| for b in OrdinalBS(D.MixStream(comps, 32, 20261121), 0): | |
| keys.extend(b) | |
| if len(keys) >= a.n: | |
| break | |
| keys = keys[:a.n] | |
| out = {"n": len(keys), "note": "one process, no DataLoader; ZEN4 CPU", "arms": {}} | |
| for H in (32, 64): | |
| spec = geom.apply_spec(H) | |
| for pol in ("typical", "safe"): | |
| ds = RQ2LineDS(flat, spec, codec, train=True, seed=20261121, policy=pol, height=H) | |
| for k in keys[:20]: | |
| ds[k] | |
| t0 = time.time() | |
| st = {} | |
| for k in keys: | |
| g = ds[k]["gid"] | |
| st[g[4]] = st.get(g[4], 0) + 1 | |
| dt = time.time() - t0 | |
| out["arms"]["h%d_%s" % (H, pol)] = {"samples_per_s_one_worker": len(keys) / dt, "status_counts": st} | |
| # the unchanged linedet path at 96 px for reference (kraken augmenter off) | |
| from PIL import Image | |
| ref = D.ArmSpec("ref", 96, 3, 8, 2560) | |
| t0 = time.time() | |
| for g, o in keys: | |
| D.pil_tensor(Image.open(flat.items[g][0]), ref, None) | |
| out["reference_96px_no_aug_samples_per_s"] = len(keys) / (time.time() - t0) | |
| # probe P2 loader (one worker): decode, one op, resize to H, resize to 96, on validation crops | |
| import importlib.util | |
| from linedet.common import read_manifest | |
| sp = importlib.util.spec_from_file_location("rq2_probe_model", "/path/to/TURKIC_OCR_CODE/scripts/r5_rq2/rq2_probe_model.py") | |
| pm = importlib.util.module_from_spec(sp) | |
| sp.loader.exec_module(pm) | |
| D.SPECS["A1"] = D.ArmSpec("A1", 96, 3, 8, 2560) | |
| conds = pm.conditions() | |
| p2 = {} | |
| for set_name, man in pm.SETS.items(): | |
| rows = [r for r in read_manifest(man) if r.get("unit") != "page"][:120] | |
| for c in ("plain96", "control_H32", "typ_H32_s0", "typ_H64_s4", "safe_H64_s0", "typnat_H32_s0", "typnat_H64_s2"): | |
| ds = pm.P2DS(rows, c, conds.index(c), D.SPECS["A1"]) | |
| t0 = time.time() | |
| for i in range(len(rows)): | |
| ds[i] | |
| p2.setdefault(set_name, {})[c] = len(rows) / (time.time() - t0) | |
| out["probe_p2_crops_per_s_one_worker"] = p2 | |
| out["probe_p2_crops_total"] = {"conditions": len(conds), "crops_per_condition": 16238 + 10000, "total": len(conds) * (16238 + 10000)} | |
| with open(a.out, "w") as f: | |
| json.dump(out, f, indent=1) | |
| print(json.dumps(out)) | |
| if __name__ == "__main__": | |
| main() | |