From 58ccf00ea50ebe2008df20ee1d59459fe8aa2b71 Mon Sep 17 00:00:00 2001 From: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com> Date: Sat, 7 Jun 2025 10:06:06 +0600 Subject: [PATCH] Fix incorrect task when loading saved checkpoint (#20966) Signed-off-by: Mohammed Yasin <32206511+Y-T-G@users.noreply.github.com> --- ultralytics/engine/model.py | 2 +- ultralytics/nn/tasks.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ultralytics/engine/model.py b/ultralytics/engine/model.py index 1382cc29..9e510ccc 100644 --- a/ultralytics/engine/model.py +++ b/ultralytics/engine/model.py @@ -293,7 +293,7 @@ class Model(torch.nn.Module): if str(weights).rpartition(".")[-1] == "pt": self.model, self.ckpt = attempt_load_one_weight(weights) - self.task = self.model.args["task"] + self.task = self.model.task self.overrides = self.model.args = self._reset_ckpt_args(self.model.args) self.ckpt_path = self.model.pt_path else: diff --git a/ultralytics/nn/tasks.py b/ultralytics/nn/tasks.py index 092787c5..c19f939c 100644 --- a/ultralytics/nn/tasks.py +++ b/ultralytics/nn/tasks.py @@ -1505,7 +1505,7 @@ def attempt_load_weights(weights, device=None, inplace=True, fuse=False): # Model compatibility updates model.args = args # attach args to model model.pt_path = w # attach *.pt file path to model - model.task = guess_model_task(model) + model.task = getattr(model, "task", guess_model_task(model)) if not hasattr(model, "stride"): model.stride = torch.tensor([32.0]) @@ -1553,7 +1553,7 @@ def attempt_load_one_weight(weight, device=None, inplace=True, fuse=False): # Model compatibility updates model.args = {k: v for k, v in args.items() if k in DEFAULT_CFG_KEYS} # attach args to model model.pt_path = weight # attach *.pt file path to model - model.task = guess_model_task(model) + model.task = getattr(model, "task", guess_model_task(model)) if not hasattr(model, "stride"): model.stride = torch.tensor([32.0])