Link to this section高度なカスタマイズ#
Ultralytics YOLOのコマンドラインインターフェースとPythonインターフェースは、どちらもベースエンジンエグゼキュータ上に構築された高度な抽象化レイヤーです。このガイドでは、Trainerエンジンに焦点を当て、特定のニーズに合わせてカスタマイズする方法を解説します。
Watch: Mastering Ultralytics YOLO: Advanced Customization
カスタムメトリクス、クラス重み付けロス、モデルの保存、バックボーンの凍結、レイヤーごとの学習率といった一般的なトレーナーのカスタマイズに関する実践的な例については、Customizing Trainerガイドを参照してください。
Link to this sectionBaseTrainer#
BaseTrainerクラスは、様々なタスクに適応可能な汎用的なトレーニングルーチンを提供します。必須のフォーマットに従いつつ、特定の関数や操作をオーバーライドすることでカスタマイズが可能です。例えば、以下の関数をオーバーライドして、独自のカスタムモデルやデータローダーを統合できます:
get_model(cfg, weights): トレーニングするモデルを構築します。get_dataloader(): データローダーを構築します。
詳細やソースコードについては、BaseTrainer Referenceを参照してください。
Link to this sectionDetectionTrainer#
Ultralytics YOLOのDetectionTrainerの使用およびカスタマイズ方法は以下の通りです:
from ultralytics.models.yolo.detect import DetectionTrainer
trainer = DetectionTrainer(overrides={...})
trainer.train()
trained_model = trainer.best # Get the best modelLink to this sectionDetectionTrainerのカスタマイズ#
直接サポートされていないカスタム検出モデルをトレーニングするには、既存の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を参照してください。
Link to this sectionその他のエンジンコンポーネント#
ValidatorsやPredictorsなどの他のコンポーネントも同様にカスタマイズ可能です。詳細については、ValidatorsおよびPredictorsのドキュメントを参照してください。
Link to this sectionカスタムトレーナーでの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インターフェースのシンプルさを維持しながら、基盤となるトレーニングプロセスを特定の要件に合わせてカスタマイズすることが可能です。
Link to this sectionFAQ#
Link to this section特定のタスクに合わせて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ロス関数の変更やコールバックの追加など、さらなるカスタマイズについては、Callbacks Guideを参照してください。
Link to this sectionUltralytics YOLOにおけるBaseTrainerの主要なコンポーネントは何ですか?#
BaseTrainerはトレーニングルーチンの基盤であり、その汎用メソッドをオーバーライドすることで様々なタスクにカスタマイズ可能です。主なコンポーネントは以下の通りです:
get_model(cfg, weights): トレーニングするモデルを構築します。get_dataloader(): データローダーを構築します。preprocess_batch(): モデルのフォワードパスの前にバッチの事前処理を処理します。set_model_attributes(): データセット情報に基づいてモデル属性を設定します。get_validator(): モデル評価用のバリデーターを返します。
カスタマイズの詳細やソースコードについては、BaseTrainer Referenceを参照してください。
Link to this sectionUltralytics 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 Guideを参照してください。
Link to this sectionモデルトレーニングにUltralytics YOLOを使用すべき理由は何ですか?#
Ultralytics YOLOは強力なエンジンエグゼキュータに対する高度な抽象化を提供しており、迅速な開発とカスタマイズに最適です。主な利点は以下の通りです:
- 使いやすさ: コマンドラインとPythonの両インターフェースが複雑なタスクを簡素化します。
- パフォーマンス: リアルタイムの物体検出や様々なビジョンAIアプリケーション向けに最適化されています。
- カスタマイズ性: カスタムモデル、ロス関数、データローダーに対して容易に拡張可能です。
- モジュール性: パイプライン全体に影響を与えることなく、コンポーネントを独立して修正できます。
- 統合性: MLエコシステム内の主要なフレームワークやツールとシームレスに連携します。
YOLOの機能の詳細については、主要なUltralytics YOLOページを参照してください。
Link to this section標準的でないモデルに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を確認してください。