浏览代码

First crack at sample exports.

/develop/sample-curation
Christopher Goy 3 年前
当前提交
fe83633e
共有 5 个文件被更改,包括 180 次插入0 次删除
  1. 50
      ml-agents/tests/yamato/yamato_utils.py
  2. 30
      .yamato/ml-agents-sample-export.yml
  3. 68
      Project/Assets/ML-Agents/Editor/Tests/SampleExporter.cs
  4. 3
      Project/Assets/ML-Agents/Editor/Tests/SampleExporter.cs.meta
  5. 29
      ml-agents/tests/yamato/sample_curation.py

50
ml-agents/tests/yamato/yamato_utils.py


import glob
import os
import shutil
import subprocess

with open(dest_path, "w") as f:
yaml.dump(configs, f)
def create_samples(
scenes: List[str],
base_path: str,
log_output_path: Optional[str] = f"{get_base_output_path()}/sample_export.txt",
) -> int:
unity_exe = get_unity_executable_path()
test_args = [
unity_exe,
"-projectPath",
f"{base_path}/Project",
"-batchmode",
"-executeMethod",
"Unity.MLAgents.SampleExporter.ExportCuratedSamples",
]
if log_output_path:
os.makedirs(os.path.dirname(log_output_path), exist_ok=True)
subprocess.run(["touch", log_output_path])
test_args += ["-logfile", log_output_path]
else:
# Log to stdout
test_args += ["-logfile", "-"]
os.makedirs(os.path.join(get_base_output_path(), "Samples"), exist_ok=True)
for scene in scenes:
test_args += ["--mlagents-scene-path", scene]
timeout = 5 * 60 # 5 minutes for now
res: subprocess.CompletedProcess = subprocess.run(test_args, timeout=timeout)
if res.returncode == 0:
for file in glob.glob(os.path.join(base_path, "Project/*.unitypackage")):
print(
f"moving {file} to {os.path.join(get_base_output_path(), 'Samples', os.path.basename(file))}"
)
shutil.move(
file,
os.path.join(get_base_output_path(), "Samples", os.path.basename(file)),
)
# Print if we fail or want verbosity.
if res.returncode != 0:
if log_output_path:
subprocess.run(["cat", log_output_path])
return res.returncode

30
.yamato/ml-agents-sample-export.yml


sample_export:
name: Samples Export 2021.2
agent:
type: Unity::VM
image: package-ci/ubuntu:stable
flavor: b1.large
variables:
UNITY_VERSION: 2021.2
commands:
- python3 -m pip install pyyaml --index-url https://artifactory.prd.it.unity3d.com/artifactory/api/pypi/pypi/simple
- python3 -m pip install unity-downloader-cli --index-url https://artifactory.prd.it.unity3d.com/artifactory/api/pypi/pypi/simple --upgrade
- unity-downloader-cli -u 2021.2 -c editor --wait --fast
- python3 -u -m ml-agents.tests.yamato.sample_curation --scene "Assets/ML-Agents/Examples/Basic/Scenes/Basic.unity" "Assets/ML-Agents/Examples/Match3/Scenes/Match3.unity" "Assets/ML-Agents/Examples/WallJump/Scenes/WallJump.unity" "Assets/ML-Agents/TestScenes/TestCompressedGrid/TestGridCompressed.unity" "Assets/ML-Agents/TestScenes/TestCompressedTexture/TestTextureCompressed.unity"
triggers:
cancel_old_ci: true
expression: |
(pull_request.target eq "main" OR
pull_request.target match "release.+") AND
NOT pull_request.draft AND
(pull_request.changes.any match "com.unity.ml-agents/**" OR
pull_request.changes.any match "com.unity.ml-agents.extensions/**" OR
pull_request.changes.any match ".yamato/ml-agents-sample-export.yml") AND
NOT pull_request.changes.all match "**/*.md"
artifacts:
logs:
paths:
- "artifacts/sample_export.txt"
samples:
paths:
- "artifacts/Samples/**"

68
Project/Assets/ML-Agents/Editor/Tests/SampleExporter.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.iOS;
namespace Unity.MLAgents
{
public class SampleExporter
{
const string k_OutputCommandLineFlag = "--mlagents-sample-ouput-path";
const string k_SceneFlag = "--mlagents-scene-path";
public static void ExportCuratedSamples()
{
var oldBurst = EditorPrefs.GetBool("BurstCompilation");
EditorPrefs.SetBool("BurstCompilation", false);
try
{
var args = Environment.GetCommandLineArgs();
var outputPath = "exported_samples";
var scenes = new List<string>();
for (var i = 0; i < args.Length - 1; i++)
{
if (args[i] == k_OutputCommandLineFlag)
{
outputPath = args[i + 1];
Debug.Log($"Overriding output path to {outputPath}");
}
if (args[i] == k_SceneFlag)
{
scenes.Add(args[i + 1]);
Debug.Log($"Exporting Scene {scenes.Last()}");
}
}
foreach (var scene in scenes)
{
var assets = new List<string> { scene };
var exampleFolderToAdd = Directory.GetParent(Directory.GetParent(scene).FullName).FullName;
Debug.Log($"Parent of Scene: {exampleFolderToAdd}");
if (Directory.Exists(Path.Combine(exampleFolderToAdd, "Scripts")))
{
exampleFolderToAdd = Path.Combine(exampleFolderToAdd, "Scripts");
}
exampleFolderToAdd = exampleFolderToAdd.Substring(exampleFolderToAdd.IndexOf("Assets"));
foreach (var guid in AssetDatabase.FindAssets("t:Script", new[] { exampleFolderToAdd }))
{
var path = AssetDatabase.GUIDToAssetPath(guid);
assets.Add(path);
Debug.Log($"Adding Asset: {path}");
}
AssetDatabase.ExportPackage(assets.ToArray(), Path.GetFileNameWithoutExtension(scene) + ".unitypackage", ExportPackageOptions.IncludeDependencies | ExportPackageOptions.Recurse);
}
}
catch (Exception e)
{
Debug.Log(e);
EditorApplication.Exit(1);
}
EditorPrefs.SetBool("BurstCompilation", oldBurst);
EditorApplication.Exit(0);
}
}
}

3
Project/Assets/ML-Agents/Editor/Tests/SampleExporter.cs.meta


fileFormatVersion: 2
guid: 914d5be190bb435eb11383db3aaf70eb
timeCreated: 1615161245

29
ml-agents/tests/yamato/sample_curation.py


import sys
import argparse
from .yamato_utils import get_base_path, create_samples
def main(scenes):
base_path = get_base_path()
print(f"Running in base path {base_path}")
returncode = create_samples(
scenes,
base_path,
log_output_path=None, # Log to stdout so we get timestamps on the logs
)
if returncode == 0:
print("Test run SUCCEEDED!")
else:
print("Test run FAILED!")
sys.exit(returncode)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--scene", nargs="+", default=None, required=True)
args = parser.parse_args()
main(args.scene)
正在加载...
取消
保存