Unity ML-Agent+Anaconda机器学习环境搭建

本文总结了ML-Agent+Anaconda+pytorch的机器学习环境搭建过程,以及笔者在搭建过程中遇到了一些问题,在此做个记录,也希望能够提供一些帮助。

环境搭建

1.构建环境

conda create -n ML_Agents python=3.8

2.设置代理或切换国内镜像:

设置代理方法:

conda config --set proxy_servers.http http://127.0.0.1:7890
conda config --set proxy_servers.https http://127.0.0.1:7890

 

建议不要用国内镜像,实在没办法再用!!!使用镜像可能会出很多问题!!!

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ 

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ 

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/ 

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ 

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ 

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/ 

conda config --set show_channel_urls yes

anaconda镜像_anaconda下载地址_anaconda安装教程-阿里巴巴开源镜像站 (aliyun.com)

anaconda | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

如果要删除

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ 

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ 

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/ 

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ 

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ 

conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/ 

 

对应的配置文件为用户目录的根目录下的.condarc文件

 

3.激活(切换)ML-Agent环境 conda activate ML_Agents

4.使用anaconda进入ml-agent根目录中:cd D:\ml-agents-release_19

5.安装需要的2个包 ml-agents 和 ml-agents-envs

  1. pip install -e./ml-agents-envs
  2. pip install -e./ml-agents

经笔者本机环境测试,使用梯子仍然无法下载,需要按照第二步设置代理!!!

国内镜像:

pip3 install -e ./ml-agents-envs -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip3 install -e ./ml-agents -i https://pypi.tuna.tsinghua.edu.cn/simple/

访问国内镜像源去下载ml-agents-envs,报错

Please see the install instructions at:https://pillow.readthedocs.io/en/latest/installation.html

pillow库安装失败,需要手动安装pip install --upgrade Pillow 。我是直接使用Anaconda去安装虚拟环境的所需库的,因为真的很快。

安装ml-agents库又出错。

同样,使用Anaconda去安装报错的库。

很奇怪为什么会出现这样的问题。可能是我conda的环境变量没配置吗。

问我为什么要配置这么多。问我,我也不知道。

 

笔者后面发现我安装Anaconda是32位的,安装错了。重装后以上操作都很顺利,没有出现上面问题。

 

6.安装pytorch的stable1.12.1

Previous PyTorch Versions | PyTorch

cudatoolkit选择低于你显卡的cuda版本安装

推荐使用:

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

-c pytorch表示直接使用源站去下载,如果Anadonaca做了镜像,去掉这段代码。

如果不行,可尝试下面方案。

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3

torchaudio==0.12.1在国内镜像源里好像不存在,先不安装。

单独执行 conda install torchaudio==0.12.1 -c pytorch

其他方案:

或者使用:pip install torch~=1.7.1

对应的国内镜像pip3 install torch~=1.7.1 -f https://download.pytorch.org/whl/torch_stable.html -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
最好使用代理去直接下载源文件,国内镜像的存在总会莫名其妙出现问题。

7.检测pytorch及cuda套件是否安装成功

import torch
print(torch.cuda.is_available()) #cuda是否可用,返回为True表示可用

torch.cuda.device_count()#返回GPU的数量,一般大于0

torch.cuda.get_device_name(0)#返回gpu名字,设备索引默认从0开始

 

8.检测mlagent环境是否构建成功 mlagents-learn --help

出现报错,如图。图中给出了解决方案:下载3.20.x系列或更小的版本且>= 3.19.0版本

使用pip uninstall protobuf 卸载:

重新安装3.19.0 pip install protobuf==3.19.0

又出现报错:

Downloading protobuf-3.19.0-cp39-cp39-win_amd64.whl (895 kB)
---------------------------------------- 895.7/895.7 kB 94.9 kB/s eta 0:00:00
Installing collected packages: protobuf
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorboard 2.15.1 requires protobuf<4.24,>=3.19.6, but you have protobuf 3.19.0 which is incompatible.

要求版本:3.19.6<=protobuf<4.24

故卸载掉安装3.19.6pip install protobuf==3.19.06

成功

 

注意:Python 3.9.10 以上目前与mlagents-learn 套件不相容,会出现错误TypeError: Invalid first argument to `register()`. typing.Dict[mlagents.trainers.settings.RewardSignalType, mlagents.trainers.settings.RewardSignalSettings] is not a class.

 

conda安装或更新库出错,报错内容大概如下:

path collision for 'lib/site-packages/wheel/vendored/packaging/__pycache__/tags.cpython-38.pyc'.
This path already exists in the target prefix, and it won't be removed
by an uninstall action in this transaction. The path is one that conda
doesn't recognize. It may have been created by another package manager.

告知在conda包管理器中有缓存,路径冲突了。使用conda clean --all 直接把缓存清了。

 

原本以为到差不多了,又出现一个错:

AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

告知float属性在numpy 1.20中已经被弃用。

要解决这个问题,一种解决方案是用 Python 的 float 类型替换所有使用了 np.float 的地方。另一种方法是降级 NumPy 到旧版本。

  1. 检测numpy当前版本,方便恢复。
(ml_agent) D:\ml-agents-release_19>pip show numpy
Name: numpy
Version: 1.24.3

2. 卸载numpy pip uninstall numpy

3. 安装1.19 pip install numpy==1.19  国内镜像: pip install numpy==1.19 -i https://pypi.tuna.tsinghua.edu.cn/simple

后续如果要使用tensorflow加速训练的话,numpy版本推荐1.2.2

2024/2/3 发现在某些地方会报np.bool被弃用的错误。

FutureWarning: In the future `np.bool` will be defined as the corresponding NumPy scalar.

所以numpy版本需继续调整,经测试最后确定版本为1.18.5

pip install numpy==1.18.5 -i https://pypi.tuna.tsinghua.edu.cn/simple

继续。emmm...

  File "D:\Anaconda3\envs\ml_agent\lib\site-packages\torch\utils\tensorboard\summary.py", line 8, in <module>
    from six.moves import range
ModuleNotFoundError: No module named 'six'

提示:缺少six库。

运行pip show six检查情况,的确不存在six库。

具体依赖的版本我不清楚,我直接在Anaconda中安装了1.16.0的six库。

终于成功了!!!弯弯绕绕苦了孩子了!!!

使用Unity开始训练

导入ml-agent-Package

打开PackageManager,导入方式选择本地磁盘

选择该配置文件

ml-agents下提供了一个Unity的模板项目,可以直接使用这个工程。这里我们我们还是用自己的工程,所有需要将模板项目中的内容迁移到咱们自己的工程里。

ml-agent提供的算法

ml-agents-release_19\config\下提供了一些经典的深度学习算法案例,可以供我们训练。每种算法下有提供多种案例,yaml文件用于关联unity,是案例中的配置文件。

如基于ppo深度学习算法的小球训练案例:mlagents-learn config/ppo/3DBall.yaml --run-id=test

命令结构:

mlagents-learn <trainer-config-file> --env=<env_name> --run-id=<run-identifier>

--env=<env_name>为可执行程序的路径地址,该片段可忽略。

--run-id为训练标识

<trainer-config-file>为训练学习所需要的参数配置文件。

如果需要换成其他算法,只需将ppo换成poca、sac等即可,也可以修改配置文件里的一些参数,来提高训练结果。

使用--resume来恢复原先的训练,或者使用--force参数来覆盖已有的数据。

尾部追加--no-graphic=false/true 来决定是否启动图形化程序测试。选项为true则不启动渲染,直接后台跑算法输入到进程中训练。

文章到此就结束了。大体严格的按照文章操作下来,基本上是没问题的。不过,实际上安装机器学习环境与你自身的电脑环境有关,注意按照文章检查环境。

有关ML-Agents、ML - Python Lower Level 的其他文章,可见 机器学习 - 麦瑞克博客 (playcreator.cn)

作者:Miracle
来源:麦瑞克博客
链接:https://www.playcreator.cn/archives/unity/3754/
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议,转载请注明!
THE END
分享
海报
Unity ML-Agent+Anaconda机器学习环境搭建
本文总结了ML-Agent+Anaconda+pytorch的机器学习环境搭建过程,以及笔者在搭建过程中遇到了一些问题,在此做个记录,也希望能够提供一些帮助。 环境搭建 1.构……
<<上一篇
下一篇>>
文章目录
关闭
目 录