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])