Intro

Weights & Biases sweep to tune YOLOv8 hyperparameters for your RoboSub dataset from Roboflow https://yolov8.com/

Dataset

Colab

https://colab.research.google.com/drive/1tIaba9VI552TZdiINRZ4oIg_3C4eJgfh#scrollTo=iR1CO-wvqBfq

Parameter Tuning

[!info] Weight and Biases Sweep

# --- Install dependencies ---
!pip install ultralytics roboflow wandb --upgrade
 
# --- Imports ---
from ultralytics import YOLO
from roboflow import Roboflow
import wandb
from google.colab import files
 
# --- Initialize Roboflow and Download Dataset ---
rf = Roboflow(api_key="fP0H2pAoE5CYfySw9gCg")
project = rf.workspace("gtmrg").project("robosub-2025-competition-hfx2t")
version = project.version(2)
dataset = version.download("yolov8")  # YOLOv8 format
 
# --- Initialize base YOLOv8 model ---
model = YOLO("yolov8n.pt")  # you can change to yolov8s.pt, yolov8m.pt, etc.
 
# --- Define WandB Sweep Configuration ---
sweep_config = {
    "method": "bayes",  # options: "random", "grid", "bayes"
    "metric": {"name": "metrics/mAP50", "goal": "maximize"},  # optimize for mAP@0.5
    "parameters": {
        "epochs": {"values": [50, 80, 100]},
        "batch_size": {"values": [8, 16, 32]},
        "imgsz": {"values": [416, 640]},
        "lr0": {"min": 1e-5, "max": 1e-2},
        "optimizer": {"values": ["Adam", "AdamW", "SGD"]},
        "momentum": {"min": 0.6, "max": 0.98},
        "weight_decay": {"min": 0.0, "max": 0.001},
        "mosaic": {"min": 0.0, "max": 1.0},
        "mixup": {"min": 0.0, "max": 0.5}
    }
}
 
# --- Sweep Training Function ---
def train_yolo():
    wandb.init(project="robosub_project")  # new run for each sweep config
    config = wandb.config
 
    model.train(
        data=dataset.location + "/data.yaml",
        epochs=config.epochs,
        imgsz=config.imgsz,
        batch=config.batch_size,
        optimizer=config.optimizer,
        lr0=config.lr0,
        momentum=config.momentum,
        weight_decay=config.weight_decay,
        mosaic=config.mosaic,
        mixup=config.mixup,
        warmup_epochs=3,
        warmup_momentum=0.8,
        warmup_bias_lr=0.1,
        augment=True,
        copy_paste=0.5,
        patience=20,
        name=f"yolo_sweep_{wandb.run.name}",
        project="robosub_project"
    )
 
# --- Create and Launch Sweep ---
sweep_id = wandb.sweep(sweep_config, project="robosub_project")
wandb.agent(sweep_id, function=train_yolo, count=10)  # count = number of runs
 
# --- OPTIONAL: Download best weights after sweep ---
files.download("runs/detect/robosub_yolo/weights/best.pt")
 

Learning Rate

Preventing Overshooting and Oscillations: A high learning rate can cause the model’s parameters to update too aggressively, potentially overshooting the optimal solution and leading to oscillations around the minimum of the loss function. This can prevent the model from settling into a good generalization point and instead cause it to fit the noise in the training data, resulting in overfitting. Decreasing the learning rate as training progresses allows for finer adjustments and helps the model converge more smoothly.

Training

  • box_loss should go down
Starting training for 80 epochs...

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
       1/80      2.16G       1.24      1.483      1.309         76        640: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 154/154 [00:54<00:00,  2.83it/s]
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 4/4 [00:01<00:00,  3.43it/s]                   all        100        427      0.649      0.588      0.602      0.438


      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
       2/80      2.64G       1.21      1.416      1.298         68        640: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 154/154 [00:51<00:00,  2.98it/s]
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 4/4 [00:01<00:00,  3.18it/s]                   all        100        427      0.633      0.638      0.648      0.451


      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
       3/80      2.64G      1.216      1.433      1.313         68        640: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 154/154 [00:51<00:00,  2.97it/s]
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 4/4 [00:01<00:00,  3.22it/s]                   all        100        427      0.658      0.683      0.676      0.501


      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
       4/80      2.64G      1.193      1.418       1.29         63        640: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 154/154 [00:50<00:00,  3.07it/s]
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 4/4 [00:02<00:00,  1.93it/s]                   all        100        427      0.666      0.643      0.642      0.46