turkic-ocr-code / src /linedet /collect.py
treoa's picture
Release 1
24aaa76
Raw History Blame Contribute Delete
2.71 kB
"""Collect results of the line arms into results.json and arch.json (numbers read from the prediction meta files, the training
logs, the timing files and the model objects).
Usage: python -m linedet.collect --out <dir>
"""
import argparse
import glob
import json
import os
from .common import ARMS, LD_CKPT, LD_OUT, PRED_DIR, load_json, save_json, utcnow
def last_train_event(arm, name):
p = os.path.join(LD_OUT, "train_logs", arm + ".jsonl")
if not os.path.exists(p):
return None
ev = None
for line in open(p):
try:
d = json.loads(line)
except Exception:
continue
if d.get("event") == "saved" and d.get("ckpt") == name:
ev = d
return ev
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--out", default=LD_OUT)
a = ap.parse_args()
rows = []
for arm in ARMS:
for ck in ("short", "long", "partial", "short_lm", "long_lm", "partial_lm"):
mp = os.path.join(PRED_DIR, arm, ck, "meta.json")
if not os.path.exists(mp):
continue
m = load_json(mp)
base_ck = ck.replace("_lm", "")
tr = last_train_event(arm, base_ck) or {}
for sname, s in m["per_set"].items():
if "cer" not in s:
rows.append({"arm": arm, "checkpoint": ck, "set": sname, "skipped": s.get("skipped")})
continue
rows.append({"arm": arm, "checkpoint": ck, "set": sname, "n_lines": s["n"], "cer": s["cer"], "wer": s["wer"],
"greedy_cer_same_lines": s.get("greedy_cer_same_lines"), "samples_seen": m["samples_seen"],
"train_wall_seconds": m["wall_time_train_seconds"], "train_peak_gpu_gib": tr.get("peak_gpu_gib_training"),
"eval_lines_per_s": s.get("lines_per_second_eval"), "decoding": m["decoding"], "gpu": m["gpu"]})
timing = {}
for f in sorted(glob.glob(os.path.join(LD_OUT, "timing", "*.json"))):
d = load_json(f)
timing[os.path.basename(f)[:-5]] = d
save_json(os.path.join(a.out, "results.json"), {"created": utcnow(), "source": "PRED_DIR meta.json files, LD_OUT/train_logs, LD_OUT/timing",
"rows": rows, "timing": timing})
print(len(rows), "result rows;", len(timing), "timing files")
try: # clarification H9: gated LM predictions (file work only)
from .lm_gate import gate_all
print("lm_gate:", gate_all())
except Exception as e:
print("lm_gate hook failed:", repr(e)[:200])
if __name__ == "__main__":
main()