YOLO Vision 2026:

高度なカスタマイズ#

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



Watch: Mastering Ultralytics YOLO: Advanced Customization
ヒント

一般的なトレーナーのカスタマイズ(カスタムメトリクス、クラス重み付け損失、モデル保存、バックボーンの凍結、レイヤーごとの学習率など)の実際的な例については、Customizing Trainerガイドを参照してください。

BaseTrainer#

BaseTrainerクラスは、さまざまなタスクに適応可能な汎用トレーニングルーチンを提供します。必要なフォーマットに準拠しながら特定の機能や操作をオーバーライドすることでカスタマイズします。たとえば、これらの機能をオーバーライドすることで独自のカスタムモデルとデータローダーを統合できます:

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

詳細とソースコードについては、BaseTrainer Referenceを参照してください。

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=None, weights=None, verbose=True):
        """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=None, weights=None, verbose=True):
        """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()

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

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

ValidatorsPredictorsなどの他のコンポーネントも同様にカスタマイズします。詳細については、ValidatorsおよびPredictorsのドキュメントを参照してください。

カスタムトレーナーでの YOLO の使用#

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

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

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

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

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

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

よくある質問 (FAQ)#

特定のタスクに合わせて Ultralytics YOLO DetectionTrainer をカスタマイズするにはどうすればよいですか?#

カスタムモデルとデータローダーに適応するようにメソッドをオーバーライドすることで、特定のタスクに合わせてDetectionTrainerをカスタマイズします。DetectionTrainerから継承し、get_modelなどのメソッドを再定義してカスタム機能を実装することから始めます。例は次のとおりです:

from ultralytics.models.yolo.detect import DetectionTrainer

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

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

loss functionの変更やcallbackの追加などのさらなるカスタマイズについては、Callbacks Guideを参照してください。

Ultralytics YOLO の BaseTrainer の主要なコンポーネントは何ですか?#

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

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

カスタマイズとソースコードの詳細については、BaseTrainer Referenceを参照してください。

Ultralytics YOLO DetectionTrainer にコールバックを追加するにはどうすればよいですか?#

DetectionTrainerでトレーニングプロセスを監視および変更するためのコールバックを追加します。トレーニング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()

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

モデルのトレーニングに Ultralytics YOLO を使用すべきなのはなぜですか?#

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

  • 使いやすさ: コマンドラインと Python インターフェースの両方で複雑なタスクが簡素化されます。
  • パフォーマンス: リアルタイムのobject detectionおよびさまざまなビジョンAIアプリケーション向けに最適化されています。
  • カスタマイズ: カスタムモデル、loss functions、およびデータローダーを簡単に拡張できます。
  • モジュール性: パイプライン全体に影響を与えることなく、コンポーネントを独立して変更できます。
  • 統合: ML エコシステム内の一般的なフレームワークやツールとシームレスに連携します。

メインのUltralytics YOLOページを探索して、YOLOの機能の詳細を確認してください。

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

はい、DetectionTrainerは非常に柔軟であり、非標準モデルに対してカスタマイズ可能です。DetectionTrainerから継承し、特定のモデルのニーズをサポートするためにメソッドをオーバーロードします。簡単な例は次のとおりです:

from ultralytics.models.yolo.detect import DetectionTrainer

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

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

包括的な手順と例については、DetectionTrainer Referenceを確認してください。

コメント