Fix duplicate pretrained weight loading in Model.train() for YAML models (#23640)
Co-authored-by: Onuralp SEZER <onuralp@ultralytics.com> Co-authored-by: Ultralytics Assistant <135830346+UltralyticsAssistant@users.noreply.github.com> Co-authored-by: UltralyticsAssistant <web@ultralytics.com> Co-authored-by: Laughing-q <1185102784@qq.com> Co-authored-by: Jing Qiu <61612323+Laughing-q@users.noreply.github.com>
This commit is contained in:
parent
75acff57c2
commit
fa1db1f467
@ -756,8 +756,6 @@ class Model(torch.nn.Module):
|
||||
|
||||
checks.check_pip_update_available()
|
||||
|
||||
if isinstance(kwargs.get("pretrained", None), (str, Path)):
|
||||
self.load(kwargs["pretrained"]) # load pretrained weights if provided
|
||||
overrides = YAML.load(checks.check_yaml(kwargs["cfg"])) if kwargs.get("cfg") else self.overrides
|
||||
custom = {
|
||||
# NOTE: handle the case when 'cfg' includes 'data'.
|
||||
@ -781,9 +779,6 @@ class Model(torch.nn.Module):
|
||||
args["resume"] = False
|
||||
|
||||
self.trainer = (trainer or self._smart_load("trainer"))(overrides=args, _callbacks=self.callbacks)
|
||||
if not args.get("resume"): # manually set model only if not resuming
|
||||
self.trainer.model = self.trainer.get_model(weights=self.model if self.ckpt else None, cfg=self.model.yaml)
|
||||
self.model = self.trainer.model
|
||||
|
||||
self.trainer.train()
|
||||
# Update model and cfg after training
|
||||
|
||||
@ -720,7 +720,7 @@ class BaseTrainer:
|
||||
cfg = weights.yaml
|
||||
elif isinstance(self.args.pretrained, (str, Path)):
|
||||
weights, _ = load_checkpoint(self.args.pretrained)
|
||||
self.model = self.get_model(cfg=cfg, weights=weights, verbose=RANK == -1) # calls Model(cfg, weights)
|
||||
self.model = self.get_model(cfg=cfg, weights=weights, verbose=RANK in {-1, 0}) # calls Model(cfg, weights)
|
||||
return ckpt
|
||||
|
||||
def optimizer_step(self):
|
||||
|
||||
@ -52,7 +52,9 @@ class RTDETRTrainer(DetectionTrainer):
|
||||
Returns:
|
||||
(RTDETRDetectionModel): Initialized model.
|
||||
"""
|
||||
model = RTDETRDetectionModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK == -1)
|
||||
model = RTDETRDetectionModel(
|
||||
cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK in {-1, 0}
|
||||
)
|
||||
if weights:
|
||||
model.load(weights)
|
||||
return model
|
||||
|
||||
@ -79,7 +79,9 @@ class ClassificationTrainer(BaseTrainer):
|
||||
Returns:
|
||||
(ClassificationModel): Configured PyTorch model for classification.
|
||||
"""
|
||||
model = ClassificationModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK == -1)
|
||||
model = ClassificationModel(
|
||||
cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK in {-1, 0}
|
||||
)
|
||||
if weights:
|
||||
model.load(weights)
|
||||
|
||||
|
||||
@ -160,7 +160,7 @@ class DetectionTrainer(BaseTrainer):
|
||||
Returns:
|
||||
(DetectionModel): YOLO detection model.
|
||||
"""
|
||||
model = DetectionModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK == -1)
|
||||
model = DetectionModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK in {-1, 0})
|
||||
if weights:
|
||||
model.load(weights)
|
||||
return model
|
||||
|
||||
@ -64,7 +64,7 @@ class OBBTrainer(yolo.detect.DetectionTrainer):
|
||||
>>> trainer = OBBTrainer()
|
||||
>>> model = trainer.get_model(cfg="yolo26n-obb.yaml", weights="yolo26n-obb.pt")
|
||||
"""
|
||||
model = OBBModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK == -1)
|
||||
model = OBBModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK in {-1, 0})
|
||||
if weights:
|
||||
model.load(weights)
|
||||
|
||||
|
||||
@ -55,7 +55,9 @@ class SegmentationTrainer(yolo.detect.DetectionTrainer):
|
||||
>>> model = trainer.get_model(cfg="yolo26n-seg.yaml")
|
||||
>>> model = trainer.get_model(weights="yolo26n-seg.pt", verbose=False)
|
||||
"""
|
||||
model = SegmentationModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK == -1)
|
||||
model = SegmentationModel(
|
||||
cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK in {-1, 0}
|
||||
)
|
||||
if weights:
|
||||
model.load(weights)
|
||||
|
||||
|
||||
@ -83,7 +83,7 @@ class WorldTrainer(DetectionTrainer):
|
||||
cfg["yaml_file"] if isinstance(cfg, dict) else cfg,
|
||||
ch=self.data["channels"],
|
||||
nc=min(self.data["nc"], 80),
|
||||
verbose=verbose and RANK == -1,
|
||||
verbose=verbose and RANK in {-1, 0},
|
||||
)
|
||||
if weights:
|
||||
model.load(weights)
|
||||
|
||||
@ -70,7 +70,7 @@ class YOLOETrainer(DetectionTrainer):
|
||||
cfg["yaml_file"] if isinstance(cfg, dict) else cfg,
|
||||
ch=self.data["channels"],
|
||||
nc=min(self.data["nc"], 80),
|
||||
verbose=verbose and RANK == -1,
|
||||
verbose=verbose and RANK in {-1, 0},
|
||||
)
|
||||
if weights:
|
||||
model.load(weights)
|
||||
@ -128,7 +128,7 @@ class YOLOEPETrainer(DetectionTrainer):
|
||||
cfg["yaml_file"] if isinstance(cfg, dict) else cfg,
|
||||
ch=self.data["channels"],
|
||||
nc=self.data["nc"],
|
||||
verbose=verbose and RANK == -1,
|
||||
verbose=verbose and RANK in {-1, 0},
|
||||
)
|
||||
|
||||
del model.model[-1].savpe
|
||||
|
||||
@ -39,7 +39,7 @@ class YOLOESegTrainer(YOLOETrainer, SegmentationTrainer):
|
||||
cfg["yaml_file"] if isinstance(cfg, dict) else cfg,
|
||||
ch=self.data["channels"],
|
||||
nc=min(self.data["nc"], 80),
|
||||
verbose=verbose and RANK == -1,
|
||||
verbose=verbose and RANK in {-1, 0},
|
||||
)
|
||||
if weights:
|
||||
model.load(weights)
|
||||
@ -85,7 +85,7 @@ class YOLOEPESegTrainer(SegmentationTrainer):
|
||||
cfg["yaml_file"] if isinstance(cfg, dict) else cfg,
|
||||
ch=self.data["channels"],
|
||||
nc=self.data["nc"],
|
||||
verbose=verbose and RANK == -1,
|
||||
verbose=verbose and RANK in {-1, 0},
|
||||
)
|
||||
|
||||
del model.model[-1].savpe
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user