Datasets:
Upload scripts/train_job.py with huggingface_hub
Browse files- scripts/train_job.py +64 -0
scripts/train_job.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, sys, json, glob
|
| 2 |
+
|
| 3 |
+
EPOCHS = int(os.getenv('EPOCHS', '150'))
|
| 4 |
+
BATCH = int(os.getenv('BATCH', '64'))
|
| 5 |
+
IMGSZ = int(os.getenv('IMGSZ', '320'))
|
| 6 |
+
PUSH = os.getenv('PUSH', '0') == '1'
|
| 7 |
+
DATA = os.getenv('DATA_DIR', '/data/ds')
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
print('torch', torch.__version__, 'cuda', torch.cuda.is_available(), flush=True)
|
| 11 |
+
assert torch.cuda.is_available(), 'no CUDA device'
|
| 12 |
+
|
| 13 |
+
n_tr = len(glob.glob(DATA + '/images/train/*'))
|
| 14 |
+
n_vl = len(glob.glob(DATA + '/images/val/*'))
|
| 15 |
+
print('train', n_tr, 'val', n_vl, flush=True)
|
| 16 |
+
assert n_tr > 1000 and n_vl > 50, 'dataset incomplete'
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
import trackio
|
| 20 |
+
trackio.init(project='microduck-detector')
|
| 21 |
+
print('trackio ok', flush=True)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
trackio = None
|
| 24 |
+
print('trackio failed (ignored):', e, flush=True)
|
| 25 |
+
|
| 26 |
+
from ultralytics import YOLO
|
| 27 |
+
model = YOLO('yolo11n.pt')
|
| 28 |
+
n_params = sum(p.numel() for p in model.model.parameters())
|
| 29 |
+
print('param count:', n_params, flush=True)
|
| 30 |
+
assert n_params < 5_000_000, 'model exceeds 5M params'
|
| 31 |
+
|
| 32 |
+
results = model.train(
|
| 33 |
+
data=DATA + '/data.yaml',
|
| 34 |
+
imgsz=IMGSZ, epochs=EPOCHS, batch=BATCH, device=0,
|
| 35 |
+
project='/data/runs', name='microduck', exist_ok=True,
|
| 36 |
+
workers=4, patience=50,
|
| 37 |
+
)
|
| 38 |
+
print('TRAIN_DONE', flush=True)
|
| 39 |
+
|
| 40 |
+
best = '/data/runs/microduck/weights/best.pt'
|
| 41 |
+
assert os.path.exists(best), 'best.pt missing'
|
| 42 |
+
|
| 43 |
+
metrics = model.val(data=DATA + '/data.yaml', imgsz=IMGSZ, device=0)
|
| 44 |
+
print('VAL_MAP50', metrics.box.map50, 'MAP', metrics.box.map, flush=True)
|
| 45 |
+
|
| 46 |
+
if trackio is not None:
|
| 47 |
+
try:
|
| 48 |
+
trackio.log({'val/mAP50': float(metrics.box.map50), 'val/mAP': float(metrics.box.map), 'params': sum(p.numel() for p in model.model.parameters())})
|
| 49 |
+
trackio.finish()
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print('trackio log failed (ignored):', e, flush=True)
|
| 52 |
+
|
| 53 |
+
if PUSH:
|
| 54 |
+
from huggingface_hub import HfApi
|
| 55 |
+
api = HfApi()
|
| 56 |
+
api.create_repo('pngwn/microduck-detector', repo_type='model', exist_ok=True)
|
| 57 |
+
api.upload_file(path_or_fileobj=best, path_in_repo='microduck_yolo11n.pt', repo_id='pngwn/microduck-detector', repo_type='model')
|
| 58 |
+
print('PUSHED model', flush=True)
|
| 59 |
+
|
| 60 |
+
# qualitative predictions on real val photos (also useful after smoke)
|
| 61 |
+
real_imgs = sorted(glob.glob(DATA + '/images/val/real_*.jpg'))
|
| 62 |
+
print('real val images:', len(real_imgs), flush=True)
|
| 63 |
+
model.predict(real_imgs, imgsz=IMGSZ, conf=0.35, save=True, project='/data/pred', name='real', exist_ok=True)
|
| 64 |
+
print('PRED_DONE', flush=True)
|