File size: 2,310 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""rc1 prep, detector: what the stage-2 checkpoint best.pt says about itself, and its parameter count.

Run with CODE/.venv_kraken/bin/python -I. Loads the checkpoint with ultralytics (the pickle is our own training output), prints the
metadata keys it carries (epoch, best fitness, version, licence line, whether the training arguments hold cluster paths), counts parameters.
usage: rc1_prep_det.py WEIGHTS REPORT_JSON
"""
import json
import os
import sys
import tempfile

os.environ.setdefault("YOLO_OFFLINE", "1")
os.environ.setdefault("YOLO_CONFIG_DIR", os.path.join(tempfile.gettempdir(), "ultralytics_cfg_rc1_prep"))

import torch  # noqa: E402
import ultralytics  # noqa: E402
from ultralytics import YOLO  # noqa: E402


def main():
    w, rep = sys.argv[1:3]
    m = YOLO(w)
    ck = m.ckpt if isinstance(getattr(m, "ckpt", None), dict) else {}
    out = {"ultralytics": ultralytics.__version__, "torch": torch.__version__, "ckpt_keys": sorted(ck.keys()),
           "task": getattr(m, "task", None), "names": getattr(m.model, "names", None)}
    for k in ("epoch", "best_fitness", "date", "version", "license", "docs"):
        if k in ck:
            out["ckpt_" + k] = ck[k]
    ta = ck.get("train_args") or {}
    out["train_args_keys_n"] = len(ta)
    out["train_args_model"] = str(ta.get("model"))
    out["train_args_data"] = str(ta.get("data"))
    out["train_args_epochs"] = ta.get("epochs")
    out["train_args_time"] = ta.get("time")
    out["train_args_imgsz"] = ta.get("imgsz")
    out["train_args_hold_datahub_paths"] = sorted(k for k, v in ta.items() if isinstance(v, str) and ("/datahub" in v or "/shared/home" in v))
    seen, n = set(), 0
    for p in m.model.parameters():
        if id(p) not in seen:
            seen.add(id(p))
            n += p.numel()
    out["parameters"] = n
    out["dtype_first_param"] = str(next(m.model.parameters()).dtype)
    tm = ck.get("train_metrics") or ck.get("train_results")
    out["train_metrics_in_ckpt"] = tm if isinstance(tm, dict) else None
    with open(rep, "w", encoding="utf-8") as f:
        json.dump(out, f, indent=1, ensure_ascii=False, default=str)
    print(json.dumps({k: out[k] for k in ("parameters", "ckpt_epoch", "ckpt_best_fitness", "train_args_hold_datahub_paths")}, default=str))


if __name__ == "__main__":
    main()