turkic-ocr-code / scripts /r5_ref /make_gpu_tasks.py
treoa's picture
Release 1
24aaa76
Raw History Blame
8.87 kB
"""R5 REF: writes gpu_tasks.json, the task file that LINEDET's driver (job 51128) reads from DATA/outputs/r5/ref/gpu_tasks.json.
Layout: the one documented in records/R5/LINEDET/code/src/linedet/driver.py (name, cmd, venv with {VENV}, requires, after, env, max_runtime_s, reserve_s, cwd).
Extra keys (gpu_mem_gb, host_ram_gb, cpus, stopping_rule, resume, outputs, what) are for the readers; the driver ignores them.
Six reference runs: R1 (HTR-Flor) and R2 (PaddleOCR PP-OCRv3 recogniser) each trained (i) on the KOHTD train forms, (ii) on the replication split,
(iii) on mix_v1 to P_short = 150,016 samples (clarification H5).
Usage: python make_gpu_tasks.py <out.json>
"""
import json
import sys
C = "/path/to/TURKIC_OCR_CODE"
D = "/path/to/TURKIC_OCR_DATA"
S = C + "/scripts/r5_ref"
REFDATA = D + "/data/processed/r5/ref"
MAN = D + "/data/processed/r5/manifests"
CK = D + "/checkpoints/r5/ref"
OUT = D + "/outputs/r5/ref"
PRED = D + "/outputs/r5/preds"
TF, PD = C + "/.venv_ref_tf", C + "/.venv_ref_paddle"
TRAIN_BUDGET_S = 16800 # (i), (ii): 4 h 40 min of training per run; predictions follow (R1 <= 55 min, R2 <= 30 min)
TRAIN_KILL_S = 17700 # driver SIGTERM; the scripts stop themselves at the budget
MIX_BUDGET_S = 7200 # (iii): fixed 150,016 samples; 2 h is a ceiling, the run is expected to need well under 1 h
MIX_KILL_S = 8400
ENV_TF = {"TF_FORCE_GPU_ALLOW_GROWTH": "true", "TF_CPP_MIN_LOG_LEVEL": "2", "OMP_NUM_THREADS": "2", "MKL_NUM_THREADS": "2"}
ENV_PD = {"FLAGS_allocator_strategy": "auto_growth", "OMP_NUM_THREADS": "2", "MKL_NUM_THREADS": "2", "OPENBLAS_NUM_THREADS": "2"}
VAL_SETS = "V1=%s/kohtd_screen_v1.tsv,kohtd_val=%s/kohtd_val.tsv,kohtd_rand_val=%s/kohtd_rand_val.tsv" % (MAN, MAN, MAN)
VAL_SETS_MIX = "V1=%s/kohtd_screen_v1.tsv,kohtd_val=%s/kohtd_val.tsv" % (MAN, MAN)
CURVE = MAN + "/curve_v1.tsv"
CKNAME = {"i": "ours", "ii": "rand", "iii": "mix"}
QUARTER = 37504
tasks = []
tasks.append({"name": "ref_warm", "venv": TF, "cmd": ["dd", "if=" + REFDATA + "/flor_pool_u8.npy", "of=/dev/null", "bs=16M"], "requires": [REFDATA + "/flor_pool_u8.npy"], "max_runtime_s": 1200,
"what": "reads the 15.9 GB preprocessed image array once so that both R1 runs (i) and (ii) share it in the page cache", "host_ram_gb": 0.1, "gpu_mem_gb": 0, "cpus": 1})
pred_name_map = {}
def train(arm, run):
name = "ref_%s_%s_train" % (arm, run)
out = "%s/%s_%s" % (CK, arm, run)
mix = run == "iii"
budget, kill = (MIX_BUDGET_S, MIX_KILL_S) if mix else (TRAIN_BUDGET_S, TRAIN_KILL_S)
req = [REFDATA + "/build_report.json", REFDATA + "/charset_%s.json" % run]
if mix:
req = [REFDATA + "/mix_iii_stats.json", REFDATA + "/charset_iii.json", REFDATA + ("/mix_iii_R1.tsv" if arm == "R1" else "/ppocr_iii_train.txt")]
what_data = {"i": "the KOHTD train forms of the project split", "ii": "the crop-level random 70% of the train+validation pool", "iii": "mix_v1 (first 150,016 samples of the seeded mix stream the recipe can encode)"}[run]
if arm == "R1":
cmd = ["{VENV}/bin/python", "-I", S + "/ref_flor_train.py", "--run", run, "--out", out, "--budget_s", "{REMAIN}", "--budget_cap_s", str(budget), "--resume", "--workers", "3" if mix else "2", "--gpu_mem_mb", "14000"]
rule = ("fixed budget 150,016 samples (4 x 1,172 batches of 32), RMSprop 0.001, batch 32; weights saved at 25/50/75/100 %." if mix else
"EarlyStopping patience 20 on validation loss; ReduceLROnPlateau x0.2 after 10 epochs; RMSprop 0.001; batch 32 (KOHTD paper 5.2, 5.4). Stops earlier at the %d s budget and is then reported with the samples it saw." % budget)
t = {"name": name, "venv": TF, "cmd": cmd, "env": ENV_TF, "after": ["ref_warm"], "gpu_mem_gb": 14, "host_ram_gb": 8, "cpus": 5 if mix else 5,
"stopping_rule": rule, "what": "HTR-Flor, pinned code arthurflor23/handwritten-text-recognition@1ca49af, trained on " + what_data}
else:
cmd = ["{VENV}/bin/python", "-I", S + "/ref_ppocr_run.py", "--run", run, "--out", out, "--budget_s", "{REMAIN}", "--budget_cap_s", str(budget), "--resume", "--workers", "3"]
rule = ("fixed budget 150,016 samples = 4 epochs of 293 iterations of 128 (chunks of the mix list), cosine schedule, lr 1e-4, warm-up 1 % of the run; the epoch-4 weights are used." if mix else
"100 epochs of cosine schedule (Bimurat 2024 2.4); if 100 epochs do not fit in the %d s budget, the number of epochs is cut so that the schedule completes (measured speed), and the run is flagged 'compressed_schedule'." % budget)
t = {"name": name, "venv": PD, "cmd": cmd, "env": ENV_PD, "after": ["ref_warm"], "gpu_mem_gb": 8, "host_ram_gb": 9, "cpus": 5, "stopping_rule": rule,
"what": "PaddleOCR PP-OCRv3 recogniser (Bimurat 2024 'SVTR-Tiny'), pinned code PaddleOCR@e218c0a, trained on " + what_data}
# {REMAIN} = seconds from the task start to (driver stop - reserve_s); the stop is job start + 22,200 s. reserve_s leaves the time of the prediction tasks that follow.
reserve = 600 if mix else (4800 if arm == "R1" else 3300)
t.update({"requires": req, "max_runtime_s": kill, "reserve_s": reserve, "skip_if_missing_after_done": True,
"resume": "automatic: the command always carries --resume; state is saved at every epoch end and on SIGTERM", "outputs": out})
tasks.append(t)
return name, out
def pred(arm, run, train_name, out, sets, suffix, requires_extra=None, kill=3300, extra_args=None, ck=None, no_beam=False):
name = "ref_%s_%s_pred%s" % (arm, run, suffix)
script = "/ref_flor_predict.py" if arm == "R1" else "/ref_ppocr_predict.py"
cmd = ["{VENV}/bin/python", "-I", S + script, "--run", run, "--sets", sets, "--train_dir", out] + (extra_args or []) + (["--no_beam"] if (no_beam and arm == "R1") else [])
t = {"name": name, "venv": TF if arm == "R1" else PD, "cmd": cmd, "env": ENV_TF if arm == "R1" else ENV_PD, "after": [train_name] + ([] if not suffix else ["ref_%s_%s_pred" % (arm, run)]),
"requires": [out + "/train_summary.json"] + (requires_extra or []), "max_runtime_s": kill, "skip_if_missing_after_done": True, "gpu_mem_gb": 10 if arm == "R1" else 4, "host_ram_gb": 6, "cpus": 4,
"what": "validation-set predictions of the %s checkpoint, shared format, DATA/outputs/r5/preds/%s/%s/" % (arm, arm, ck or CKNAME[run])}
tasks.append(t)
return name
def metrics(arm, run, ck):
folders = [ck] + ([ck + "_paperdec"] if arm == "R1" else [])
for fo in folders:
pth = "%s/%s/%s" % (PRED, arm, fo)
tasks.append({"name": "ref_%s_%s_metrics_%s" % (arm, run, fo), "venv": TF, "after": [pred_name_map[(arm, run)]],
"cmd": ["{VENV}/bin/python", "-I", S + "/ref_paper_metrics.py", pth + "/kohtd_rand_val.tsv", "%s/metrics/%s_%s_kohtd_rand_val.json" % (OUT, arm, fo)],
"requires": [pth + "/kohtd_rand_val.tsv"], "max_runtime_s": 900, "skip_if_missing_after_done": True, "gpu_mem_gb": 0, "host_ram_gb": 1, "cpus": 1,
"what": "the published papers' metrics on the validation part of the replication split (arm %s, checkpoint folder %s)" % (arm, fo)})
for arm in ("R1", "R2"):
for run in ("i", "ii", "iii"):
tn, out = train(arm, run)
if run != "iii":
pn = pred(arm, run, tn, out, VAL_SETS, "", kill=3300 if arm == "R1" else 1800)
pred_name_map[(arm, run)] = pn
pred(arm, run, tn, out, "curve_v1=" + CURVE, "_curve", requires_extra=[CURVE], kill=1500)
metrics(arm, run, CKNAME[run])
else:
pn = pred(arm, run, tn, out, VAL_SETS_MIX, "", kill=2400 if arm == "R1" else 1500)
pred_name_map[(arm, run)] = pn
# curve_v1 at the 100 % point (checkpoint folder "mix") and at 25, 50, 75 % (folders mix_curve_<samples>)
pred(arm, run, tn, out, "curve_v1=" + CURVE, "_curve", requires_extra=[CURVE], kill=1500)
for q in (1, 2, 3):
s = q * QUARTER
extra = (["--weights_file", "%s/curve_%d.hdf5" % (out, s), "--ck_name", "mix_curve_%d" % s] if arm == "R1" else ["--weights", "%s/model/iter_epoch_%d" % (out, q), "--ck_name", "mix_curve_%d" % s])
pred(arm, run, tn, out, "curve_v1=" + CURVE, "_curve_%d" % s, requires_extra=[CURVE], kill=1500, extra_args=extra, ck="mix_curve_%d" % s, no_beam=True)
spec = {"description": "Reference arms R1 (HTR-Flor) and R2 (PaddleOCR PP-OCRv3 recogniser of Bimurat 2024), each trained three times: (i) KOHTD train forms of the project split, (ii) replication split, (iii) mix_v1 to 150,016 samples. Written by stage REF; included by the driver of job 51128.",
"written_for_job": 51128, "train_budget_s": TRAIN_BUDGET_S, "tasks": tasks}
json.dump(spec, open(sys.argv[1], "w"), indent=1)
print(len(tasks), "tasks")