Browse Source

model swap manager added

audio
cailean 6 months ago
parent
commit
a4de1e1104
  1. 4
      Assets/3_MarathonEnvs/Scripts/Ragdoll004/RagDollAgent.cs
  2. 40
      Assets/5_Scenes/DreconDemo.unity
  3. 86
      Assets/8_Scripts/1_Managers/Model Manager.cs
  4. 4
      Assets/8_Scripts/1_Managers/UIManager.cs
  5. 2
      Assets/ML-Agents/Timers/DreconDemo_timers.json
  6. BIN
      nnmodels/10000.onnx
  7. BIN
      nnmodels/15000.onnx
  8. BIN
      nnmodels/5000.onnx

4
Assets/3_MarathonEnvs/Scripts/Ragdoll004/RagDollAgent.cs

@ -67,6 +67,9 @@ public class RagDollAgent : Agent
public LayerMask m_ExcludeLayer;
public LayerMask m_Temp;
public delegate void OnMoveToNextModelDelegate();
public static OnMoveToNextModelDelegate m_MoveToNextModel;
void Awake()
{
if (RequestCamera && CameraTarget != null)
@ -244,6 +247,7 @@ public class RagDollAgent : Agent
timer = 0;
waitStarted = false;
m_TerrainCollider.excludeLayers = m_Temp;
m_MoveToNextModel?.Invoke();
EndEpisode();
}
}

40
Assets/5_Scenes/DreconDemo.unity

@ -594,7 +594,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 153031623}
serializedVersion: 2
m_LocalRotation: {x: -0.000010761081, y: 0.9982297, z: 0.059476666, w: -0.000077618104}
m_LocalRotation: {x: -0.00001076108, y: 0.9982297, z: 0.059476666, w: -0.000077618104}
m_LocalPosition: {x: 18.101631, y: 8.540332, z: 4.4995093}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -918,7 +918,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 320225263}
serializedVersion: 2
m_LocalRotation: {x: 0.38916236, y: 0.5932106, z: -0.58294636, w: 0.39601454}
m_LocalRotation: {x: 0.38916615, y: 0.59320796, z: -0.58294386, w: 0.3960183}
m_LocalPosition: {x: 17.78, y: 21.78, z: 3.06}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -1000,7 +1000,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 326736459}
serializedVersion: 2
m_LocalRotation: {x: -0.07396109, y: 0.9066138, z: 0.17882524, w: 0.37497023}
m_LocalRotation: {x: -0.073961094, y: 0.9066138, z: 0.17882526, w: 0.3749702}
m_LocalPosition: {x: 17.7008, y: 8.655965, z: -0.081218004}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -1188,11 +1188,11 @@ Camera:
y: 0
width: 1
height: 1
near clip plane: 0.01
near clip plane: 0.1
far clip plane: 5000
field of view: 31.1
orthographic: 0
orthographic size: 0.53
field of view: 40
orthographic: 1
orthographic size: -0.34
m_Depth: -1
m_CullingMask:
serializedVersion: 2
@ -1216,8 +1216,8 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 330585543}
serializedVersion: 2
m_LocalRotation: {x: -0.254152, y: 0.69070727, z: -0.6135771, w: -0.28610036}
m_LocalPosition: {x: 22.891924, y: 9.244215, z: 4.6990404}
m_LocalRotation: {x: 0.38916615, y: 0.59320796, z: -0.58294386, w: 0.3960183}
m_LocalPosition: {x: 17.78, y: 21.78, z: 3.06}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
@ -2196,6 +2196,7 @@ MonoBehaviour:
- {fileID: 45273097}
- {fileID: 446294241}
- {fileID: 405810719}
m_ModelNameText: {fileID: 1261636063}
--- !u!4 &635122736 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 7722778103894512693, guid: 248e74dcdf41731459e829be7f230e47,
@ -3905,7 +3906,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 75.5, y: 916.7}
m_AnchoredPosition: {x: 808.4, y: 916.7}
m_SizeDelta: {x: 1800, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1261636063
@ -3928,7 +3929,7 @@ MonoBehaviour:
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text: 0
m_text: 500,000
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: e43a431f01118124d8d1cf15a46635c3, type: 2}
m_sharedMaterial: {fileID: -966069743490610113, guid: e43a431f01118124d8d1cf15a46635c3,
@ -7975,6 +7976,7 @@ GameObject:
- component: {fileID: 4153003204276630831}
- component: {fileID: 3541975130338892481}
- component: {fileID: 7412007325377876319}
- component: {fileID: 7412007325377876320}
m_Layer: 6
m_Name: MarathonMan004
m_TagString: Untagged
@ -13142,6 +13144,22 @@ MonoBehaviour:
info2store: {fileID: 0}
debugWithLargestROM: 0
extraoffset: 10
--- !u!114 &7412007325377876320
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1973854533754064255}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: af36152b9449197449b578448c25b741, type: 3}
m_Name:
m_EditorClassIdentifier:
m_ModelName:
m_InitialModel: {fileID: 5022602860645237092, guid: 0e791689cf0300c4fbe8be76f09fccff,
type: 3}
m_DirectoryPath: '../nnmodels '
--- !u!4 &7431187848490878806
Transform:
m_ObjectHideFlags: 0

86
Assets/8_Scripts/1_Managers/Model Manager.cs

@ -1,5 +1,9 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.Barracuda;
using Unity.Barracuda.ONNX;
using Unity.MLAgents.Policies;
using UnityEngine;
@ -8,22 +12,96 @@ public class ModelManager : MonoBehaviour
[SerializeField]
public string m_ModelName;
[Header("Model Settings")]
public NNModel m_InitialModel;
public string m_DirectoryPath;
private List<NNModel> m_ModelList = new List<NNModel>();
private int m_CurrentModel = 0;
private BehaviorParameters m_Parameters;
private RagDollAgent m_Agent;
// Unity Events //
public delegate void onUpdateModelNameDelegate(string modelName);
public delegate void onUpdateModelNameDelegate(float modelName);
public static onUpdateModelNameDelegate m_UpdateModelName;
private void OnEnable()
{
RagDollAgent.m_MoveToNextModel += SwapModel;
m_Parameters = GetComponent<BehaviorParameters>();
m_Agent = GetComponent<RagDollAgent>();
}
private void OnDisable()
{
RagDollAgent.m_MoveToNextModel -= SwapModel;
}
private void Start()
{
LoadLocalModels();
SetModel(m_ModelList[m_CurrentModel]);
}
private void SwapModel()
{
m_CurrentModel++;
if (m_CurrentModel >= m_ModelList.Count)
m_CurrentModel = 0;
SetModel(m_ModelList[m_CurrentModel]);
}
private void SetModel(NNModel model)
{
m_Agent.SetModel("DReCon-v0", model, InferenceDevice.Burst);
UpdateModelName();
}
private void LoadLocalModels()
{
DirectoryInfo dirInfo = new DirectoryInfo(Path.Combine(Application.dataPath, m_DirectoryPath));
FileInfo[] nnList = dirInfo.GetFiles("*.onnx");
// Sort files by filename (assuming filenames are numeric)
Array.Sort(nnList, (x, y) => {
int num1 = int.Parse(Path.GetFileNameWithoutExtension(x.Name));
int num2 = int.Parse(Path.GetFileNameWithoutExtension(y.Name));
return num1.CompareTo(num2);
});
ConvertNNModels(nnList);
}
private void ConvertNNModels(FileInfo[] nnList)
{
foreach (FileInfo element in nnList)
{
var converter = new ONNXModelConverter(true);
byte[] modelData = File.ReadAllBytes(element.FullName.ToString());
Model model = converter.Convert(modelData);
NNModelData modelD = ScriptableObject.CreateInstance<NNModelData>();
using (var memoryStream = new MemoryStream())
using (var writer = new BinaryWriter(memoryStream))
{
ModelWriter.Save(writer, model);
modelD.Value = memoryStream.ToArray();
}
modelD.name = "Data";
modelD.hideFlags = HideFlags.HideInHierarchy;
NNModel result = ScriptableObject.CreateInstance<NNModel>();
result.modelData = modelD;
result.name = element.Name;
// Add Model to Model List
m_ModelList.Add(result);
}
}
private void FixedUpdate()
private void UpdateModelName()
{
if (m_Parameters != null)
{
m_ModelName = m_Parameters.Model.name;
m_UpdateModelName?.Invoke(m_ModelName);
m_ModelName = Path.GetFileNameWithoutExtension(m_Parameters.Model.name);
m_UpdateModelName?.Invoke(float.Parse(m_ModelName));
}
}
}

4
Assets/8_Scripts/1_Managers/UIManager.cs

@ -34,11 +34,11 @@ public class UIManager : MonoBehaviour
}
}
private void UpdateModelName(string modelName)
private void UpdateModelName(float modelName)
{
if( m_ModelNameText != null )
{
m_ModelNameText.text = modelName;
m_ModelNameText.text = modelName.ToString("N0");
}
}
}

2
Assets/ML-Agents/Timers/DreconDemo_timers.json

@ -1 +1 @@
{"count":1,"self":69.959808,"total":71.912097899999992,"children":{"InitializeActuators":{"count":1,"self":0.0009989,"total":0.0009989,"children":null},"InitializeSensors":{"count":1,"self":0.0010052,"total":0.0010052,"children":null},"AgentSendState":{"count":3942,"self":0.0166721,"total":0.4865961,"children":{"CollectObservations":{"count":1971,"self":0.45088469999999997,"total":0.45088469999999997,"children":null},"WriteActionMask":{"count":1971,"self":0.0030042999999999997,"total":0.0030042999999999997,"children":null},"RequestDecision":{"count":1971,"self":0.016035,"total":0.016035,"children":null}}},"DecideAction":{"count":3942,"self":0.8178881,"total":0.8178881,"children":null},"AgentAct":{"count":3942,"self":0.64580149999999992,"total":0.64580149999999992,"children":null}},"gauges":{"DReCon-v0.CumulativeReward":{"count":2,"max":247.057678,"min":79.2546539,"runningAverage":163.156158,"value":247.057678,"weightedAverage":121.205414}},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1714389274","unity_version":"2022.3.5f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.5f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\Projects\\24_3-Moloch\\2_Unity\\Expeirments\\Drecon2022 -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-caile -hubSessionId 9b433f92-5c93-446a-923f-743ea7173ebf -accessToken EpS5BrO4ecWgDnIKinrxODUmKDNMMpqYHpQjzvf9aho005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"DreconDemo","end_time_seconds":"1714389346"}}
{"count":1,"self":57.1685376,"total":58.9029435,"children":{"InitializeActuators":{"count":1,"self":0.0009961,"total":0.0009961,"children":null},"InitializeSensors":{"count":1,"self":0.0015038,"total":0.0015038,"children":null},"AgentSendState":{"count":3077,"self":0.0150424,"total":0.5569085,"children":{"CollectObservations":{"count":1539,"self":0.3906794,"total":0.3906794,"children":null},"WriteActionMask":{"count":1539,"self":0.0039163,"total":0.0039163,"children":null},"RequestDecision":{"count":1539,"self":0.1472704,"total":0.1472704,"children":null}}},"DecideAction":{"count":3077,"self":0.64407969999999992,"total":0.64407969999999992,"children":null},"AgentAct":{"count":3077,"self":0.5309171,"total":0.5309171,"children":null}},"gauges":{},"metadata":{"timer_format_version":"0.1.0","start_time_seconds":"1714393193","unity_version":"2022.3.5f1","command_line_arguments":"C:\\Program Files\\Unity\\Hub\\Editor\\2022.3.5f1\\Editor\\Unity.exe -projectpath C:\\Users\\caile\\Desktop\\Projects\\24_3-Moloch\\2_Unity\\Expeirments\\Drecon2022 -useHub -hubIPC -cloudEnvironment production -licensingIpc LicenseClient-caile -hubSessionId 9b433f92-5c93-446a-923f-743ea7173ebf -accessToken EpS5BrO4ecWgDnIKinrxODUmKDNMMpqYHpQjzvf9aho005f","communication_protocol_version":"1.5.0","com.unity.ml-agents_version":"2.0.1","scene_name":"DreconDemo","end_time_seconds":"1714393252"}}

BIN
nnmodels/10000.onnx

Binary file not shown.

BIN
nnmodels/15000.onnx

Binary file not shown.

BIN
nnmodels/5000.onnx

Binary file not shown.
Loading…
Cancel
Save