ultralytics 8.4.45 Fix pretrained checkpoint training regression (#24378)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn Jocher 2026-04-29 17:15:40 +02:00 committed by GitHub
parent a8ff07a896
commit 79b2086bfd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 18 deletions

View File

@ -163,13 +163,17 @@ def test_nan_recovery():
assert nan_injected[0], "NaN injection failed"
@pytest.mark.parametrize("pretrained", [True, False, MODEL])
def test_train_reuses_loaded_checkpoint_config(monkeypatch, pretrained):
"""Test training reuses an already-loaded checkpoint config without forcing pretrained weights."""
@pytest.mark.parametrize(
"kwargs,uses_weights",
[({}, True), ({"pretrained": True}, True), ({"pretrained": False}, False), ({"pretrained": MODEL}, True)],
)
def test_train_reuses_loaded_checkpoint_model(monkeypatch, kwargs, uses_weights):
"""Test training reuses loaded checkpoint config while respecting the pretrained argument."""
model = YOLO("yolo26n.yaml")
model.ckpt = {"checkpoint": True}
model.ckpt_path = "/tmp/fake.pt"
model.overrides["model"] = "ul://glenn-jocher/m2/exp-14"
model.overrides["pretrained"] = False
original_model = model.model
captured = {}
@ -198,15 +202,15 @@ def test_train_reuses_loaded_checkpoint_config(monkeypatch, pretrained):
lambda path: (original_model, {"checkpoint": True}),
)
model.train(data="coco8.yaml", epochs=1, pretrained=pretrained)
model.train(data="coco8.yaml", epochs=1, **kwargs)
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 (None if pretrained is False else original_model), "Unexpected weights loaded"
assert captured["weights"] is (original_model if uses_weights else None), "Unexpected weights loaded"
@pytest.mark.parametrize("pretrained", [False, MODEL])
def test_setup_model_respects_pretrained_arg_for_pt_models(monkeypatch, pretrained):
@pytest.mark.parametrize("pretrained,uses_weights", [(True, True), (False, False), (MODEL, True)])
def test_setup_model_respects_pretrained_arg_for_pt_models(monkeypatch, pretrained, uses_weights):
"""Test .pt models use checkpoint config while respecting the pretrained argument."""
captured = {}
checkpoint_model = SimpleNamespace(yaml={"nc": 80})
@ -228,4 +232,4 @@ def test_setup_model_respects_pretrained_arg_for_pt_models(monkeypatch, pretrain
trainer.setup_model()
assert captured["cfg"] == checkpoint_model.yaml, "Checkpoint config was not used"
assert captured["weights"] is (None if pretrained is False else checkpoint_model), "Unexpected weights loaded"
assert captured["weights"] is (checkpoint_model if uses_weights else None), "Unexpected weights loaded"

View File

@ -1,6 +1,6 @@
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
__version__ = "8.4.44"
__version__ = "8.4.45"
import importlib
import os

View File

@ -767,6 +767,7 @@ class Model(torch.nn.Module):
"task": self.task,
} # method defaults
args = {**overrides, **custom, **kwargs, "mode": "train", "session": self.session} # prioritizes rightmost args
pretrained = kwargs.get("pretrained", overrides.get("pretrained", True) if kwargs.get("cfg") else True)
if args.get("resume"):
if args["resume"] is True: # resume=True (boolean) uses current model as checkpoint
if self.ckpt and self.ckpt.get("epoch", -1) >= 0 and self.ckpt.get("optimizer") is not None:
@ -781,10 +782,10 @@ class Model(torch.nn.Module):
self.trainer = (trainer or self._smart_load("trainer"))(overrides=args, _callbacks=self.callbacks)
if not args.get("resume") and self.ckpt:
# Reuse the already-loaded checkpoint config to avoid re-resolving remote sources during trainer setup.
weights = self.model if args.get("pretrained") else None
if isinstance(args.get("pretrained"), (str, Path)):
weights, _ = load_checkpoint(args["pretrained"])
# Reuse the already-loaded checkpoint model to avoid re-resolving remote weight sources during trainer setup.
weights = None if pretrained is False else self.model
if isinstance(pretrained, (str, Path)):
weights, _ = load_checkpoint(pretrained)
self.trainer.model = self.trainer.get_model(weights=weights, cfg=self.model.yaml)
self.model = self.trainer.model

View File

@ -718,12 +718,10 @@ class BaseTrainer:
if str(self.model).endswith(".pt"):
weights, ckpt = load_checkpoint(self.model)
cfg = weights.yaml
if isinstance(self.args.pretrained, (str, Path)):
weights, _ = load_checkpoint(self.args.pretrained)
elif not self.args.pretrained and not self.resume:
weights = None
elif isinstance(self.args.pretrained, (str, Path)):
if isinstance(self.args.pretrained, (str, Path)):
weights, _ = load_checkpoint(self.args.pretrained)
elif self.args.pretrained is False and not self.resume:
weights = None
self.model = self.get_model(cfg=cfg, weights=weights, verbose=RANK in {-1, 0}) # calls Model(cfg, weights)
return ckpt

View File

@ -84,6 +84,8 @@ class ClassificationTrainer(BaseTrainer):
model.load(weights)
for m in model.modules():
if self.args.pretrained is False and hasattr(m, "reset_parameters"):
m.reset_parameters()
if isinstance(m, torch.nn.Dropout) and self.args.dropout:
m.p = self.args.dropout # set dropout
for p in model.parameters():