mirror of
https://github.com/ultralytics/ultralytics
synced 2026-04-21 14:07:18 +00:00
Some checks are pending
CI / Benchmarks (yolo26n, macos-26, 3.12) (push) Waiting to run
CI / Benchmarks (yolo26n, ubuntu-24.04-arm, 3.12) (push) Waiting to run
CI / Benchmarks (yolo26n, ubuntu-latest, 3.12) (push) Waiting to run
CI / Tests (macos-26, 3.12, latest) (push) Waiting to run
CI / Tests (ubuntu-24.04-arm, 3.12, latest) (push) Waiting to run
CI / Tests (ubuntu-latest, 3.12, latest) (push) Waiting to run
CI / Tests (ubuntu-latest, 3.8, 1.8.0, 0.9.0) (push) Waiting to run
CI / Tests (windows-latest, 3.12, latest) (push) Waiting to run
CI / SlowTests (macos-26, 3.12, latest) (push) Waiting to run
CI / SlowTests (ubuntu-24.04-arm, 3.12, latest) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.10, 1.11.0, 0.12.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.10, 1.12.0, 0.13.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.10, 1.13.0, 0.14.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.11, 2.0.0, 0.15.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.11, 2.1.0, 0.16.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.10.0, 0.25.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.11.0, 0.26.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.2.0, 0.17.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.3.0, 0.18.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.4.0, 0.19.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.5.0, 0.20.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.6.0, 0.21.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.7.0, 0.22.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.8.0, 0.23.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, 2.9.0, 0.24.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.12, latest) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.8, 1.8.0, 0.9.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.9, 1.10.0, 0.11.0) (push) Waiting to run
CI / SlowTests (ubuntu-latest, 3.9, 1.9.0, 0.10.0) (push) Waiting to run
CI / SlowTests (windows-latest, 3.12, latest) (push) Waiting to run
CI / GPU (push) Waiting to run
CI / RaspberryPi (push) Waiting to run
CI / NVIDIA_Jetson (JetPack5.1.2, 1.23.5, https://github.com/ultralytics/assets/releases/download/v0.0.0/onnxruntime_gpu-1.16.3-cp38-cp38-linux_aarch64.whl, 3.8, jetson-jp512, https://github.com/ultralytics/assets/releases/download/v0.0.0/torch-2.2.0-cp38-c… (push) Waiting to run
CI / NVIDIA_Jetson (JetPack6.2, 1.26.4, https://github.com/ultralytics/assets/releases/download/v0.0.0/onnxruntime_gpu-1.20.0-cp310-cp310-linux_aarch64.whl, 3.10, jetson-jp62, https://github.com/ultralytics/assets/releases/download/v0.0.0/torch-2.5.0a0+872d… (push) Waiting to run
CI / Conda (ubuntu-latest, 3.12) (push) Waiting to run
CI / Summary (push) Blocked by required conditions
Publish Docker Images / Build (push) Waiting to run
Publish Docker Images / trigger-actions (push) Blocked by required conditions
Publish Docker Images / notify (push) Blocked by required conditions
Publish Docs / Docs (push) Waiting to run
Publish to PyPI / check (push) Waiting to run
Publish to PyPI / build (push) Blocked by required conditions
Publish to PyPI / publish (push) Blocked by required conditions
Publish to PyPI / sbom (push) Blocked by required conditions
Publish to PyPI / notify (push) Blocked by required conditions
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
203 lines
7.2 KiB
Python
203 lines
7.2 KiB
Python
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
|
|
|
import sys
|
|
from types import SimpleNamespace
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from tests import MODEL, SOURCE, TASK_MODEL_DATA
|
|
from ultralytics import YOLO
|
|
from ultralytics.cfg import get_cfg
|
|
from ultralytics.engine.exporter import Exporter
|
|
from ultralytics.models.yolo import classify, detect, obb, pose, segment
|
|
from ultralytics.nn.tasks import load_checkpoint
|
|
from ultralytics.utils import ASSETS, DEFAULT_CFG, WEIGHTS_DIR
|
|
|
|
|
|
def test_func(*args, **kwargs):
|
|
"""Test function used as a callback stub to verify callback registration."""
|
|
print("callback test passed")
|
|
|
|
|
|
def test_export():
|
|
"""Test model exporting functionality by adding a callback and verifying its execution."""
|
|
exporter = Exporter()
|
|
exporter.add_callback("on_export_start", test_func)
|
|
assert test_func in exporter.callbacks["on_export_start"], "on_export_start callback not registered"
|
|
f = exporter(model=YOLO("yolo26n.yaml").model)
|
|
YOLO(f)(SOURCE) # exported model inference
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"trainer_cls,validator_cls,predictor_cls,data,model,weights",
|
|
[
|
|
(
|
|
detect.DetectionTrainer,
|
|
detect.DetectionValidator,
|
|
detect.DetectionPredictor,
|
|
"coco8.yaml",
|
|
"yolo26n.yaml",
|
|
MODEL,
|
|
),
|
|
(
|
|
segment.SegmentationTrainer,
|
|
segment.SegmentationValidator,
|
|
segment.SegmentationPredictor,
|
|
"coco8-seg.yaml",
|
|
"yolo26n-seg.yaml",
|
|
WEIGHTS_DIR / "yolo26n-seg.pt",
|
|
),
|
|
(
|
|
classify.ClassificationTrainer,
|
|
classify.ClassificationValidator,
|
|
classify.ClassificationPredictor,
|
|
"imagenet10",
|
|
"yolo26n-cls.yaml",
|
|
None,
|
|
),
|
|
(obb.OBBTrainer, obb.OBBValidator, obb.OBBPredictor, "dota8.yaml", "yolo26n-obb.yaml", None),
|
|
(pose.PoseTrainer, pose.PoseValidator, pose.PosePredictor, "coco8-pose.yaml", "yolo26n-pose.yaml", None),
|
|
],
|
|
)
|
|
def test_task(trainer_cls, validator_cls, predictor_cls, data, model, weights):
|
|
"""Test YOLO training, validation, and prediction for various tasks."""
|
|
overrides = {
|
|
"data": data,
|
|
"model": model,
|
|
"imgsz": 32,
|
|
"epochs": 1,
|
|
"save": False,
|
|
"mask_ratio": 1,
|
|
"overlap_mask": False,
|
|
}
|
|
|
|
# Trainer
|
|
trainer = trainer_cls(overrides=overrides)
|
|
trainer.add_callback("on_train_start", test_func)
|
|
assert test_func in trainer.callbacks["on_train_start"], "on_train_start callback not registered"
|
|
trainer.train()
|
|
|
|
# Validator
|
|
cfg = get_cfg(DEFAULT_CFG)
|
|
cfg.data = data
|
|
cfg.imgsz = 32
|
|
val = validator_cls(args=cfg)
|
|
val.add_callback("on_val_start", test_func)
|
|
assert test_func in val.callbacks["on_val_start"], "on_val_start callback not registered"
|
|
val(model=trainer.best)
|
|
|
|
# Predictor
|
|
pred = predictor_cls(overrides={"imgsz": [64, 64]})
|
|
pred.add_callback("on_predict_start", test_func)
|
|
assert test_func in pred.callbacks["on_predict_start"], "on_predict_start callback not registered"
|
|
|
|
# Determine model path for prediction
|
|
model_path = weights if weights else trainer.best
|
|
if model == "yolo26n.yaml": # only for detection
|
|
# Confirm there is no issue with sys.argv being empty
|
|
with mock.patch.object(sys, "argv", []):
|
|
result = pred(source=ASSETS, model=model_path)
|
|
assert len(result) > 0, f"Predictor returned no results for {model}"
|
|
else:
|
|
result = pred(source=ASSETS, model=model_path)
|
|
assert len(result) > 0, f"Predictor returned no results for {model}"
|
|
|
|
# Test resume functionality
|
|
with pytest.raises(AssertionError):
|
|
trainer_cls(overrides={**overrides, "resume": trainer.last}).train()
|
|
|
|
|
|
@pytest.mark.parametrize("task,weight,data", TASK_MODEL_DATA)
|
|
def test_resume_incomplete(task, weight, data, tmp_path):
|
|
"""Test training resumes from an incomplete checkpoint."""
|
|
train_args = {
|
|
"data": data,
|
|
"epochs": 2,
|
|
"save": True,
|
|
"plots": False,
|
|
"workers": 0,
|
|
"project": tmp_path,
|
|
"name": task,
|
|
"imgsz": 32,
|
|
"exist_ok": True,
|
|
}
|
|
|
|
def stop_after_first_epoch(trainer):
|
|
if trainer.epoch == 0:
|
|
trainer.stop = True
|
|
|
|
def disable_final_eval(trainer):
|
|
trainer.final_eval = lambda: None
|
|
|
|
model = YOLO(weight)
|
|
model.add_callback("on_train_start", disable_final_eval)
|
|
model.add_callback("on_train_epoch_end", stop_after_first_epoch)
|
|
model.train(**train_args)
|
|
last_path = model.trainer.last
|
|
_, ckpt = load_checkpoint(last_path)
|
|
assert ckpt["epoch"] == 0, "checkpoint should be resumable"
|
|
|
|
# Resume training using the checkpoint
|
|
resume_model = YOLO(last_path)
|
|
resume_model.train(resume=True, **train_args)
|
|
assert resume_model.trainer.start_epoch == resume_model.trainer.epoch == 1, "resume test failed"
|
|
|
|
|
|
def test_nan_recovery():
|
|
"""Test NaN loss detection and recovery during training."""
|
|
nan_injected = [False]
|
|
|
|
def inject_nan(trainer):
|
|
"""Inject NaN into loss during batch processing to test recovery mechanism."""
|
|
if trainer.epoch == 1 and trainer.tloss is not None and not nan_injected[0]:
|
|
trainer.tloss *= torch.tensor(float("nan"))
|
|
nan_injected[0] = True
|
|
|
|
overrides = {"data": "coco8.yaml", "model": "yolo26n.yaml", "imgsz": 32, "epochs": 3}
|
|
trainer = detect.DetectionTrainer(overrides=overrides)
|
|
trainer.add_callback("on_train_batch_end", inject_nan)
|
|
trainer.train()
|
|
assert nan_injected[0], "NaN injection failed"
|
|
|
|
|
|
def test_train_reuses_loaded_checkpoint_model(monkeypatch):
|
|
"""Test training reuses an already-loaded checkpoint model instead of re-parsing the model source."""
|
|
model = YOLO("yolo26n.yaml")
|
|
model.ckpt = {"checkpoint": True}
|
|
model.ckpt_path = "/tmp/fake.pt"
|
|
model.overrides["model"] = "ul://glenn-jocher/m2/exp-14"
|
|
original_model = model.model
|
|
captured = {}
|
|
|
|
class FakeTrainer:
|
|
def __init__(self, overrides=None, _callbacks=None):
|
|
self.overrides = overrides
|
|
self.callbacks = _callbacks
|
|
self.model = None
|
|
self.validator = SimpleNamespace(metrics=None)
|
|
self.best = MODEL.parent / "nonexistent-best.pt"
|
|
self.last = MODEL
|
|
captured["trainer"] = self
|
|
|
|
def get_model(self, cfg=None, weights=None, verbose=True):
|
|
captured["cfg"] = cfg
|
|
captured["weights"] = weights
|
|
return original_model
|
|
|
|
def train(self):
|
|
return None
|
|
|
|
monkeypatch.setattr("ultralytics.engine.model.checks.check_pip_update_available", lambda: None)
|
|
monkeypatch.setattr(model, "_smart_load", lambda key: FakeTrainer)
|
|
monkeypatch.setattr(
|
|
"ultralytics.engine.model.load_checkpoint",
|
|
lambda path: (original_model, {"checkpoint": True}),
|
|
)
|
|
|
|
model.train(data="coco8.yaml", epochs=1)
|
|
|
|
assert captured["trainer"].model is original_model, "Trainer model does not match original"
|
|
assert captured["cfg"] == original_model.yaml, f"Config mismatch: {captured['cfg']} != {original_model.yaml}"
|
|
assert captured["weights"] is original_model, "Weights do not match original model"
|