コンテンツへスキップ

高度なカスタマイズ

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



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

ベーストレーナー

BaseTrainer には、一般的な定型トレーニングルーチンが含まれています。正しい書式に従う限り、必要な関数や操作をオーバーライドすることで、どのようなタスクにもカスタマイズすることができます。例えば、これらの関数をオーバーライドするだけで、独自のカスタムモデルやデータローダをサポートすることができます:

  • get_model(cfg, weights) - 学習するモデルを構築する関数
  • get_dataloader() - データ・ローダーをビルドする関数 詳細とソース・コードは以下を参照。 BaseTrainer 参考

検出トレーナー

の使い方は以下の通り。YOLOv8 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.これらの詳細については、「参考文献」のセクションを参照のこと。

よくあるご質問

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

カスタマイズするにはUltralytics YOLOv8 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をご参照ください。 コールバック・ガイド.

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

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

  • get_model(cfg, weights) 学習するモデルを構築する。
  • get_dataloader() を使用してデータ・ローダーを構築する。

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

Ultralytics YOLOv8 DetectionTrainer にコールバックを追加するには?

コールバックを追加することで、以下のトレーニングプロセスを監視・変更することができる。Ultralytics YOLOv8 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 YOLOv8 を使うべき理由は?

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

  • 使いやすさ:コマンドラインとPython の両方のインターフェイスは、複雑なタスクを簡素化します。
  • パフォーマンスリアルタイムの物体検出や様々な視覚AIアプリケーションに最適化されています。
  • カスタマイズ:カスタムモデル、損失関数、データローダを簡単に拡張可能。

YOLOv8 の機能については、以下をご覧ください。 Ultralytics YOLO.

Ultralytics YOLOv8 DetectionTrainer を標準モデル以外に使用できますか?

そうだ、Ultralytics YOLOv8 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のドキュメントを参照してください。



作成日:2023-11-12 更新日:2024-07-04
著者:glenn-jocher(7),RizwanMunawar(1),AyushExel(1),Laughing-q(1)

コメント