File size: 3,967 Bytes
d70361b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""

Resume Day 2-3 tasks SEQUENTIALLY (one heavy job at a time).



Previous crashes were likely caused by running 4 AdaptFormer/TensorFlow jobs in

parallel while also writing thousands of mask PNGs to disk.



Usage:

    python scripts/resume_day3_tasks.py

    python scripts/resume_day3_tasks.py --from grid

"""

from __future__ import annotations



import argparse

import json

import subprocess

import sys

from pathlib import Path



ROOT = Path(__file__).resolve().parent.parent

PY = sys.executable





def run_step(name: str, cmd: list[str]) -> None:

    print(f"\n{'=' * 60}\nSTEP: {name}\n{'=' * 60}")

    subprocess.run(cmd, check=True, cwd=ROOT)

    print(f"STEP DONE: {name}")





def main():

    parser = argparse.ArgumentParser(description="Resume Day 3 tasks sequentially")

    parser.add_argument("--from", dest="from_step",

                        choices=["baseline", "grid", "calibration", "finetune"],

                        default="baseline")

    parser.add_argument("--finetune-epochs", type=int, default=12)

    parser.add_argument("--force", action="store_true",

                        help="re-run steps even if output artifacts already exist")

    args = parser.parse_args()



    baseline_out = ROOT / "runs/delhi_baseline/metrics.json"

    grid_out = ROOT / "runs/calibration/best_params.json"

    calib_out = ROOT / "runs/calibration/leaderboard.json"

    finetune_glob = ROOT / "runs/finetune_adaptformer"



    steps: list[tuple[str, list[str], Path | None]] = [

        ("baseline", [PY, "scripts/record_delhi_baseline.py"], baseline_out),

        ("grid", [

            PY, "scripts/grid_search_calibration.py",

            "--manifest", "docs/delhi_eval/manifest.json",

            "--methods", "Feature-Based",

            "--sensitivities", "0.2,0.3,0.4,0.5,0.6,0.7,0.8",

            "--fusions", "smart_union,hysteresis",

            "--out", "runs/calibration/leaderboard.csv",

        ], grid_out),

        ("calibration", [

            PY, "scripts/delhi_calibration_sweep.py",

            "--manifest", "docs/delhi_eval/manifest.json",

            "--out", "runs/calibration",

            "--methods", "Feature-Based",

            "--quick",

        ], calib_out),

        ("finetune", [

            PY, "scripts/finetune_adaptformer.py",

            "--manifest", "docs/delhi_eval/manifest.json",

            "--epochs", str(args.finetune_epochs),

            "--batch-size", "2",

        ], None),

    ]



    start = False

    for name, cmd, artifact in steps:

        if name == args.from_step:

            start = True

        if not start:

            continue

        if artifact and artifact.is_file() and not args.force:

            print(f"\nSKIP {name}: {artifact} already exists (use --force to re-run)")

            continue

        if name == "finetune" and not args.force:

            existing = sorted(finetune_glob.glob("*/metrics.json"))

            if existing:

                print(f"\nSKIP finetune: {existing[-1]} already exists (use --force to re-run)")

                continue

        run_step(name, cmd)



    summary = {}

    for path, key in [

        (baseline_out, "baseline"),

        (grid_out, "calibration_best"),

        (calib_out, "calibration_leaderboard"),

        (ROOT / "runs/calibration/grid_search/manifest_report.json", "grid_search_manifest"),

    ]:

        if path.is_file():

            summary[key] = json.loads(path.read_text(encoding="utf-8"))

    finetune_runs = sorted(finetune_glob.glob("*/metrics.json"))

    if finetune_runs:

        summary["finetune"] = json.loads(finetune_runs[-1].read_text(encoding="utf-8"))



    out = ROOT / "runs/day3_completion_summary.json"

    out.parent.mkdir(parents=True, exist_ok=True)

    out.write_text(json.dumps(summary, indent=2), encoding="utf-8")

    print(f"\nAll steps finished. Summary: {out}")





if __name__ == "__main__":

    main()