ThreeSixNine commited on
Commit
a31e720
·
verified ·
1 Parent(s): 0384d4d

fine-tune script used by the GPU training job

Browse files
Files changed (1) hide show
  1. train_yolo.py +96 -0
train_yolo.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Fine-tune YOLO11n card detector on the synthetic poker-table benchmark.
2
+
3
+ Used for both the smoke (few epochs, fraction of train) and the real run.
4
+ After a real run, uploads weights to the Hub model repo.
5
+ """
6
+ import argparse
7
+ import os
8
+
9
+ parse = argparse.ArgumentParser()
10
+ parse.add_argument("--data", default="/data/benchmark/data.yaml")
11
+ parse.add_argument("--weights", default="stock-best.pt")
12
+ parse.add_argument("--epochs", type=int, default=80)
13
+ parse.add_argument("--patience", type=int, default=15)
14
+ parse.add_argument("--batch", type=int, default=16)
15
+ parse.add_argument("--imgsz", type=int, default=1280)
16
+ parse.add_argument("--fraction", type=float, default=1.0, help="fraction of train set")
17
+ parse.add_argument("--device", default=0, help="0 for GPU, cpu for CPU smoke")
18
+ parse.add_argument("--project", default="/data/runs")
19
+ parse.add_argument("--name", default="poker_cards_ft")
20
+ parse.add_argument("--upload-model-repo", default=None,
21
+ help="e.g. ThreeSixNine/yolo11n-poker-table-cards; skips upload if empty")
22
+ parse.add_argument("--trackio-space-id", default=None,
23
+ help="trackio space id; enables per-epoch metric logging")
24
+ parse.add_argument("--trackio-project", default="yolo-poker-cards-ft")
25
+ args = parse.parse_args()
26
+
27
+ if args.trackio_space_id:
28
+ import trackio
29
+ trackio.init(project=args.trackio_project, space_id=args.trackio_space_id)
30
+
31
+ DOWNLOAD = ("shrimantasatpati/yolov11_playing_cards_detection",
32
+ "detect/yolo11n_playing_cards/weights/best.pt")
33
+
34
+ if not os.path.exists(args.weights): # stock weights, idempotent download
35
+ from huggingface_hub import hf_hub_download
36
+ args.weights = hf_hub_download(*DOWNLOAD)
37
+ print(f"weights: {args.weights}", flush=True)
38
+
39
+ from ultralytics import YOLO
40
+ model = YOLO(args.weights)
41
+
42
+ if args.trackio_space_id:
43
+ import trackio
44
+
45
+ def _log_epoch(trainer):
46
+ m = getattr(trainer, "metrics", None) or {}
47
+ row = {"epoch": trainer.epoch,
48
+ "loss": float(trainer.loss) if trainer.loss is not None else None}
49
+ for k, v in (m.items() if isinstance(m, dict) else []):
50
+ try:
51
+ row[k] = round(float(v), 4)
52
+ except (TypeError, ValueError):
53
+ pass
54
+ trackio.log(row, step=trainer.epoch)
55
+
56
+ model.add_callback("on_fit_epoch_end", _log_epoch)
57
+
58
+ model.train(data=args.data, epochs=args.epochs, patience=args.patience,
59
+ batch=args.batch, imgsz=args.imgsz, fraction=args.fraction,
60
+ device=args.device, project=args.project, name=args.name,
61
+ exist_ok=True)
62
+
63
+ metrics = model.val(data=args.data, imgsz=args.imgsz, split="val")
64
+ if args.trackio_space_id:
65
+ try:
66
+ d = metrics.results_dict
67
+ trackio.log({f"final/{k}": round(float(v), 4) for k, v in d.items()},
68
+ step=args.epochs)
69
+ trackio.finish()
70
+ except Exception as e:
71
+ print("trackio final log failed:", e, flush=True)
72
+ results_dir = os.path.join(args.project, args.name)
73
+ print("== TRAIN DONE ==", flush=True)
74
+ print("results_dir:", results_dir, flush=True)
75
+ try:
76
+ d = metrics.results_dict
77
+ print("val metrics:", {k: round(float(v), 4) for k, v in d.items()}, flush=True)
78
+ except Exception as e:
79
+ print("metrics read failed:", e, flush=True)
80
+
81
+ if args.upload_model_repo:
82
+ from huggingface_hub import HfApi
83
+ api = HfApi()
84
+ api.upload_file(
85
+ path_or_fileobj=os.path.join(results_dir, "weights", "best.pt"),
86
+ path_in_repo="best.pt",
87
+ repo_id=args.upload_model_repo,
88
+ repo_type="model",
89
+ )
90
+ api.upload_file(
91
+ path_or_fileobj=os.path.join(results_dir, "weights", "last.pt"),
92
+ path_in_repo="last.pt",
93
+ repo_id=args.upload_model_repo,
94
+ repo_type="model",
95
+ )
96
+ print(f"uploaded weights to {args.upload_model_repo}", flush=True)