Consolidate checkpoint loading functions (#21933)

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn Jocher 2025-09-04 13:02:52 +02:00 committed by GitHub
parent 0b93106d0a
commit 0cf560e30b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 17 additions and 72 deletions

View File

@ -71,11 +71,7 @@ keywords: Ultralytics, YOLO, nn tasks, DetectionModel, PoseModel, RTDETRDetectio
<br><br><hr><br>
## ::: ultralytics.nn.tasks.attempt_load_weights
<br><br><hr><br>
## ::: ultralytics.nn.tasks.attempt_load_one_weight
## ::: ultralytics.nn.tasks.load_checkpoint
<br><br><hr><br>

View File

@ -12,7 +12,7 @@ from PIL import Image
from ultralytics.cfg import TASK2DATA, get_cfg, get_save_dir
from ultralytics.engine.results import Results
from ultralytics.nn.tasks import attempt_load_one_weight, guess_model_task, yaml_model_load
from ultralytics.nn.tasks import guess_model_task, load_checkpoint, yaml_model_load
from ultralytics.utils import (
ARGV,
ASSETS,
@ -294,7 +294,7 @@ class Model(torch.nn.Module):
weights = checks.check_model_file_from_stem(weights) # add suffix, i.e. yolo11n -> yolo11n.pt
if str(weights).rpartition(".")[-1] == "pt":
self.model, self.ckpt = attempt_load_one_weight(weights)
self.model, self.ckpt = load_checkpoint(weights)
self.task = self.model.task
self.overrides = self.model.args = self._reset_ckpt_args(self.model.args)
self.ckpt_path = self.model.pt_path
@ -385,7 +385,7 @@ class Model(torch.nn.Module):
self._check_is_pytorch_model()
if isinstance(weights, (str, Path)):
self.overrides["pretrained"] = weights # remember the weights for DDP training
weights, self.ckpt = attempt_load_one_weight(weights)
weights, self.ckpt = load_checkpoint(weights)
self.model.load(weights)
return self
@ -802,7 +802,7 @@ class Model(torch.nn.Module):
# Update model and cfg after training
if RANK in {-1, 0}:
ckpt = self.trainer.best if self.trainer.best.exists() else self.trainer.last
self.model, self.ckpt = attempt_load_one_weight(ckpt)
self.model, self.ckpt = load_checkpoint(ckpt)
self.overrides = self.model.args
self.metrics = getattr(self.trainer.validator, "metrics", None) # TODO: no metrics returned by DDP
return self.metrics

View File

@ -24,7 +24,7 @@ from torch import nn, optim
from ultralytics import __version__
from ultralytics.cfg import get_cfg, get_save_dir
from ultralytics.data.utils import check_cls_dataset, check_det_dataset
from ultralytics.nn.tasks import attempt_load_one_weight, attempt_load_weights
from ultralytics.nn.tasks import load_checkpoint
from ultralytics.utils import (
DEFAULT_CFG,
GIT,
@ -644,10 +644,10 @@ class BaseTrainer:
cfg, weights = self.model, None
ckpt = None
if str(self.model).endswith(".pt"):
weights, ckpt = attempt_load_one_weight(self.model)
weights, ckpt = load_checkpoint(self.model)
cfg = weights.yaml
elif isinstance(self.args.pretrained, (str, Path)):
weights, _ = attempt_load_one_weight(self.args.pretrained)
weights, _ = load_checkpoint(self.args.pretrained)
self.model = self.get_model(cfg=cfg, weights=weights, verbose=RANK == -1) # calls Model(cfg, weights)
return ckpt
@ -768,7 +768,7 @@ class BaseTrainer:
last = Path(check_file(resume) if exists else get_latest_run())
# Check that resume data YAML exists, otherwise strip to force re-download of dataset
ckpt_args = attempt_load_weights(last).args
ckpt_args = load_checkpoint(last)[0].args
if not isinstance(ckpt_args["data"], dict) and not Path(ckpt_args["data"]).exists():
ckpt_args["data"] = self.args.data

View File

@ -186,9 +186,9 @@ class YOLOEDetectValidator(DetectionValidator):
self.device = select_device(self.args.device, verbose=False)
if isinstance(model, (str, Path)):
from ultralytics.nn.tasks import attempt_load_weights
from ultralytics.nn.tasks import load_checkpoint
model = attempt_load_weights(model, device=self.device)
model, _ = load_checkpoint(model, device=self.device) # model, ckpt
model.eval().to(self.device)
data = check_det_dataset(refer_data or self.args.data)
names = [name.split("/", 1)[0] for name in list(data["names"].values())]

View File

@ -5,18 +5,16 @@ from .tasks import (
ClassificationModel,
DetectionModel,
SegmentationModel,
attempt_load_one_weight,
attempt_load_weights,
guess_model_scale,
guess_model_task,
load_checkpoint,
parse_model,
torch_safe_load,
yaml_model_load,
)
__all__ = (
"attempt_load_one_weight",
"attempt_load_weights",
"load_checkpoint",
"parse_model",
"yaml_model_load",
"guess_model_task",

View File

@ -203,9 +203,9 @@ class AutoBackend(nn.Module):
model = model.fuse(verbose=verbose)
model = model.to(device)
else: # pt file
from ultralytics.nn.tasks import attempt_load_one_weight
from ultralytics.nn.tasks import load_checkpoint
model, _ = attempt_load_one_weight(model, device=device, fuse=fuse) # load model, ckpt
model, _ = load_checkpoint(model, device=device, fuse=fuse) # load model, ckpt
# Common PyTorch model processing
if hasattr(model, "kpt_shape"):

View File

@ -1483,61 +1483,12 @@ def torch_safe_load(weight, safe_only=False):
return ckpt, file
def attempt_load_weights(weights, device=None, inplace=True, fuse=False):
"""
Load an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a.
Args:
weights (str | List[str]): Model weights path(s).
device (torch.device, optional): Device to load model to.
inplace (bool): Whether to do inplace operations.
fuse (bool): Whether to fuse model.
Returns:
(torch.nn.Module): Loaded model.
"""
ensemble = Ensemble()
for w in weights if isinstance(weights, list) else [weights]:
ckpt, w = torch_safe_load(w) # load ckpt
args = {**DEFAULT_CFG_DICT, **ckpt["train_args"]} if "train_args" in ckpt else None # combined args
model = (ckpt.get("ema") or ckpt["model"]).float() # FP32 model
# Model compatibility updates
model.args = args # attach args to model
model.pt_path = w # attach *.pt file path to model
model.task = getattr(model, "task", guess_model_task(model))
if not hasattr(model, "stride"):
model.stride = torch.tensor([32.0])
# Append
ensemble.append((model.fuse().eval() if fuse and hasattr(model, "fuse") else model.eval()).to(device))
# Module updates
for m in ensemble.modules():
if hasattr(m, "inplace"):
m.inplace = inplace
elif isinstance(m, torch.nn.Upsample) and not hasattr(m, "recompute_scale_factor"):
m.recompute_scale_factor = None # torch 1.11.0 compatibility
# Return model
if len(ensemble) == 1:
return ensemble[-1]
# Return ensemble
LOGGER.info(f"Ensemble created with {weights}\n")
for k in "names", "nc", "yaml":
setattr(ensemble, k, getattr(ensemble[0], k))
ensemble.stride = ensemble[int(torch.argmax(torch.tensor([m.stride.max() for m in ensemble])))].stride
assert all(ensemble[0].nc == m.nc for m in ensemble), f"Models differ in class counts {[m.nc for m in ensemble]}"
return ensemble
def attempt_load_one_weight(weight, device=None, inplace=True, fuse=False):
def load_checkpoint(weight, device=None, inplace=True, fuse=False):
"""
Load a single model weights.
Args:
weight (str): Model weight path.
weight (str | Path): Model weight path.
device (torch.device, optional): Device to load model to.
inplace (bool): Whether to do inplace operations.
fuse (bool): Whether to fuse model.