From 0cf560e30b3ea54e1ebbed3f6f461507d31ba331 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 4 Sep 2025 13:02:52 +0200 Subject: [PATCH] Consolidate checkpoint loading functions (#21933) Signed-off-by: Glenn Jocher Co-authored-by: UltralyticsAssistant --- docs/en/reference/nn/tasks.md | 6 +--- ultralytics/engine/model.py | 8 ++--- ultralytics/engine/trainer.py | 8 ++--- ultralytics/models/yolo/yoloe/val.py | 4 +-- ultralytics/nn/__init__.py | 6 ++-- ultralytics/nn/autobackend.py | 4 +-- ultralytics/nn/tasks.py | 53 ++-------------------------- 7 files changed, 17 insertions(+), 72 deletions(-) diff --git a/docs/en/reference/nn/tasks.md b/docs/en/reference/nn/tasks.md index 07e52845..72bde10f 100644 --- a/docs/en/reference/nn/tasks.md +++ b/docs/en/reference/nn/tasks.md @@ -71,11 +71,7 @@ keywords: Ultralytics, YOLO, nn tasks, DetectionModel, PoseModel, RTDETRDetectio



-## ::: ultralytics.nn.tasks.attempt_load_weights - -



- -## ::: ultralytics.nn.tasks.attempt_load_one_weight +## ::: ultralytics.nn.tasks.load_checkpoint



diff --git a/ultralytics/engine/model.py b/ultralytics/engine/model.py index 07cbd14f..7ab279d3 100644 --- a/ultralytics/engine/model.py +++ b/ultralytics/engine/model.py @@ -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 diff --git a/ultralytics/engine/trainer.py b/ultralytics/engine/trainer.py index 79ac72c8..0869c311 100644 --- a/ultralytics/engine/trainer.py +++ b/ultralytics/engine/trainer.py @@ -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 diff --git a/ultralytics/models/yolo/yoloe/val.py b/ultralytics/models/yolo/yoloe/val.py index b3a45db4..4e0744d5 100644 --- a/ultralytics/models/yolo/yoloe/val.py +++ b/ultralytics/models/yolo/yoloe/val.py @@ -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())] diff --git a/ultralytics/nn/__init__.py b/ultralytics/nn/__init__.py index f3a54d51..fb2c034e 100644 --- a/ultralytics/nn/__init__.py +++ b/ultralytics/nn/__init__.py @@ -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", diff --git a/ultralytics/nn/autobackend.py b/ultralytics/nn/autobackend.py index ad0ded24..610f9c95 100644 --- a/ultralytics/nn/autobackend.py +++ b/ultralytics/nn/autobackend.py @@ -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"): diff --git a/ultralytics/nn/tasks.py b/ultralytics/nn/tasks.py index 999d6638..b4b07bf6 100644 --- a/ultralytics/nn/tasks.py +++ b/ultralytics/nn/tasks.py @@ -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.