ML-Agents Release 22 の公式ドキュメントを読む
ML-Agents Release 22 の公式ドキュメントを読んでまとめます。
(環境構築からサンプルプログラムの 3DBall の実行までのやり方はこちら->
https://zenn.dev/15bluefortran/articles/60a37b46e2cd88 にまとめてあります)
この記事では公式ドキュメント(https://unity-technologies.github.io/ml-agents/Getting-Started/)による Unity 環境でのサンプルエージェントを訓練し、学習済みモデルをUnity環境に組み込むまでの手順を説明をまとめています。
環境の準備
Open the Package Manager Window by navigating to Window -> Package Manager in the menu. Navigate to the ML-Agents Package and click on it. Find the 3D Ball sample and click Import.
UnityのパッケージマネージャーからML-Agentsパッケージをインポートし、3D Ballサンプルを読み込みます。
In the Project window, go to the Assets/ML-Agents/Examples/3DBall/Scenes folder and open the 3DBall scene file.
Projectウィンドウから3DBallシーンファイルを開きます。
Unity環境の理解
An agent is an autonomous actor that observes and interacts with an environment.
Agent は、環境を観察し、相互作用して自律的に行動します。
Behavior Parameters — Every Agent must have a Behavior. The Behavior determines how an Agent makes decisions.
Behavior Parameters は Agent の行動を決定します。
Behavior Parameters : Vector Observation Space ... a Space Size of 8. ... the x and z components of the agent cube's rotation and the x, y, and z components of the ball's relative position and velocity.
Vector Observation Spaceは、 Agent が意思決定を行うための情報を浮動小数点数のベクトルとして定義します(3DBallでは8つの要素)。
Behavior Parameters : Actions ... continuous actions ... a Space Size of 2 to control the amount of x and z rotations...
Actions は Agent への指示であり、連続値または離散値で表されます(3DBallでは2つの連続値)。
学習済みモデルの使用
In the Project window, drag the 3DBall Model located in Assets/ML-Agents/Examples/3DBall/TFModels into the Model property under Behavior Parameters (Script) component in the Agent GameObject Inspector window.
学習済みモデル(.onnxファイル)をAgent GameObjectのBehavior ParametersのModelプロパティにドラッグ&ドロップします。
Set the Inference Device to use for this model as CPU.
Inference Device をCPUに設定します
Click the Play button in the Unity Editor and you will see the platforms balance the balls using the pre-trained model.
Unity Editorの再生ボタンを押すと、学習済みモデルを使ってボールのバランスを取る様子を確認できます。
新しいモデルの訓練
Run mlagents-learn config/ppo/3DBall.yaml --run-id=first3DBallRun.
コマンドラインからmlagents-learnコマンドを実行し、トレーニングを開始します。config/ppo/3DBall.yamlは設定ファイル、--run-idはトレーニングセッションを識別するためのIDです。
mlagents-learn config/ppo/3DBall.yaml --run-id=first3DBallRun
If you've quit the training early using Ctrl+C and want to resume training, run the same command again, appending the --resume flag:
mlagents-learn config/ppo/3DBall.yaml --run-id=first3DBallRun --resume
トレーニングを Ctrl+C で途中で停止した場合は --resume フラグを追加して再開します。
mlagents-learn config/ppo/3DBall.yaml --run-id=first3DBallRun --resume
When the message "Start training by pressing the Play button in the Unity Editor" is displayed on the screen, you can press the Play button in Unity to start training in the Editor.
Unity Editorの再生ボタンを押して、トレーニングを開始します。
Note how the Mean Reward value printed to the screen increases as training progresses.
トレーニングが進むにつれて、Mean Reward(平均報酬)の値が増加することを確認します。
In order to observe the training process in more detail, you can use TensorBoard. From the command line run:
tensorboard --logdir results
TensorBoardを使用して、トレーニングの進行状況を視覚的に確認できます。
tensorboard --logdir results
学習済みモデルの埋め込み
Move your model file into Project/Assets/ML-Agents/Examples/3DBall/TFModels/.
トレーニングによって生成されたモデルファイル(.onnx)をProject/Assets/ML-Agents/Examples/3DBall/TFModels/に移動します。
Drag the <behavior_name>.onnx file from the Project window of the Editor to the Model placeholder in the Ball3DAgent inspector window.
学習済みモデルを再度、Modelにドラッグ&ドロップします。
Press the Play button at the top of the Editor.
Unity Editorの再生ボタンを押して、学習済みモデルが動作することを確認します。
Discussion