9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DockerでUnity ML-Agentsを動䜜させる(v0.6.0察応)

9
Last updated at Posted at 2018-12-26

远蚘

v0.9.1で動䜜させる方法をたずめたした。

DockerでUnity ML-Agentsを動䜜させる(v0.9.1察応) - Qiita
https://qiita.com/kai_kou/items/0c2f3c7d22363fd91e4e

はじめに

以前、v0.5.0で動䜜させる方法をたずめたしたが、v0.6.0がリリヌスされたので、改めおUnity ML-Agents(v0.6.0)をDocker䞊で動䜜させおみたした。

v0.5.0で動䜜させたいずいう方は䞋蚘をご参考ください。

DockerでUnity ML-Agentsを動䜜させる(v0.5.0察応) - Qiita
https://qiita.com/kai_kou/items/df3f3518a5a27b66f617

UnityやUnity ML-Agentsの環境構築などは䞋蚘をご参考ください。

Macでhomebrewを䜿っおUnityをむンストヌルする(Unity Hub、日本語化察応)
https://qiita.com/kai_kou/items/445e614fb71f2204e033

MacでUnity ML-Agentsの環境を構築する(v0.6.0察応) - Qiita
https://qiita.com/kai_kou/items/2a6545d1f9d83178d0c0

手順

基本的には公匏のドキュメントに沿えばよい感じです。

Using Docker For ML-Agents
https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Using-Docker.md

Dockerのむンストヌル

Dockerがむンストヌルされおいない堎合、むンストヌルしたす。

> brew cask install docker
(略)

> docker --version
Docker version 18.09.0, build 4d60db4

> open /Applications/Docker.app

Dockerを初回起動するず初期蚭定のためにパスワヌド入力が求められたす。

UnityにLinuxビルドサポヌトコンポヌネントを远加する

  • Unity Hubアプリを起動する
  • [Installs] > [On my machine]からUnityリスト右偎にある[...]から[Add Component]を遞択する
  • [Add components your install]ダむアログの[Platforms]にある[Linux Build Support]にチェックを入れお[Done]ボタンをクリックする
Unity Hub Add components to your install

孊習甚のappをビルドする

ML-Agentsリポゞトリをダりンロヌド

適圓なディレクトリにリポゞトリをダりンロヌドする。

> mkdir 適圓なディレクトリ
> cd 適圓なディレクトリ
> git clone https://github.com/Unity-Technologies/ml-agents.git
  • Unityを起動しお、[open]からダりンロヌドしおきたリポゞトリにあるml-agents/ml-agents/UnitySDKフォルダを開く

Unity゚ディタのバヌゞョンによっお、再むンポヌトするか、確認ダむアログが立ち䞊がりたす。
UnitySDK.png

「Continue(続行)」ボタンをクリックしお進めたす。

再むンポヌト凊理するず初回、プロゞェクトが開くたでに時間がかかりたす。

スクリヌンショット 2018-09-13 10.50.01.png

起動したした。

今回は、サンプルずしお含たれおいる[3DBall]Scenesを利甚したす。

  • Unityの䞋パネルにある[Project]タブから以䞋のフォルダたで開く
    • [Assets] > [ML-Agents] > [Examples] > [3DBall] > [Scenes]
  • 開いたら、[3DBall]ファむルがあるので、ダブルクリックしお開く
スクリヌンショット 2018-07-28 13.32.12.png

Scenes(シヌン)の蚭定

  • Unityアプリの[Edit]メニュヌから[Project Settings] > [Player]を開く
孊習のための蚭定
  • Unityアプリの右偎にある[Inspector]ビュヌで以䞋の蚭定を確認する
    • [Resolution and Presentation]の[Run In Background]がチェックされおいる
    • [Display Resolution Dialog]がDisableになっおいる
ResolutionAndPresentation.png
  • Unityアプリの[Hierarchy]ビュヌから[Ball3DAcademy]を開く
    • Unityアプリの[Inspector]ビュヌで[Broadcast hub] > [3DBallLearning(LearningBrain)]暪にある**[Control]にチェックを入れる**
Unity_2018_2_7f1_Personal__64bit__-_3DBall_unity_-_UnitySDK_-_PC__Mac___Linux_Standalone__Personal___OpenGL_4_1_-3.png
  • [Ctrl] + [s]キヌでシヌンを保存する

蚭定倉曎埌、しっかりず保存しないずビルド時に蚭定が反映されなくおハマりたす。

Prefabs(プレハブ)の蚭定

  • Unityアプリの䞋にある[Project]タブから以䞋のフォルダたで開く
    • [Assets] > [ML-Agents] > [Examples] > [3DBall] > [Prefabs]
  • Prefabsフォルダ内の[Game]の[▶]ボタンをクリックしお展開し、[Pratform]を遞択する
  • Unityアプリの右偎にある[Insoector]パネルのBall 3D Agent(Script)にあるBrainを[3DBallLearning(LearningBrain)]に以䞋の手順で倉曎する
    • [Project]タブから以䞋のフォルダたで開き、
      • [Assets] > [ML-Agents] > [Examples] > [3DBall] > [Brains]
    • [3DBallLearning]を[Insoector]パネルのBrainにドラッグ&ドロップする
Unity_2018_2_7f1_Personal__64bit__-_3DBall_unity_-_UnitySDK_-_PC__Mac___Linux_Standalone__Personal___OpenGL_4_1_.png Unity_2018_2_7f1_Personal__64bit__-_3DBall_unity_-_UnitySDK_-_PC__Mac___Linux_Standalone__Personal___OpenGL_4_1_-2.png - [Ctrl] + [s]キヌでシヌンを保存する - 蚭定倉曎埌、しっかりず保存しないず、ビルド時に蚭定が反映されなくおハマりたす。

ビルド蚭定

  • Unityアプリの[File]メニュヌから[Build Settings]を遞択する
  • [Build Settings]ダむアログで[Platform]でPC, Mac & Linux Standaloneが遞択されおいるこずを確認する
  • [Target Platform]をLinuxに倉曎する
  • [Architecture]をx86_64に倉曎する
  • [Headless Mode]にチェックを入れる
  • [Build Settings]ダむアログで[Build]ボタンをクリックする
  • ファむル保存ダむアログで以䞋を指定しおビルドを開始する
    • ファむル名: 3DBall
    • フォルダ名: 任意のディレクトリ/ml-agents\unity-volume
スクリヌンショット 2018-12-25 11.51.22.png

するず、unity-volumeに以䞋フォルダ・ファむルが出力されたす。

> ls 任意のディレクトリ/ml-agents\unity-volume
3DBall.x86_64 3DBall_Data

ハむパヌパラメヌタヌの甚意

ハむパヌパラメヌタヌファむルをunity-volumeフォルダにコピヌしおおきたす。

> cp config/trainer_config.yaml unity-volume

Dockerコンテナを構築する

Dockerが起動しおいるこずを確認しおから、ml-agentsリポゞトリ盎䞋でdockerコンテナを構築する。
すでにDockerfileが甚意されおいるので、buildするだけ。楜々ですね。

> cd 任意のディレクトリ/ml-agents
> docker build -t 3dball_ml_docker .
(略)
Successfully built db0b82f60411
Successfully tagged 3dball_ml_docker:latest

Dockerコンテナの実行

Dockerコンテナが構築できたら実行しおみたす。

bash
# unity-ml-docker-3dball: コンテナ名(任意)
# 3DBall: Unityでbuild時に付けたアプリの名前(拡匵子なし)
# 3dball_ml_docker: Dockerでbuild時に付けた名前
# docker_first_run: 機械孊習結果を保存する際の名称(任意)

> docker run --name unity-ml-docker-3dball \
  --mount type=bind,source="$(pwd)"/unity-volume,target=/unity-volume \
  -p 5005:5005 \
  3dball_ml_docker:latest \
  trainer_config.yaml \
  --docker-target-name=unity-volume \
  --env=3DBall \
  --train \
  --run-id=docker_first_run

fishシェルで実行する堎合は、"$(pwd)"を"$PWD"に眮き換えたす。

fishの堎合
> docker run --name unity-ml-docker-3dball \
  --mount type=bind,source="$PWD"/unity-volume,target=/unity-volume \
  -p 5005:5005 \
  3dball_ml_docker:latest \
  trainer_config.yaml \
  --docker-target-name=unity-volume \
  --env=3DBall \
  --train \
  --run-id=docker_first_run

Unity ML-Agents公匏にあるdocker run コマンドがおそらく動䜜怜蚌しおなくお、パラメヌタ指定゚ラヌずなりたす。以䞋は`mlagents-learn コマンドのヘルプです。
Dockerで実行する堎合には、Dockerむメヌゞ名3dball_ml_docker:latest の埌からが、```mlagents-learn``` のパラメヌタ指定になりたす。

mlagents-learn help

    Usage:
      mlagents-learn <trainer-config-path> [options]
      mlagents-learn --help

    Options:
      --env=<file>               Name of the Unity executable [default: None].
      --curriculum=<directory>   Curriculum json directory for environment [default: None].
      --keep-checkpoints=<n>     How many model checkpoints to keep [default: 5].
      --lesson=<n>               Start learning from this lesson [default: 0].
      --load                     Whether to load the model or randomly initialize [default: False].
      --run-id=<path>            The directory name for model and summary statistics [default: ppo].
      --num-runs=<n>             Number of concurrent training sessions [default: 1].
      --save-freq=<n>            Frequency at which to save model [default: 50000].
      --seed=<n>                 Random seed used for training [default: -1].
      --slow                     Whether to run the game at training speed [default: False].
      --train                    Whether to train model, or only run inference [default: False].
      --worker-id=<n>            Number to add to communication port (5005) [default: 0].
      --docker-target-name=<dt>  Docker volume to store training-specific files [default: None].
      --no-graphics              Whether to run the environment in no-graphics mode [default: False].

実行するず、孊習が始たりたす。
trainer_config.yamlのmax_steps で指定されおいるステップ数が完了するか、ctrl+cキヌで孊習が終了したす。

> docker run (略)

INFO:mlagents.trainers:{'--curriculum': 'None',
 '--docker-target-name': 'unity-volume',
 '--env': '3DBall',
 '--help': False,
 '--keep-checkpoints': '5',
 '--lesson': '0',
 '--load': False,
 '--no-graphics': False,
 '--num-runs': '1',
 '--run-id': 'docker_first_run',
 '--save-freq': '50000',
 '--seed': '-1',
 '--slow': False,
 '--train': True,
 '--worker-id': '0',
 '<trainer-config-path>': 'trainer_config.yaml'}


                        ▄▄▄▓▓▓▓
                   ╓▓▓▓▓▓▓█▓▓▓▓▓
              ,▄▄▄m▀▀▀'  ,▓▓▓▀▓▓▄                           ▓▓▓  ▓▓▌
            ▄▓▓▓▀'      ▄▓▓▀  ▓▓▓      ▄▄     ▄▄ ,▄▄ ▄▄▄▄   ,▄▄ ▄▓▓▌▄ ▄▄▄    ,▄▄
          ▄▓▓▓▀        ▄▓▓▀   ▐▓▓▌     ▓▓▌   ▐▓▓ ▐▓▓▓▀▀▀▓▓▌ ▓▓▓ ▀▓▓▌▀ ^▓▓▌  ╒▓▓▌
        ▄▓▓▓▓▓▄▄▄▄▄▄▄▄▓▓▓      ▓▀      ▓▓▌   ▐▓▓ ▐▓▓    ▓▓▓ ▓▓▓  ▓▓▌   ▐▓▓▄ ▓▓▌
        ▀▓▓▓▓▀▀▀▀▀▀▀▀▀▀▓▓▄     ▓▓      ▓▓▌   ▐▓▓ ▐▓▓    ▓▓▓ ▓▓▓  ▓▓▌    ▐▓▓▐▓▓
          ^█▓▓▓        ▀▓▓▄   ▐▓▓▌     ▓▓▓▓▄▓▓▓▓ ▐▓▓    ▓▓▓ ▓▓▓  ▓▓▓▄    ▓▓▓▓`
            '▀▓▓▓▄      ^▓▓▓  ▓▓▓       └▀▀▀▀ ▀▀ ^▀▀    `▀▀ `▀▀   '▀▀    ▐▓▓▌
               ▀▀▀▀▓▄▄▄   ▓▓▓▓▓▓,                                      ▓▓▓▓▀
                   `▀█▓▓▓▓▓▓▓▓▓▌
                        ¬`▀▀▀█▓


E1225 03:40:19.381223700      14 server_chttp2.cc:40]        {"created":"@1545709219.381184700","description":"Only 1 addresses added out of total 2 resolved","file":"src/core/ext/transport/chttp2/server/chttp2_server.cc","file_line":316,"referenced_errors":[{"created":"@1545709219.381167100","description":"OS Error","errno":97,"file":"src/core/lib/iomgr/socket_utils_common_posix.cc","file_line":259,"os_error":"Address family not supported by protocol","syscall":"socket","target_address":"[::1]:5005"}]}
INFO:mlagents.envs:
'Ball3DAcademy' started successfully!
Unity Academy name: Ball3DAcademy
        Number of Brains: 1
        Number of Training Brains : 1
        Reset Parameters :

Unity brain name: 3DBallLearning
        Number of Visual Observations (per agent): 0
        Vector Observation space size (per agent): 8
        Number of stacked Vector Observation: 1
        Vector Action space type: continuous
        Vector Action space size (per agent): [2]
        Vector Action descriptions: ,
2018-12-25 03:40:21.048460: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
INFO:mlagents.envs:Hyperparameters for the PPO Trainer of brain 3DBallLearning:
        batch_size:     64
        beta:   0.001
        buffer_size:    12000
        epsilon:        0.2
        gamma:  0.995
        hidden_units:   128
        lambd:  0.99
        learning_rate:  0.0003
        max_steps:      5.0e4
        normalize:      True
        num_epoch:      3
        num_layers:     2
        time_horizon:   1000
        sequence_length:        64
        summary_freq:   1000
        use_recurrent:  False
        summary_path:   /unity-volume/summaries/docker_first_run-0_3DBallLearning
        memory_size:    256
        use_curiosity:  True
        curiosity_strength:     0.01
        curiosity_enc_size:     128
        model_path:     /unity-volume/models/docker_first_run-0/3DBallLearning
INFO:mlagents.trainers: docker_first_run-0: 3DBallLearning: Step: 1000. Mean Reward: 1.266. Std of Reward: 0.732. Training.
(略)

孊習結果をアプリに組み蟌む

孊習結果は、ml-agents/unity-volume フォルダ内に保存されたす。

TensorFlowSharpプラグむンのむンポヌト

孊習結果を組み蟌むのに必芁ずなるプラグむンのむンポヌトず蚭定を行いたす。
TensorFlowSharpプラグむンをダりンロヌドしおむンポヌトしたす。

プラグむンをダりンロヌドしおファむルを開くず、UnityでImport確認ダむアログが衚瀺されたす。[Import]ボタンをクリックしおむンポヌトしたす。こちらはv0.5.0から倉わりないようです。
スクリヌンショット 2018-12-20 14.30.44.png

プラグむンがむンポヌトできたら、再床Playerの蚭定を行いたす。

  • Unityアプリの[Edit]メニュヌから[Project Settings] > [Player]を遞択する
  • [Inspector]ビュヌの[Other Settings]欄で以䞋を蚭定する
    • [Scripting Runtime Version]を[.Net 4.x Equivalent]にする
    • [Scripting Defined Symbols]にENABLE_TENSORFLOW を入力する
    • [Allow 'unsafe' Code]にチェックを入れる
スクリヌンショット 2018-12-20 14.36.01.png

[Allow 'unsafe' Code]にチェックを入れないず゚ラヌがでお実行できないのでご泚意ください。

蚭定倉曎したらUnityアプリの[File] > [Save Project]で保存しお、Unityアプリを再起動したす。

孊習結果ファむルの取り蟌み

タヌミナルかFinderで孊習結果を以䞋フォルダにコピヌしたす。

  • 孊習結果ファむル: ml-agents/unity-volume/models/docker_first_run-0/3DBallLearnig.bytes
  • 保存先: UnitySDK/Assets/ML-Agents/Examples/3DBall/TFModels/

※すでに保存先に3DBallLearnig.bytes ファむルが存圚しおいたすので、リネヌムしおください。

> cp ml-agents/unity-volume/models/docker_first_run-0/3DBallLearnig.bytes ../UnitySDK/Assets/ML-Agents/Examples/3DBall/TFModels
  • Unityアプリの[Project]パネルで以䞋ファむルを遞択する
    • [Assets] > [ML-Agents] > [Examples] > [3DBall] > [Brains] > [3DBallLearning]
  • Unityアプリの[Project]パネルで以䞋フォルダを遞択する
    • [Assets] > [ML-Agents] > [Examples] > [3DBall] > [TFModels]
  • Unityアプリの[Inspector]パネルにある[Model]ずいう項目に[TFModels]フォルダ内の3DBallLearning.bytesファむルをドラッグドロップする
スクリヌンショット_2018-12-21_10_21_10.png - Unityアプリの[Hierarchy]パネルから以䞋を遞択する - [3DBall] > [Ball3DAcademy] - Unityアプリの[Inspector]パネルにある[Brains] > [3DBallLearning(LearningBrain)]暪の**[Control]のチェックを倖す** - Unity䞊郚にある[▶]ボタンをクリックする

これで、孊習結果が組み蟌たれた状態でアプリが起動したす。

スクリヌンショット 2018-09-13 12.05.04.png

参考

Using Docker For ML-Agents
https://github.com/Unity-Technologies/ml-agents/blob/master/docs/Using-Docker.md

DockerでUnity ML-Agentsを動䜜させる(v0.5.0察応) - Qiita
https://qiita.com/kai_kou/items/df3f3518a5a27b66f617

Docker for Macをむンストヌルしおみた
https://qiita.com/scrummasudar/items/750aa52f4e0e747eed68

Macでhomebrewを䜿っおUnityをむンストヌルする(Unity Hub、日本語化察応)
https://qiita.com/kai_kou/items/445e614fb71f2204e033

MacでUnity ML-Agentsの環境を構築する(v0.6.0察応) - Qiita https://qiita.com/kai_kou/items/2a6545d1f9d83178d0c0

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?