コンテンツへスキップ

Efficient Hyperparameter Tuning with Ray Tune and YOLO11

ハイパーパラメーターの調整は、ハイパーパラメーターの最適なセットを検出することで、モデルのパフォーマンスのピークを達成するために不可欠です。これには、さまざまなハイパーパラメーターで試行を実行し、各試行のパフォーマンスを評価することが含まれます。

Accelerate Tuning with Ultralytics YOLO11 and Ray Tune

Ultralytics YOLO11 incorporates Ray Tune for hyperparameter tuning, streamlining the optimization of YOLO11 model hyperparameters. With Ray Tune, you can utilize advanced search strategies, parallelism, and early stopping to expedite the tuning process.

レイ・チューン

レイ・チューンの概要

Ray Tune is a hyperparameter tuning library designed for efficiency and flexibility. It supports various search strategies, parallelism, and early stopping strategies, and seamlessly integrates with popular machine learning frameworks, including Ultralytics YOLO11.

との統合Weights & Biases

YOLO11 also allows optional integration with Weights & Biases for monitoring the tuning process.

インストール

必要なパッケージをインストールするには

インストール

# Install and update Ultralytics and Ray Tune packages
pip install -U ultralytics "ray[tune]"

# Optionally install W&B for logging
pip install wandb

使用方法

使用方法

from ultralytics import YOLO

# Load a YOLO11n model
model = YOLO("yolo11n.pt")

# Start tuning hyperparameters for YOLO11n training on the COCO8 dataset
result_grid = model.tune(data="coco8.yaml", use_ray=True)

tune() メソッド・パラメーター

について tune() method in YOLO11 provides an easy-to-use interface for hyperparameter tuning with Ray Tune. It accepts several arguments that allow you to customize the tuning process. Below is a detailed explanation of each parameter:

パラメータタイプ説明デフォルト値
datastrThe dataset configuration file (in YAML format) to run the tuner on. This file should specify the training and validation data paths, as well as other dataset-specific settings.
spacedict, optionalA dictionary defining the hyperparameter search space for Ray Tune. Each key corresponds to a hyperparameter name, and the value specifies the range of values to explore during tuning. If not provided, YOLO11 uses a default search space with various hyperparameters.
grace_periodint, optionalThe grace period in epochs for the ASHA scheduler in Ray Tune. The scheduler will not terminate any trial before this number of epochs, allowing the model to have some minimum training before making a decision on early stopping.10
gpu_per_trialint, optionalチューニング中にトライアルごとに割り当てるGPUの数。これは、特にマルチGPU 環境で、GPU の使用を管理するのに役立ちます。指定しない場合、チューナーは利用可能なすべての GPU を使用します。なし
iterationsint, optionalチューニング中に実行する試行の最大数。このパラメータは、テストされるハイパーパラメータの組み合わせの総数を制御するのに役立ち、チューニングプロセスが無限に実行されないようにします。10
**train_argsdict, optionalに渡す追加引数。 train() method during tuning. These arguments can include settings like the number of training epochs, batch size, and other training-specific configurations.{}

これらのパラメータをカスタマイズすることで、特定のニーズと利用可能な計算リソースに合わせて、ハイパーパラメータ最適化プロセスを微調整することができます。

デフォルトの検索スペース

The following table lists the default search space parameters for hyperparameter tuning in YOLO11 with Ray Tune. Each parameter has a specific value range defined by tune.uniform().

パラメータ値の範囲説明
lr0tune.uniform(1e-5, 1e-1)Initial learning rate
lrftune.uniform(0.01, 1.0)最終学習率係数
momentumtune.uniform(0.6, 0.98)勢い
weight_decaytune.uniform(0.0, 0.001)体重減少
warmup_epochstune.uniform(0.0, 5.0)ウォームアップ・エポック
warmup_momentumtune.uniform(0.0, 0.95)ウォームアップの勢い
boxtune.uniform(0.02, 0.2)ボックスロス重量
clstune.uniform(0.2, 4.0)減量クラス
hsv_htune.uniform(0.0, 0.1)色相増強範囲
hsv_stune.uniform(0.0, 0.9)飽和増大範囲
hsv_vtune.uniform(0.0, 0.9)値(輝度)増強範囲
degreestune.uniform(0.0, 45.0)回転増大範囲(度)
translatetune.uniform(0.0, 0.9)翻訳増強範囲
scaletune.uniform(0.0, 0.9)補強範囲の拡大
sheartune.uniform(0.0, 10.0)剪断増大範囲(度)
perspectivetune.uniform(0.0, 0.001)パースペクティブ拡張範囲
flipudtune.uniform(0.0, 1.0)垂直フリップ増加確率
fliplrtune.uniform(0.0, 1.0)水平フリップ増加確率
mosaictune.uniform(0.0, 1.0)モザイク補強確率
mixuptune.uniform(0.0, 1.0)混合補強確率
copy_pastetune.uniform(0.0, 1.0)コピペ補強確率

カスタム検索スペースの例

In this example, we demonstrate how to use a custom search space for hyperparameter tuning with Ray Tune and YOLO11. By providing a custom search space, you can focus the tuning process on specific hyperparameters of interest.

使用方法

from ultralytics import YOLO

# Define a YOLO model
model = YOLO("yolo11n.pt")

# Run Ray Tune on the model
result_grid = model.tune(
    data="coco8.yaml",
    space={"lr0": tune.uniform(1e-5, 1e-1)},
    epochs=50,
    use_ray=True,
)

In the code snippet above, we create a YOLO model with the "yolo11n.pt" pretrained weights. Then, we call the tune() メソッドで、データセットの設定を "coco8.yaml "で指定する。初期学習率のためのカスタム探索空間を提供する。 lr0 キー "lr0 "と値 "lr0 "を持つ辞書を使う。 tune.uniform(1e-5, 1e-1).最後に、エポック数のような追加のトレーニング引数を、次のようにチューニングメソッドに直接渡します。 epochs=50.

レイ・チューンの結果を処理する

Ray Tune でハイパーパラメータのチューニング実験を行った後、得られた結果について様々な分析を行いたい場合があります。このガイドでは、これらの結果を処理・分析するための一般的なワークフローを紹介します。

ディレクトリからのチューニング実験結果の読み込み

を使ったチューニング実験を行った後、次のように述べた。 tuner.fit()ディレクトリから結果をロードできます。これは、特に最初のトレーニングスクリプトが終了した後に分析を実行する場合に便利です。

experiment_path = f"{storage_path}/{exp_name}"
print(f"Loading results from {experiment_path}...")

restored_tuner = tune.Tuner.restore(experiment_path, trainable=train_mnist)
result_grid = restored_tuner.get_results()

基本的な実験レベルの分析

トライアルの結果の概要を把握できます。トライアル中にエラーがなかったかどうか、すぐに確認することができます。

if result_grid.errors:
    print("One or more trials failed!")
else:
    print("No errors!")

基本的なトライアルレベルの分析

個々のトライアルのハイパーパラメータ構成と、最後に報告されたメトリクスにアクセスします。

for i, result in enumerate(result_grid):
    print(f"Trial #{i}: Configuration: {result.config}, Last Reported Metrics: {result.metrics}")

臨床試験で報告されたメトリクスの全履歴をプロットする

各試験で報告されたメトリクスの履歴をプロットして、メトリクスの経時変化を見ることができます。

import matplotlib.pyplot as plt

for i, result in enumerate(result_grid):
    plt.plot(
        result.metrics_dataframe["training_iteration"],
        result.metrics_dataframe["mean_accuracy"],
        label=f"Trial {i}",
    )

plt.xlabel("Training Iterations")
plt.ylabel("Mean Accuracy")
plt.legend()
plt.show()

概要

このドキュメントでは、Ultralytics を使用して Ray Tune で実行された実験結果を解析するための一般的なワークフローについて説明します。主なステップには、ディレクトリからの実験結果のロード、基本的な実験レベルおよびトライアルレベルの解析の実行、メトリクスのプロットなどがあります。

ハイパーパラメーターのチューニング実験を最大限に活用するために、Ray Tuneの「Analyze Results(結果の分析)」ドキュメントページをさらに詳しくご覧ください。

よくあるご質問

How do I tune the hyperparameters of my YOLO11 model using Ray Tune?

To tune the hyperparameters of your Ultralytics YOLO11 model using Ray Tune, follow these steps:

  1. 必要なパッケージをインストールする:

    pip install -U ultralytics "ray[tune]"
    pip install wandb  # optional for logging
    
  2. Load your YOLO11 model and start tuning:

    from ultralytics import YOLO
    
    # Load a YOLO11 model
    model = YOLO("yolo11n.pt")
    
    # Start tuning with the COCO8 dataset
    result_grid = model.tune(data="coco8.yaml", use_ray=True)
    

これは、Ray Tuneの高度な検索ストラテジーと並列性を利用し、モデルのハイパーパラメータを効率的に最適化します。詳しくはRay Tuneのドキュメントをご覧ください。

What are the default hyperparameters for YOLO11 tuning with Ray Tune?

Ultralytics YOLO11 uses the following default hyperparameters for tuning with Ray Tune:

パラメータ値の範囲説明
lr0tune.uniform(1e-5, 1e-1)初期学習率
lrftune.uniform(0.01, 1.0)最終学習率係数
momentumtune.uniform(0.6, 0.98)勢い
weight_decaytune.uniform(0.0, 0.001)体重減少
warmup_epochstune.uniform(0.0, 5.0)ウォームアップ・エポック
boxtune.uniform(0.02, 0.2)ボックスロス重量
clstune.uniform(0.2, 4.0)減量クラス
hsv_htune.uniform(0.0, 0.1)色相増強範囲
translatetune.uniform(0.0, 0.9)翻訳増強範囲

これらのハイパーパラメータは、特定のニーズに合わせてカスタマイズすることができる。完全なリストと詳細については、ハイパーパラメータのチューニング・ガイドを参照してください。

How can I integrate Weights & Biases with my YOLO11 model tuning?

To integrate Weights & Biases (W&B) with your Ultralytics YOLO11 tuning process:

  1. W&Bを設置する:

    pip install wandb
    
  2. チューニングスクリプトを修正する:

    import wandb
    
    from ultralytics import YOLO
    
    wandb.init(project="YOLO-Tuning", entity="your-entity")
    
    # Load YOLO model
    model = YOLO("yolo11n.pt")
    
    # Tune hyperparameters
    result_grid = model.tune(data="coco8.yaml", use_ray=True)
    

このセットアップにより、チューニング・プロセスをモニターし、ハイパーパラメータの設定を追跡し、結果をW&Bで視覚化することができる。

Why should I use Ray Tune for hyperparameter optimization with YOLO11?

レイ・チューンは、ハイパーパラメータの最適化において多くの利点を提供する:

  • 高度な検索戦略:ベイズ最適化やHyperOptなどのアルゴリズムを活用し、効率的なパラメータ検索を実現。
  • 並列性:複数のトライアルの並列実行をサポートし、チューニングプロセスを大幅にスピードアップ。
  • 早期停止:ASHAのような戦略を採用し、パフォーマンスの低い試験を早期に終了させ、計算資源を節約する。

Ray Tune seamlessly integrates with Ultralytics YOLO11, providing an easy-to-use interface for tuning hyperparameters effectively. To get started, check out the Efficient Hyperparameter Tuning with Ray Tune and YOLO11 guide.

How can I define a custom search space for YOLO11 hyperparameter tuning?

To define a custom search space for your YOLO11 hyperparameter tuning with Ray Tune:

from ray import tune

from ultralytics import YOLO

model = YOLO("yolo11n.pt")
search_space = {"lr0": tune.uniform(1e-5, 1e-1), "momentum": tune.uniform(0.6, 0.98)}
result_grid = model.tune(data="coco8.yaml", space=search_space, use_ray=True)

これは、チューニング・プロセス中に探索する初期学習率や運動量のようなハイパーパラメータの範囲をカスタマイズする。高度な設定については、カスタム探索空間の例のセクションを参照してください。

📅 Created 11 months ago ✏️ Updated 22 days ago

コメント