高度なカスタマイズ
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):
...
trainer = CustomTrainer(overrides={...})
trainer.train()
あなたは今、トレーナーをさらにカスタマイズする必要があることに気づいた:
- をカスタマイズする
loss function
. - 追加
callback
Googleドライブにモデルをアップロードする。epochs
その方法はこうだ:
from ultralytics.models.yolo.detect import DetectionTrainer
from ultralytics.nn.tasks import DetectionModel
class MyCustomModel(DetectionModel):
def init_criterion(self):
...
class CustomTrainer(DetectionTrainer):
def get_model(self, cfg, weights):
return MyCustomModel(...)
# callback to upload model weights
def log_model(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
.これらの詳細については、「参考文献」のセクションを参照のこと。