高度なカスタマイズ
Ultralytics YOLOのコマンドラインおよびPythonインターフェースは、どちらもベースとなるエンジンエグゼキューター上に構築された高レベルの抽象化です。本ガイドではTrainerエンジンに焦点を当て、特定のニーズに合わせてカスタマイズする方法を解説します。
Watch: Mastering Ultralytics YOLO: Advanced Customization
カスタムメトリクス、クラス加重損失、モデル保存、バックボーンのフリーズ、層ごとの学習率など、一般的なトレーナーのカスタマイズに関する実用的な例については、Customizing Trainerガイドを参照してください。
BaseTrainer
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 modelDetectionTrainerのカスタマイズ
直接サポートされていないカスタム検出モデルを学習するには、既存の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()Further customize the trainer by modifying the loss function or adding a callback to upload the model to Google Drive every 10 epochs. Here's an example:
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()コールバックのトリガーイベントやエントリポイントの詳細については、Callbacksガイドを参照してください。
その他のエンジンコンポーネント
ValidatorsやPredictorsなどの他のコンポーネントも同様にカスタマイズできます。詳細については、ValidatorsおよびPredictorsのドキュメントを参照してください。
カスタムトレーナーでのYOLOの使用
YOLOモデルクラスは、トレーナークラスのための高レベルなラッパーを提供します。このアーキテクチャを活用することで、機械学習ワークフローの柔軟性を高めることができます。
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("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, weights):
"""Loads a custom detection model given configuration and weight files."""
...
trainer = CustomTrainer(overrides={...})
trainer.train()
trained_model = trainer.best # Get the best model損失関数の変更やコールバックの追加などのさらなるカスタマイズについては、Callbacksガイドを参照してください。
Ultralytics YOLOにおけるBaseTrainerの主要コンポーネントは何ですか?
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()コールバックイベントやエントリポイントの詳細については、Callbacksガイドを参照してください。
なぜモデルの学習にUltralytics YOLOを使用すべきなのですか?
Ultralytics YOLOは、強力なエンジンエグゼキューターに対する高レベルな抽象化を提供するため、迅速な開発とカスタマイズに最適です。主な利点は以下の通りです。
- 使いやすさ: コマンドラインとPythonインターフェースの両方が複雑なタスクを簡素化します。
- パフォーマンス: リアルタイムの物体検出やさまざまなビジョンAIアプリケーション向けに最適化されています。
- カスタマイズ性: カスタムモデル、損失関数、データローダーに対して容易に拡張可能です。
- モジュール性: コンポーネントは、パイプライン全体に影響を与えることなく独立して変更できます。
- 統合性: 機械学習エコシステムの一般的なフレームワークやツールとシームレスに連携します。
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リファレンスを確認してください。