1. Create a virtual environment
Before starting a Unity AI project, you first need to set up an appropriate virtual environment. The following steps will guide you on how to create:
- Download Python 3.7.
- Download Anaconda 2020.11. Mirror resources can be found at the Tsinghua University open source software mirror station: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
- Use Anaconda to create a new environment named ml-agents. If you are using the command line, you can use the following command to create:
conda create --name ml-agents python=3.8
- Enter in Anaconda Prompt
to activate the environment you just created.activate ml-agents
- Install tensorflow 2.2.0, command:
python -m pip install tensorflow==2.2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
- Install torch 1.7.1, command:
pip install torch==1.7.1 -i https://pypi.tuna.tsinghua.edu.cn/simple
- Install ml-agents, command:
(Note: Please ensure that the VPN is not enabled during the installation process, otherwise the installation may fail)pip install mlagents -i https://pypi.tuna.tsinghua.edu.cn/simple
- If an error is reported during training, you may need to downgrade the protobuf version. Command:
pip install protobuf==3.20.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
2. Training
After setting up the environment, you can start training your Unity AI project:
- Enter in Anaconda Prompt
to activate your environment.activate ml-agents
- enter
Switch to drive E. (Steps 2 and 3 need to be determined according to your own project)E:
- pass
Go to the training directory in your project.cd E:\UnityProjects\TestAI\Assets\Train
- enter
Start training.mlagents-learn config.yaml
- If you need to resume previous training, you can enter
。mlagents-learn config.yaml --resume
3. Ensure version correspondence
Make sure your Unity version matches the version installed on pip. You can view the corresponding relationship in the Releases section of https://github.com/Unity-Technologies/ml-agents/releases/tag/release_20 . For example, the ML-Agents Release 4 version I use is paired as follows:
- com.unity.ml-agents (C#):v1.2.0
- mlagents (Python):v0.18.0
- mlagents-envs (Python):v0.18.0
- gym-unity (Python):v0.18.0
- Communicator (C#/Python):v1.0.0
To check the version, follow these steps:
- Check the ML-Agents version of your Unity project: In the Unity Editor, open Window > Package Manager. In the window that opens, find the ML-Agents package and view its version number.
- Check the ML-Agents version of the training environment: In the command line terminal, run the following command:
pip show mlagents
- Update the command line tools MLAgents and mlagents-envs to the corresponding versions. The commands are as follows:
-
pip install --upgrade mlagents==0.18.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
-
pip install --upgrade mlagents-envs==0.18.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
-
Reference: https://zhuanlan.zhihu.com/p/82617680