Published Date : 2020年9月1日10:15

【Unity,ML-Agents】簡単な設定方法と学習方法
【Unity,ML-Agents】Simple configuration and learning methods


This blog has an English translation


YouTubeにアップした動画、「【Unity,ML-Agents】簡単な設定方法と学習方法」の補足説明の記事です。

Here's a little more about the 「【Unity,ML-Agents】Simple configuration and learning methods」 video I uploaded to YouTube.


目次

Table of Contents




① 動画の説明
① Video Description



Unityで強化学習をするためのML-Agentsの実行環境を整えていきます。

We will prepare the execution environment of ML-Agents for reinforcement learning in Unity.

前回CUDAバージョン11をインストールしましたが、

We installed CUDA version 11 last time,

ML-Agentsで使うPythonのTensorflowはCUDAバージョン10.1が必要なので、

but Python Tensorflow used by ML-Agents requires CUDA version 10.1,

再インストールします。やり方は簡単です。

so We will reinstall it. It's easy to do.

前回と同じようにcuDNNとCUDAツールキットをバージョン10.1でインストールし直して、

Reinstall and reconfigure the cuDNN and CUDA toolkits

パスを設定し直します。

with version 10.1 as before.

もし、またCUDAバージョン11が必要になったらパスを書き換えるだけで済みます。

If you need CUDA version 11 again, you just need to rewrite the path.

少しCUDAのサンプルを動かして遊んでみましょう。

Let's play with the CUDA sample a little bit.

適当なサンプルを選んで、Visual Studio2017を立ち上げます。

Start Visual Studio 2017 by selecting the appropriate samples.

ソリューションの再ターゲットを選択。

Select solution retargeting.

ビルドをして、実行するだけです。

Just build and run.

それではML-Agentsのサンプルを学習させてみましょう。

Let's take a look at the ML-Agents sample.

ML-AgentsをGithubからダウンロードしてください。

Download ML-Agents from Github.

コマンドプロンプトかpowerShellを開いてpip install mlagentsで必要なpythonモジュールをインストールします。

Open a command prompt or power shell and install the python modules required by pip install mlagents.

numpyは1.19.0より下のバージョンが必要なので、もし1.19.0以上であればダウングレードしてください。

Since numpy requires a version lower than 1.19. 0, downgrade it if it is 1.19. 0 or higher.

tensorflowは自動でインストールされますので、Pythonで動かせるかどうか、GPUが有効かどうか確かめます。

Since tensorflow is automatically installed, it will check if it can run in Python and if the GPU is enabled.

python -c "import tensorflow as tf; print(tf.__version__)"
python -c "from tensorflow.python.client import device_lib; print(device_lib.list_local_devices())"

GPU:0と表示されていればGPUでTensorflowが使えます。

If it says [GPU: 0] then Tensorflow works on the GPU.

それでは新しいUnityプロジェクトを作成してください。

Now create a new Unity project.

WindowタブからPackageManagerを選択。

Select PackageManager from the Window tab.

mlagentsのパッケージをインストールします。

Install the mlagents package.

ダウンロードしたML-Agentsのフォルダの中の[Project]の中の[Assets]の中の[ML-Agents]をシーン内にドラッグアンドドロップします。

Drag and drop [ML-Agents] from [Assets] in [Project] in the downloaded ML-Agents folder into the scene.

Examplesの中の好きなサンプルを動かして試してみてください。

Experiment with your favorite examples in [examples folder].

さて次はPythonのTensorflowを使ってモデルの学習を試してみましょう。

Now let's try learning the model with Python's Tensorflow.

コマンドプロンプトを立ち上げて、ダウンロードしたML-Agentsに移動します。

Launch a command prompt and navigate to the [ML-Agents folder] you downloaded.

学習の設定ファイルは[config]の中の[ppo]の中にサンプル名で複数存在してます。

There are several learning configuration files with sample names in [ppo] in [config].

設定ファイルの中身を書き換えることもできます。

You can also rewrite the contents of the configuration file.

あとは設定ファイルに対して、[mlagents-learn]コマンドを実行するだけです。

All you have to do is run the [mlagents-learn] command on the configuration file.

mlagents-learn config/ppo/Basic.yaml --run-id=mltest --train --force

idには学習モデルを格納するフォルダの名前を入力します。

For id, enter the name of the folder that contains the learning model.

コマンドが成功したら、Unityに戻ってプレイボタンを押します。

If the command succeeds, return to Unity and press the Play button.

すると学習が開始されます。

This starts learning.

学習が終わるとresultsフォルダの中に先ほど入力したID名のフォルダ内にモデルファイルが作成されます。

When you are finished, a model file is created in the results folder in the folder with the ID name you entered earlier.

そのモデルファイルをシーン内にドラックアンドドロップします。

Drag and drop the model file into the scene.

エージェントを選択して、モデルにその学習済みモデルファイルをアタッチします。

Select an agent to attach its learned model file to the model.

後はプレハブの項目の横にあるOverridesを選択、ApplyAllを選択して、プレイボタンを押すだけです。

All you have to do is select Overrides next to the prefab item, select ApplyAll, and press the Play button.



以上です。お疲れ様です。

That's all. Thank you for your hard work.





See You Next Page!