File size: 4,526 Bytes
0107764
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Recompute the SetCon numbers of the paper from the released weights and compare them with the printed values.

Default: Tab. 1 (both SetCon rows) and the abstract (Mean-13 accuracy 56.6 % with class names, 65.3 % with anchors)
for all three stage-2 seeds. With --all also Tab. 2 (reference at test), Tab. 3 and the numbers of Sec. 4.3-4.4.

  python examples/reproduce_table1.py --unimts-zip UniMTS_data.zip --eval-data <SetCon-eval folder or hub id> \
      --work work/

Steps (each can run alone, e.g. one seed per GPU):
  --step prepare            extract and check the 54 test files, build the model-ready caches (CPU, a few minutes)
  --step evaluate --seeds 2 embeddings and predictions of one seed (GPU, about 5 min for the default readings)
  --step tables             the paper's numbers from the per-seed results

Reference settings: CUDA with bfloat16 autocast and batches of 64, calibrator on CPU in float32, torch 2.8.0+cu128,
numpy 2.0.2. A CPU run or another batch size changes a few predictions and can move a printed digit.
"""
import argparse
import json
import sys
from pathlib import Path

import torch

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from setcon import eval_data_dir, load_model  # noqa: E402
from setcon import data, tables  # noqa: E402
from setcon.evaluate import evaluate_seed  # noqa: E402
from setcon.protocol import DATASETS, EvalData  # noqa: E402

MAIN_READINGS = ("six_random", "six_of_twelve")
ALL_READINGS = ("six_random", "six_of_twelve", "walking_frame", "zeroed", "one_window")


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--work", required=True, help="working folder (raw files, caches, results)")
    ap.add_argument("--eval-data", default=None, help="SetCon-eval folder or hub id (default: the hub dataset)")
    ap.add_argument("--unimts-zip", default=None, help="UniMTS_data.zip (extracted into <work>/UniMTS_data)")
    ap.add_argument("--unimts-dir", default=None, help="already extracted UniMTS_data folder (checked by sha256)")
    ap.add_argument("--model", default=None, help="model folder or hub id (default: this repository)")
    ap.add_argument("--seeds", default="1,2,3")
    ap.add_argument("--step", choices=("all", "prepare", "evaluate", "tables"), default="all")
    ap.add_argument("--all", action="store_true", help="every SetCon number of Tab. 1-3 and Sec. 4.3-4.4")
    ap.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu")
    a = ap.parse_args()
    work = Path(a.work)
    ed_dir = eval_data_dir(a.eval_data)
    seeds = [int(s) for s in a.seeds.split(",")]
    readings = ALL_READINGS if a.all else MAIN_READINGS
    caches = work / "benchmarks"

    if a.step in ("all", "prepare"):
        if a.unimts_zip:
            print("extracting and checking the test files ...", flush=True)
            raw = data.extract_test_files(a.unimts_zip, work / "UniMTS_data", ed_dir)
        elif a.unimts_dir:
            raw = Path(a.unimts_dir)
            data.verify_test_files(raw, ed_dir)
        else:
            raise SystemExit("--unimts-zip or --unimts-dir is required for the prepare step")
        print("building the model-ready caches ...", flush=True)
        data.write_benchmarks(raw, caches, [d for d in DATASETS if not (caches / f"{d}.npz").exists()])

    if a.step in ("all", "evaluate"):
        if a.device != "cuda":
            print("note: no CUDA - the numbers can differ from the paper in the last digit", flush=True)
        ed = EvalData(ed_dir)
        for seed in seeds:
            model = load_model(seed=seed, model=a.model)
            evaluate_seed(model, seed, caches, ed, work / "results", readings=readings, device=a.device)

    if a.step in ("all", "tables"):
        R = tables.Results(work / "results", seeds, ed_dir)
        numbers = tables.main_numbers(R)
        paper = dict(tables.PAPER_MAIN)
        if a.all:
            numbers.update(tables.more_numbers(R))
            paper.update(tables.PAPER_MORE)
        rows = tables.compare(numbers, paper)
        print(tables.format_rows(rows))
        n_ok = sum(r[-1] for r in rows)
        print(f"\n{n_ok} of {len(rows)} numbers equal the printed values (seeds {', '.join(map(str, seeds))}).")
        (work / "results" / "paper_numbers.json").write_text(json.dumps(
            dict(seeds=seeds, numbers=numbers, matches={r[0]: r[-1] for r in rows}), indent=1) + "\n")


if __name__ == "__main__":
    main()