Download train_yolo.py from ThreeSixNine/poker-table-perception-eval: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/datasets/ThreeSixNine/poker-table-perception-eval/resolve/main/train_yolo.py
- Command line
-
hf download hf://datasets/ThreeSixNine/poker-table-perception-eval/train_yolo.py
-
curl -L -o train_yolo.py https://huggingface.co/datasets/ThreeSixNine/poker-table-perception-eval/resolve/main/train_yolo.py
4.09 kB
| """Fine-tune YOLO11n card detector on the synthetic poker-table benchmark. | |
| Used for both the smoke (few epochs, fraction of train) and the real run. | |
| After a real run, uploads weights to the Hub model repo. | |
| """ | |
| import argparse | |
| import os | |
| parse = argparse.ArgumentParser() | |
| parse.add_argument("--data", default="/data/benchmark/data.yaml") | |
| parse.add_argument("--weights", default="stock-best.pt") | |
| parse.add_argument("--epochs", type=int, default=80) | |
| parse.add_argument("--patience", type=int, default=15) | |
| parse.add_argument("--batch", type=int, default=16) | |
| parse.add_argument("--imgsz", type=int, default=1280) | |
| parse.add_argument("--fraction", type=float, default=1.0, help="fraction of train set") | |
| parse.add_argument("--device", default=0, help="0 for GPU, cpu for CPU smoke") | |
| parse.add_argument("--project", default="/data/runs") | |
| parse.add_argument("--name", default="poker_cards_ft") | |
| parse.add_argument("--upload-model-repo", default=None, | |
| help="e.g. ThreeSixNine/yolo11n-poker-table-cards; skips upload if empty") | |
| parse.add_argument("--trackio-space-id", default=None, | |
| help="trackio space id; enables per-epoch metric logging") | |
| parse.add_argument("--trackio-project", default="yolo-poker-cards-ft") | |
| parse.add_argument("--upload-tag", default="", | |
| help="suffix for uploaded weight names, e.g. 'mixed' -> " | |
| "best_mixed.pt (avoids overwriting prior runs)") | |
| args = parse.parse_args() | |
| if args.trackio_space_id: | |
| import trackio | |
| trackio.init(project=args.trackio_project, space_id=args.trackio_space_id) | |
| DOWNLOAD = ("shrimantasatpati/yolov11_playing_cards_detection", | |
| "detect/yolo11n_playing_cards/weights/best.pt") | |
| if not os.path.exists(args.weights): # stock weights, idempotent download | |
| from huggingface_hub import hf_hub_download | |
| args.weights = hf_hub_download(*DOWNLOAD) | |
| print(f"weights: {args.weights}", flush=True) | |
| from ultralytics import YOLO | |
| model = YOLO(args.weights) | |
| if args.trackio_space_id: | |
| import trackio | |
| def _log_epoch(trainer): | |
| m = getattr(trainer, "metrics", None) or {} | |
| row = {"epoch": trainer.epoch, | |
| "loss": float(trainer.loss) if trainer.loss is not None else None} | |
| for k, v in (m.items() if isinstance(m, dict) else []): | |
| try: | |
| row[k] = round(float(v), 4) | |
| except (TypeError, ValueError): | |
| pass | |
| trackio.log(row, step=trainer.epoch) | |
| model.add_callback("on_fit_epoch_end", _log_epoch) | |
| model.train(data=args.data, epochs=args.epochs, patience=args.patience, | |
| batch=args.batch, imgsz=args.imgsz, fraction=args.fraction, | |
| device=args.device, project=args.project, name=args.name, | |
| exist_ok=True) | |
| metrics = model.val(data=args.data, imgsz=args.imgsz, split="val") | |
| if args.trackio_space_id: | |
| try: | |
| d = metrics.results_dict | |
| trackio.log({f"final/{k}": round(float(v), 4) for k, v in d.items()}, | |
| step=args.epochs) | |
| trackio.finish() | |
| except Exception as e: | |
| print("trackio final log failed:", e, flush=True) | |
| results_dir = os.path.join(args.project, args.name) | |
| print("== TRAIN DONE ==", flush=True) | |
| print("results_dir:", results_dir, flush=True) | |
| try: | |
| d = metrics.results_dict | |
| print("val metrics:", {k: round(float(v), 4) for k, v in d.items()}, flush=True) | |
| except Exception as e: | |
| print("metrics read failed:", e, flush=True) | |
| if args.upload_model_repo: | |
| from huggingface_hub import HfApi | |
| api = HfApi() | |
| tag = args.upload_tag | |
| api.upload_file( | |
| path_or_fileobj=os.path.join(results_dir, "weights", "best.pt"), | |
| path_in_repo=f"best{'_' + tag if tag else ''}.pt", | |
| repo_id=args.upload_model_repo, | |
| repo_type="model", | |
| ) | |
| api.upload_file( | |
| path_or_fileobj=os.path.join(results_dir, "weights", "last.pt"), | |
| path_in_repo=f"last{'_' + tag if tag else ''}.pt", | |
| repo_id=args.upload_model_repo, | |
| repo_type="model", | |
| ) | |
| print(f"uploaded weights to {args.upload_model_repo}", flush=True) | |