コンテンツへスキップ

高度なカスタマイズ

Ultralytics YOLO コマンドラインとPython インターフェースの両方は、ベースエンジンエグゼキューター上に構築された高レベルの抽象化です。このガイドでは Trainer エンジンをカスタマイズする方法を説明する。



見るんだ: MasteringUltralytics YOLO : 高度なカスタマイズ

ベーストレーナー

について BaseTrainer クラスは様々なタスクに適応可能な一般的なトレーニングルーチンを提供します。必要な書式を守りながら特定の関数や操作をオーバーライドしてカスタマイズします。例えば、これらの関数をオーバーライドすることで、独自のカスタムモデルやデータローダを統合することができます:

  • get_model(cfg, weights):学習するモデルを構築する。
  • get_dataloader():dataloaderをビルドします。

詳細とソースコードについては BaseTrainer 参考.

検出トレーナー

Ultralytics YOLO使い方とカスタマイズ方法は以下の通りです。 DetectionTrainer:

from ultralytics.models.yolo.detect import DetectionTrainer

trainer = DetectionTrainer(overrides={...})
trainer.train()
trained_model = trainer.best  # Get the 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()

損失関数を変更したり、10エポックごとにGoogle Driveにモデルをアップロードするコールバックを追加することで、トレーナーをさらにカスタマイズすることができます。以下はその例です:

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 callbacks
trainer.train()

コールバックのトリガー・イベントとエントリー・ポイントの詳細については、コールバック・ガイドを参照してください。

その他のエンジン・コンポーネント

などの他のコンポーネントをカスタマイズする。 Validators そして Predictors 同様である。詳細は バリデーター そして 予測要因.

カスタムトレーナーでYOLO 使用する

について YOLO modelクラスは、Trainerクラスの高レベルラッパーを提供します。このアーキテクチャを活用することで、機械学習ワークフローの柔軟性を高めることができます:

from ultralytics import YOLO
from ultralytics.models.yolo.detect import DetectionTrainer


# Create a custom trainer
class MyCustomTrainer(DetectionTrainer):
    def get_model(self, cfg, weights):
        """Custom code implementation."""
        ...


# Initialize YOLO model
model = YOLO("yolo11n.pt")

# Train with custom trainer
results = model.train(trainer=MyCustomTrainer, data="coco8.yaml", epochs=3)

このアプローチにより、YOLO インターフェイスのシンプルさを維持しながらも、基本的なトレーニング・プロセスを特定の要件に合わせてカスタマイズすることができます。

よくあるご質問

特定のタスク用にUltralytics YOLO DetectionTrainerをカスタマイズするには?

をカスタマイズする 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 the best model

損失関数の変更やコールバックの追加など、さらなるカスタマイズについては、コールバックガイドを参照してください。

Ultralytics YOLOBaseTrainerの主なコンポーネントは何ですか?

について BaseTrainer は、トレーニングルーチンの基礎となるもので、汎用的なメソッドをオーバーライドすることで、さまざまなタスク用にカスタマイズすることができます。主なコンポーネントは以下の通り:

  • get_model(cfg, weights):学習するモデルを構築する。
  • get_dataloader():dataloaderをビルドします。
  • preprocess_batch():モデルのフォワードパス前のバッチ前処理を行う。
  • set_model_attributes():データセット情報に基づいてモデルの属性を設定する。
  • get_validator():モデル評価用のバリデータを返します。

カスタマイズとソースコードの詳細については BaseTrainer 参考.

Ultralytics YOLO DetectionTrainerにコールバックを追加する方法を教えてください。

コールバックを追加して、以下のトレーニング・プロセスを監視・修正する。 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 YOLO 使う必要があるのですか?

Ultralytics YOLO 、強力なエンジンエグゼキュータに対する高レベルの抽象化を提供し、迅速な開発とカスタマイズに最適です。主な利点は以下の通りです:

  • 使いやすさ:コマンドラインとPython の両方のインターフェイスは、複雑なタスクを簡素化します。
  • パフォーマンスリアルタイムの物体検出や様々な視覚AIアプリケーションに最適化されています。
  • カスタマイズ:カスタムモデル、損失関数、データローダを簡単に拡張可能。
  • モジュール性:パイプライン全体に影響を与えることなく、コンポーネントを個別に変更できます。
  • 統合:MLエコシステム内の一般的なフレームワークやツールとシームレスに連携。

YOLO機能についての詳細は、以下のページをご覧ください。 Ultralytics YOLOのページをご覧ください。

Ultralytics YOLO DetectionTrainerを非標準モデルに使用できますか?

そうだ。 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 参考.



📅作成:1年前 ✏️更新しました 2ヶ月前

コメント