[Unity] I tried 3DBall sample of Unity “ML-Agents” (Tried Machine Learning)

Regarding ML-Agents which is Unity plugin to realize Machine Learning environment in Unity, in the last 2 times, I explained how to set up 3Dball which is sample of ML-Agents using sample learned model.

[Link: I tried 3DBall sample of Unity “ML-Agents” (Using sample learned model) Part1]

[Link: I tried 3DBall sample of Unity “ML-Agents” (Using sample learned model) Part2]

This time, I’ll explain how to run Machine Learning itself for 3Dball sample in Unity. (Beforehand you need to set up the setting explained last 2 times.)

[Overview of the operation steps (Machine Learning)]

4. Set up Unity to make machine to learn
4.1. Set up in Unity
4.2. Build in Unity

5. Perform Machine Learning
5.1. Prepare for Machine Learning
5.2. Make machine to learn

6. Run 3DBall with my learned model
6.1. Set up to use my learned model
6.2. Run 3DBall with my learned model

 

[4. Set up Unity to make machine to learn]

4.1. Set up in Unity

Open ‘Player Settings’ screen by ‘Edit > Project Settings > Player’.

Confirm following items in ‘Resolution and Presentation’ area.

  • [Run In Background] : To be checked
  • [Display Resolution Dialog] : To be ‘Disabled’

Next, click ‘Ball3DBrain’ game object in Hierarchy view, and change ‘Brain Type’ to be ‘External’ in Inspector view.

Here, save the scene by ‘File > Save Scenes’.

 

4.2. Build in Unity

Open ‘Build Settings’ screen by ‘File > Build Settings’.
This time, build for ‘PC, Mac & Linux Standalone’ platform. Click ’Build’ button, and start to build the program.

System may ask a location to save file, you need to chose a folder under ‘ml-agents-master > python’. Here I put ‘3DBall’ as the file name. Click ‘Save’ button, and start the building.

One exe file named ‘3DBall’ was created under ‘ml-agents-master > python’.

(From ml-agents version 0.4, there is a way to run machine learning on Unity editor directly instead of building the program beforehand like introducing here.)

 

[5. Perform Machine Learning]

5.1. Prepare for Machine Learning

For a person already closed ‘Anaconda Prompt’ last time used, I’ll explain about the explanation quickly here. Open ‘Anaconda Prompt’ screen.

Input ‘chcp 65001’ in command prompt, and press the Enter key.

chcp 65001

Move the current folder to ‘ml-agents-master’ folder.

cd <paste folder path>

And change the environment for ml-agents.

activate ml-agents

Move the current folder to ‘python’ folder.

cd python

 

5.2. Make machine to learn

Run python learning program ‘learn.py’ indicating exe file (3DBall) which was created just now in the same folder. (Until ml-agents v0.2, it used ppo.py as python learning program.)

python learn.py 3DBall –train

Started Machine Learning. A small screen opened, and 12 agents are trying to play 3DBall game with very high speed.

In the beginning, they’re playing very poorly.

Proceeding the learning by the machine.

Becoming the machine to be able to balance the balls very well.

After the learning finished, the result was saved in ‘ml-agents-master > python > models > ppo’ folder as 3DBall_ppo.bytes.

 

[6. Run 3DBall with my learned model]

6.1. Set up to use my learned model

Let’s try to run 3DBall with learned model (bytes file) just now created.
Firstly, drag and drop the ‘3DBall_ppo.bytes’ file just now created to ‘TFModels’ folder in Project view.

Next, click ‘Ball3DBrain’ game object in Hierarchy view, and change some settings in Inspector view like following.

  • [Brain Type] : Change ‘Internal’
  • [Graph Model] : Drag and drop the ‘3DBall_ppo.bytes’ file from Project view

 

6.2. Run 3DBall with my learned model

Run the 3DBall game on Unity editor. Automatically playing with balancing very well.

 

The Machine Learning is performed on Unity like this. I think this 3DBall sample is very easily understandable, and have visual impact. It’s very good sample to understand Machine Learning for Unity.
This Unity’s ML-Agents has many other machine learning samples, I’ll also try the others next time.

 

[Original Japanese Site: http://blog.lab7.biz/archives/11191646.html]

 

Leave a Reply

Your email address will not be published. Required fields are marked *