File size: 2,364 Bytes
6bd4371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'''
Score one or more checkpoints (local dirs or HF repo ids) on Norma.

    python evaluate.py --models Ericu950/oga-macronizer-char runs/final/seed1/best \
        --splits test validation --out results/eval

For each model and split it prints the Table 1 metrics and the per-source
scores, and writes:
- <out>/<name>.<split>.metrics.json
- <out>/<name>.<split>.positions.jsonl: one row per scored position
  (source, line, char, word, gold, pred), for the error analysis.
'''

import argparse
import json
import os
import time

from norma import format_table, load_split, metrics, score
from predict import MacronPredictor


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--models", nargs="+", required=True)
    ap.add_argument("--splits", nargs="+", default=["test"])
    ap.add_argument("--norma_repo", default="Urdatorn/norma")
    ap.add_argument("--norma_revision", default=None)
    ap.add_argument("--out", default=None)
    ap.add_argument("--device", default=None)
    ap.add_argument("--batch_size", type=int, default=256,
                    help="1 = one line at a time without padding, as the original predict.py")
    a = ap.parse_args()
    data = {s: load_split(s, a.norma_repo, a.norma_revision) for s in a.splits}
    all_metrics = {}
    for m in a.models:
        t = time.time()
        pred = MacronPredictor(m, device=a.device)
        name = m.strip("/").replace("/", "__") + ("" if a.batch_size != 1 else ".unbatched")
        for s, recs in data.items():
            outputs = pred.macronize_lines([r.plain for r in recs], batch_size=a.batch_size)
            pos = score(recs, outputs)
            met = metrics(pos)
            all_metrics[(m, s)] = met
            print(f"\n=== {m} on Norma {s} ({len(recs)} lines, {met['n']} positions, "
                  f"{time.time()-t:.1f}s) ===", flush=True)
            print(format_table(met), flush=True)
            if a.out:
                os.makedirs(a.out, exist_ok=True)
                with open(os.path.join(a.out, f"{name}.{s}.metrics.json"), "w") as f:
                    json.dump(met, f, indent=2)
                with open(os.path.join(a.out, f"{name}.{s}.positions.jsonl"), "w", encoding="utf-8") as f:
                    for p in pos:
                        f.write(json.dumps(p, ensure_ascii=False) + "\n")


if __name__ == "__main__":
    main()