高级定制
Ultralytics YOLO 命令行接口和Python 接口都只是基础引擎执行器的高级抽象。让我们来看看 Trainer 引擎。
观看: Mastering Ultralytics YOLO: Advanced Customization
基础培训师
BaseTrainer 包含通用的模板培训例程。只要遵循正确的格式,就可以为任何任务定制所需的函数或操作。例如,只需重载这些函数,就能支持自己的自定义模型和数据加载器:
get_model(cfg, weights)
- 建立待训练模型的函数get_dataloader()
- 构建数据加载器的函数 更多详情和源代码请参见BaseTrainer
参考资料
探测训练器
Here's how you can use the YOLO11 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()
要了解有关回调触发事件和入口点的更多信息,请查阅我们的《回调指南》。
其他发动机部件
还有其他类似的可定制组件,如 Validators
和 Predictors
.有关更多信息,请参见参考资料部分。
常见问题
How do I customize the Ultralytics YOLO11 DetectionTrainer for specific tasks?
To customize the Ultralytics YOLO11 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
您可以参考我们的 回调指南.
What are the key components of the BaseTrainer in Ultralytics YOLO11?
"(《世界人权宣言》) BaseTrainer
in Ultralytics YOLO11 serves as the foundation for training routines and can be customized for various tasks by overriding its generic methods. Key components include:
get_model(cfg, weights)
来建立要训练的模型。get_dataloader()
来构建数据加载器。
有关定制和源代码的更多详情,请参阅 BaseTrainer
参考资料.
How can I add a callback to the Ultralytics YOLO11 DetectionTrainer?
You can add callbacks to monitor and modify the training process in Ultralytics YOLO11 DetectionTrainer
. For instance, here's how you can add a callback to log model weights after every training epoch:
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()
有关回调事件和入口点的更多详情,请参阅我们的《回调指南》。
Why should I use Ultralytics YOLO11 for model training?
Ultralytics YOLO11 offers a high-level abstraction on powerful engine executors, making it ideal for rapid development and customization. Key benefits include:
- 易用性:命令行和Python 界面均可简化复杂任务。
- Performance: Optimized for real-time object detection and various vision AI applications.
- Customization: Easily extendable for custom models, loss functions, and dataloaders.
Learn more about YOLO11's capabilities by visiting Ultralytics YOLO.
Can I use the Ultralytics YOLO11 DetectionTrainer for non-standard models?
Yes, Ultralytics YOLO11 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文档。