コンテンツにスキップ

高度なカスタマイズ

Ultralytics YOLOのコマンドラインとpythonインターフェースはどちらも、ベースエンジンのexecutor上に構築された高レベルの抽象化です。このガイドでは、 Trainer エンジン。特定のニーズに合わせてカスタマイズする方法を説明します。



見る: Ultralytics YOLOの習得:高度なカスタマイズ

BaseTrainer

The BaseTrainer クラスは、様々なタスクに適応可能な汎用的なトレーニングルーチンを提供します。必要な形式を遵守しながら、特定の関数または操作をオーバーライドしてカスタマイズします。たとえば、以下の関数をオーバーライドして、独自のカスタムモデルとデータローダーを統合します。

  • get_model(cfg, weights): トレーニングするモデルを構築します。
  • get_dataloader(): データローダーを構築します。

詳細とソースコードについては、以下を参照してください。 BaseTrainer リファレンス.

DetectionTrainer

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ドライブにアップロードするコールバックを追加して、トレーナーをさらにカスタマイズします。次に例を示します。

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の使用

The YOLO model classは、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 YOLOにおけるBaseTrainerの主要な構成要素は何ですか?

The BaseTrainer 汎用的なメソッドをオーバーライドすることで、さまざまなタスクに合わせてカスタマイズ可能なトレーニングルーチンの基盤として機能します。主なコンポーネントは次のとおりです。

  • get_model(cfg, weights): トレーニングするモデルを構築します。
  • get_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 インターフェースの両方で、複雑なタスクを簡素化します。
  • パフォーマンス: リアルタイムの物体検出や様々なVision AIアプリケーション向けに最適化されています。
  • カスタマイズ: カスタムモデル、損失関数、およびデータローダー用に簡単に拡張できます。
  • モジュール性: コンポーネントは、パイプライン全体に影響を与えることなく個別に変更できます。
  • 統合:MLエコシステムにおける一般的なフレームワークやツールとシームレスに連携します。

メインのUltralytics YOLOページを調べて、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年前に作成 ✏️ 5ヶ月前に更新

コメント