コンテンツへスキップ

高度なカスタマイズ

Ultralytics YOLO コマンドライン・インターフェイスもPython インターフェイスも、ベース・エンジンのエグゼキューターに対するハイレベルな抽象化に過ぎない。トレーナー・エンジンを見てみよう。



見るんだ: 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

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

あなたは今、トレーナーをさらにカスタマイズする必要があることに気づいた:

  • をカスタマイズする loss function.
  • 追加 callback 10回ごとにモデルをGoogle ドライブにアップロードします。 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のドキュメントを参照してください。

📅 Created 11 months ago ✏️ Updated 20 days ago

コメント