浏览代码

WIP - check for presence of ModelOverrides

/check-for-ModelOverriders
Chris Elion 4 年前
当前提交
83f2ec5e
共有 3 个文件被更改,包括 134 次插入51 次删除
  1. 140
      Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs
  2. 2
      Project/Assets/ML-Agents/Examples/Startup/Scripts/Startup.cs
  3. 43
      Project/Assets/ML-Agents/Examples/Startup/Startup.unity

140
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelOverrider.cs


using Unity.Barracuda.ONNX;
using Unity.MLAgents;
using Unity.MLAgents.Policies;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif

/// </summary>
public class ModelOverrider : MonoBehaviour
{
HashSet<string> k_SupportedExtensions = new HashSet<string> { "nn", "onnx" };
static HashSet<string> k_SupportedExtensions = new HashSet<string> { "nn", "onnx" };
const string k_CommandLineModelOverrideDirectoryFlag = "--mlagents-override-model-directory";
const string k_CommandLineModelOverrideExtensionFlag = "--mlagents-override-model-extension";
const string k_CommandLineQuitAfterEpisodesFlag = "--mlagents-quit-after-episodes";

public struct OverrideSettings
{
public string BehaviorNameOverrideDirectory;
public List<string> OverrideExtensions;
public int MaxEpisodes;
public int TimeoutSeconds;
public bool QuitOnLoadFailure;
public bool HasOverrides()
{
return !string.IsNullOrEmpty(BehaviorNameOverrideDirectory);
}
}
private OverrideSettings m_OverrideSettings;
string m_BehaviorNameOverrideDirectory;
//string m_BehaviorNameOverrideDirectory;
private List<string> m_OverrideExtensions = new List<string>();
//private List<string> m_OverrideExtensions = new List<string>();
// Cached loaded NNModels, with the behavior name as the key.
Dictionary<string, NNModel> m_CachedModels = new Dictionary<string, NNModel>();

// Will default to 1 if override models are specified, otherwise 0.
int m_MaxEpisodes;
//int m_MaxEpisodes;
// Deadline - exit if the time exceeds this
DateTime m_Deadline = DateTime.MaxValue;

int m_PreviousAgentCompletedEpisodes;
bool m_QuitOnLoadFailure;
//bool m_QuitOnLoadFailure;
[Tooltip("Debug values to be used in place of the command line for overriding models.")]
public string debugCommandLineOverride;

get
{
GetAssetPathFromCommandLine();
return !string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory);
return m_OverrideSettings.HasOverrides();
}
}

}
/// <summary>
/// Get the asset path to use from the commandline arguments.
/// </summary>
/// <returns></returns>
void GetAssetPathFromCommandLine()
static OverrideSettings GetSettingsFromCommandLine(string debugCommandLineOverride = null)
var maxEpisodes = 0;
var timeoutSeconds = 0;
var overrideSettings = new OverrideSettings();
overrideSettings.OverrideExtensions = new List<string>();
string[] commandLineArgsOverride = null;
if (!string.IsNullOrEmpty(debugCommandLineOverride) && Application.isEditor)

{
if (args[i] == k_CommandLineModelOverrideDirectoryFlag && i < args.Length - 1)
{
m_BehaviorNameOverrideDirectory = args[i + 1].Trim();
overrideSettings.BehaviorNameOverrideDirectory = args[i + 1].Trim();
}
else if (args[i] == k_CommandLineModelOverrideExtensionFlag && i < args.Length - 1)
{

EditorApplication.isPlaying = false;
#endif
}
m_OverrideExtensions.Add(overrideExtension);
overrideSettings.OverrideExtensions.Add(overrideExtension);
Int32.TryParse(args[i + 1], out maxEpisodes);
Int32.TryParse(args[i + 1], out overrideSettings.MaxEpisodes);
Int32.TryParse(args[i + 1], out timeoutSeconds);
Int32.TryParse(args[i + 1], out overrideSettings.TimeoutSeconds);
m_QuitOnLoadFailure = true;
overrideSettings.QuitOnLoadFailure = true;
if (!string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory))
return overrideSettings;
}
/// <summary>
/// Get the asset path to use from the commandline arguments.
/// </summary>
/// <returns></returns>
void GetAssetPathFromCommandLine()
{
m_OverrideSettings = GetSettingsFromCommandLine(debugCommandLineOverride);
if (!string.IsNullOrEmpty(m_OverrideSettings.BehaviorNameOverrideDirectory))
m_MaxEpisodes = maxEpisodes > 0 ? maxEpisodes : 1;
Debug.Log($"setting m_MaxEpisodes to {maxEpisodes}");
m_OverrideSettings.MaxEpisodes = m_OverrideSettings.MaxEpisodes > 0 ? m_OverrideSettings.MaxEpisodes : 1;
Debug.Log($"setting m_MaxEpisodes to {m_OverrideSettings.MaxEpisodes}");
if (timeoutSeconds > 0)
if (m_OverrideSettings.TimeoutSeconds > 0)
m_Deadline = DateTime.Now + TimeSpan.FromSeconds(timeoutSeconds);
Debug.Log($"setting deadline to {timeoutSeconds} from now.");
m_Deadline = DateTime.Now + TimeSpan.FromSeconds(m_OverrideSettings.TimeoutSeconds);
Debug.Log($"Setting deadline to {m_OverrideSettings.TimeoutSeconds} seconds from now.");
}
}

void FixedUpdate()
{
if (m_MaxEpisodes > 0)
if (m_OverrideSettings.MaxEpisodes > 0)
if (TotalCompletedEpisodes >= m_MaxEpisodes && TotalNumSteps > m_MaxEpisodes * m_Agent.MaxStep)
if (TotalCompletedEpisodes >= m_OverrideSettings.MaxEpisodes && TotalNumSteps > m_OverrideSettings.MaxEpisodes * m_Agent.MaxStep)
{
Debug.Log($"ModelOverride reached {TotalCompletedEpisodes} episodes and {TotalNumSteps} steps. Exiting.");
Application.Quit(0);

{
Debug.Log(
$"Deadline exceeded. " +
$"{TotalCompletedEpisodes}/{m_MaxEpisodes} episodes and " +
$"{TotalNumSteps}/{m_MaxEpisodes * m_Agent.MaxStep} steps completed. Exiting.");
$"{TotalCompletedEpisodes}/{m_OverrideSettings.MaxEpisodes} episodes and " +
$"{TotalNumSteps}/{m_OverrideSettings.MaxEpisodes * m_Agent.MaxStep} steps completed. Exiting.");
Application.Quit(0);
#if UNITY_EDITOR
EditorApplication.isPlaying = false;

return m_CachedModels[behaviorName];
}
if (string.IsNullOrEmpty(m_BehaviorNameOverrideDirectory))
if (string.IsNullOrEmpty(m_OverrideSettings.BehaviorNameOverrideDirectory))
{
Debug.Log($"No override directory set.");
return null;

var overrideExtensions = (m_OverrideExtensions.Count > 0)
? m_OverrideExtensions.ToArray()
var overrideExtensions = (m_OverrideSettings.OverrideExtensions.Count > 0)
? m_OverrideSettings.OverrideExtensions.ToArray()
: new[] { "nn", "onnx" };
byte[] rawModel = null;

{
var assetPath = Path.Combine(m_BehaviorNameOverrideDirectory, $"{behaviorName}.{overrideExtension}");
var assetPath = Path.Combine(m_OverrideSettings.BehaviorNameOverrideDirectory, $"{behaviorName}.{overrideExtension}");
try
{
rawModel = File.ReadAllBytes(assetPath);

if (rawModel == null)
{
Debug.Log($"Couldn't load model file(s) for {behaviorName} in {m_BehaviorNameOverrideDirectory} (full path: {Path.GetFullPath(m_BehaviorNameOverrideDirectory)}");
Debug.Log($"Couldn't load model file(s) for {behaviorName} in {m_OverrideSettings.BehaviorNameOverrideDirectory} (full path: {Path.GetFullPath(m_OverrideSettings.BehaviorNameOverrideDirectory)}");
// Cache the null so we don't repeatedly try to load a missing file
m_CachedModels[behaviorName] = null;
return null;

}
}
if (!overrideOk && m_QuitOnLoadFailure)
if (!overrideOk && m_OverrideSettings.QuitOnLoadFailure)
{
if (!string.IsNullOrEmpty(overrideError))
{

#endif
}
}
public static void CheckSceneForModelOverrides()
{
var overrideSettings = GetSettingsFromCommandLine();
if (!overrideSettings.HasOverrides())
{
// No overrides specified on the commmandline, so don't check the scene.
return;
}
var overrideComponentsFound = SceneHasModelOverrideComponents();
if (overrideComponentsFound)
{
// Expected override components and found them.
return;
}
Debug.LogError("Model overriding set on command line, but scene contains no ModelOverride components.");
if (overrideSettings.QuitOnLoadFailure)
{
Application.Quit(1);
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#endif
}
}
static bool SceneHasModelOverrideComponents()
{
GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
Debug.Log($"Found {allObjects.Length} total GameObjects.");
foreach (var gameObj in allObjects)
{
Debug.Log($"Checking GameObject {gameObj.name}.");
var modeloverride = gameObj.GetComponentsInChildren<ModelOverrider>();
if (modeloverride != null && modeloverride.Length > 0)
{
// Found at least 1 model override.
return true;
}
}
Debug.Log($"No ModelOverriders found.");
return false;
}
}
}

2
Project/Assets/ML-Agents/Examples/Startup/Scripts/Startup.cs


Application.Quit(22);
return;
}
ModelOverrider.CheckSceneForModelOverrides();
}
}
}

43
Project/Assets/ML-Agents/Examples/Startup/Startup.unity


--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 8
serializedVersion: 9
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3

m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.3731316, g: 0.38074902, b: 0.3587254, a: 1}
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074, b: 0.35872698, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0

m_BounceScale: 1
m_IndirectOutputScale: 1
m_AlbedoBoost: 1
m_TemporalCoherenceThreshold: 1
serializedVersion: 9
serializedVersion: 12
m_TextureWidth: 1024
m_TextureHeight: 1024
m_AtlasSize: 1024
m_ExtractAmbientOcclusion: 0
m_Padding: 2
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1

m_PVRDirectSampleCount: 32
m_PVRSampleCount: 500
m_PVRBounces: 2
m_PVREnvironmentSampleCount: 500
m_PVREnvironmentReferencePointCount: 2048
m_PVRFilteringMode: 2
m_PVRDenoiserTypeDirect: 0
m_PVRDenoiserTypeIndirect: 0
m_PVRDenoiserTypeAO: 0
m_PVRFilteringMode: 1
m_PVREnvironmentMIS: 0
m_PVRCulling: 1
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5

m_PVRFilteringAtrousPositionSigmaAO: 1
m_ShowResolutionOverlay: 1
m_ExportTrainingData: 0
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
--- !u!196 &4

--- !u!1 &520360983
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Name: GameObject
m_Name: StartupGameObject
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 520360983}
m_Enabled: 1
m_EditorHideFlags: 0

--- !u!4 &520360985
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 520360983}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}

正在加载...
取消
保存