跳至内容

高级定制

Ultralytics YOLO 命令行接口和Python 接口都只是基础引擎执行器的高级抽象。让我们来看看 Trainer 引擎。



观看: 掌握Ultralytics YOLOv8 :高级定制

基础培训师

BaseTrainer 包含通用的模板培训例程。只要遵循正确的格式,就可以为任何任务定制所需的函数或操作。例如,只需重载这些函数,就能支持自己的自定义模型和数据加载器:

  • get_model(cfg, weights) - 建立待训练模型的函数
  • get_dataloader() - 构建数据加载器的函数 更多详情和源代码请参见 BaseTrainer 参考资料

探测训练器

以下是如何使用YOLOv8 DetectionTrainer 并进行定制。

from ultralytics.models.yolo.detect import DetectionTrainer

trainer = DetectionTrainer(overrides={...})
trainer.train()
trained_model = trainer.best  # get best model

自定义检测训练器

让我们定制培训师 来训练自定义检测模型 的重载。您只需重载现有的 get_model 功能性:

from ultralytics.models.yolo.detect import DetectionTrainer


class CustomTrainer(DetectionTrainer):
    def get_model(self, cfg, weights):
        """Loads a custom detection model given configuration and weight files."""
        ...


trainer = CustomTrainer(overrides={...})
trainer.train()

您现在意识到,您需要进一步定制培训师:

  • 自定义 loss function.
  • 添加 callback 每隔 10 分钟就会将模型上传到Google Drive。 epochs 具体方法如下
from ultralytics.models.yolo.detect import DetectionTrainer
from ultralytics.nn.tasks import DetectionModel


class MyCustomModel(DetectionModel):
    def init_criterion(self):
        """Initializes the loss function and adds a callback for uploading the model to Google Drive every 10 epochs."""
        ...


class CustomTrainer(DetectionTrainer):
    def get_model(self, cfg, weights):
        """Returns a customized detection model instance configured with specified config and weights."""
        return MyCustomModel(...)


# callback to upload model weights
def log_model(trainer):
    """Logs the path of the last model weight used by the trainer."""
    last_weight_path = trainer.last
    print(last_weight_path)


trainer = CustomTrainer(overrides={...})
trainer.add_callback("on_train_epoch_end", log_model)  # Adds to existing callback
trainer.train()

要了解有关回调触发事件和入口点的更多信息,请查阅我们的《回调指南》

其他发动机部件

还有其他类似的可定制组件,如 ValidatorsPredictors.有关更多信息,请参见参考资料部分。

常见问题

如何为特定任务定制Ultralytics YOLOv8 DetectionTrainer?

自定义Ultralytics YOLOv8 DetectionTrainer 的方法,你可以重写其方法以适应你的自定义模型和数据加载器。首先从 DetectionTrainer 然后重新定义方法,如 get_model 来实现自定义功能。下面是一个例子:

from ultralytics.models.yolo.detect import DetectionTrainer


class CustomTrainer(DetectionTrainer):
    def get_model(self, cfg, weights):
        """Loads a custom detection model given configuration and weight files."""
        ...


trainer = CustomTrainer(overrides={...})
trainer.train()
trained_model = trainer.best  # get best model

如需进一步定制,如更改 loss function 或添加一个 callback您可以参考我们的 回调指南.

Ultralytics YOLOv8 中 BaseTrainer 的主要组成部分是什么?

"(《世界人权宣言》) BaseTrainer Ultralytics YOLOv8 作为训练例程的基础,可通过覆盖其通用方法为各种任务进行定制。主要组件包括

  • get_model(cfg, weights) 来建立要训练的模型。
  • get_dataloader() 来构建数据加载器。

有关定制和源代码的更多详情,请参阅 BaseTrainer 参考资料.

如何为Ultralytics YOLOv8 DetectionTrainer 添加回调?

您可以添加回调,以便在Ultralytics YOLOv8 DetectionTrainer.例如,您可以在每次训练历时后添加一个回调来记录模型权重:

from ultralytics.models.yolo.detect import DetectionTrainer


# callback to upload model weights
def log_model(trainer):
    """Logs the path of the last model weight used by the trainer."""
    last_weight_path = trainer.last
    print(last_weight_path)


trainer = DetectionTrainer(overrides={...})
trainer.add_callback("on_train_epoch_end", log_model)  # Adds to existing callbacks
trainer.train()

有关回调事件和入口点的更多详情,请参阅我们的《回调指南》

为什么要使用Ultralytics YOLOv8 进行模型训练?

Ultralytics YOLOv8 为强大的引擎执行器提供了高级抽象,是快速开发和定制的理想选择。主要优势包括

  • 易用性:命令行和Python 界面均可简化复杂任务。
  • 性能针对实时物体检测和各种视觉人工智能应用进行了优化。
  • 自定义:可轻松扩展自定义模型、损失函数和数据加载器。

了解有关YOLOv8 功能的更多信息,请访问 Ultralytics YOLO.

Ultralytics YOLOv8 DetectionTrainer 可以用于非标准模型吗?

是的、Ultralytics YOLOv8 DetectionTrainer 具有高度灵活性,可针对非标准模型进行定制。通过继承自 DetectionTrainer因此,您可以重载不同的方法,以支持特定模型的需求。下面是一个简单的例子:

from ultralytics.models.yolo.detect import DetectionTrainer


class CustomDetectionTrainer(DetectionTrainer):
    def get_model(self, cfg, weights):
        """Loads a custom detection model."""
        ...


trainer = CustomDetectionTrainer(overrides={...})
trainer.train()

有关更全面的说明和示例,请查看DetectionTrainer文档。



创建于 2023-11-12,更新于 2024-07-04
作者:glenn-jocher(7),RizwanMunawar(1),AyushExel(1),Laughing-q(1)

评论