-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Convenience Features #5842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Convenience Features #5842
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
89117fd
Added feature to training area replicator to optionally specify repli…
78aecd0
Add ModelCarousel component that cycles through a list of models to s…
jrupert-unity 790eb97
Add usage notes and fix default values
jrupert-unity dd229c6
Added new config feature to distribute checkpointing evenly throughou…
16533d7
Dotnet formatting.
53af3a4
Updated docs.
a282961
Added tests.
3e77202
Added tests.
3648984
Updated grid sensor y scaling.
6a64f6d
Condition compile Recorder logic, which isn't supported on non-Editor…
jrupert-unity fc25e6f
Merge branch 'develop-convenience-features' of github.com:Unity-Techn…
jrupert-unity cbfad96
Fixed issue with training area replicator during editor playback.
348e5bd
Merge branch 'develop-convenience-features' of github.com:Unity-Techn…
b85b37d
Fixed issue with nightly gha.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelCarousel.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text.RegularExpressions; | ||
| using TMPro; | ||
| using Unity.Barracuda; | ||
| using Unity.MLAgents; | ||
| using Unity.MLAgents.Policies; | ||
| using UnityEditor; | ||
| #if UNITY_EDITOR | ||
| using UnityEditor.Recorder; | ||
| #endif | ||
| using UnityEngine; | ||
|
|
||
| /** | ||
| * Usage Notes: | ||
| * | ||
| * Add onnx models to the list and they will be played sequentially. | ||
| * Create a recorder and a video of the sequence will be captured automatically if "Auto Record" is selected. | ||
| * Recording only works in the Editor (not in standalone build) | ||
| * Create a TextMeshPro Text GameObject and attach it to have the number of training steps of the current model shown. | ||
| * To manually control transition between models choose a very large time, or "Pause" the system, | ||
| * then use "Force Next" to advance. | ||
| * "Reset" will start the sequence from the beginning again, use "Start" to proceed after resetting. | ||
| * "Time Scale Override" can be set, "Seconds Between Switches" will decrease proportionally if you increase this | ||
| * (i.e it represents simulated seconds between switches, not real time) | ||
| */ | ||
|
|
||
|
|
||
| public class ModelCarousel : MonoBehaviour | ||
| { | ||
| public bool m_Start = true; | ||
| public bool m_Reset = false; | ||
| public bool m_Pause = true; | ||
| public bool m_ForceNext = false; | ||
| public bool m_Loop = false; | ||
| public bool m_AutoRecord = true; | ||
| public bool m_ResetAgentOnModelChange = false; | ||
| public int m_SecondsBetweenSwitches = 10; | ||
| public float m_TimeScaleOverride = 0.0f; | ||
| public List<NNModel> m_Models = new List<NNModel>(); | ||
| public bool m_ShowStepNumber = true; | ||
| public int m_StepNumberRounding = 10000; | ||
|
|
||
| private int m_StepsSinceLastSwitch = 0; | ||
| private int m_CurrentModelIndex = 0; | ||
| private int m_CurrentlySetModelIndex = -1; | ||
|
|
||
| private NNModel m_OriginalModel = null; | ||
|
|
||
| private int k_FixedUpdatePerSecond; | ||
|
|
||
| // The attached Agent | ||
| Agent m_Agent; | ||
|
|
||
| public TextMeshProUGUI textMeshComponent; | ||
|
|
||
| #if UNITY_EDITOR | ||
| private RecorderWindow GetRecorderWindow() | ||
| { | ||
| return (RecorderWindow)EditorWindow.GetWindow(typeof(RecorderWindow)); | ||
| } | ||
| #endif | ||
|
|
||
| private void Reset() | ||
| { | ||
| m_Reset = false; | ||
| m_StepsSinceLastSwitch = 0; | ||
| m_CurrentModelIndex = 0; | ||
| m_Agent.SetModel(m_OriginalModel.name, m_OriginalModel); | ||
| textMeshComponent?.SetText("Ready to Start"); | ||
| } | ||
|
|
||
| private void OnEnable() | ||
| { | ||
| m_Agent = GetComponent<Agent>(); | ||
| m_OriginalModel = m_Agent.GetComponent<BehaviorParameters>().Model; | ||
|
|
||
| Reset(); | ||
|
|
||
| k_FixedUpdatePerSecond = (int)(1.0f / Time.fixedDeltaTime); | ||
|
|
||
| if (m_TimeScaleOverride > 0.0f) | ||
| { | ||
| Time.timeScale = m_TimeScaleOverride; | ||
| } | ||
| } | ||
|
|
||
| void StartRecording() | ||
| { | ||
| #if UNITY_EDITOR | ||
| if (!m_AutoRecord) | ||
| return; | ||
|
|
||
| Debug.Log("Starting Recording"); | ||
| RecorderWindow recorderWindow = GetRecorderWindow(); | ||
| if (!recorderWindow.IsRecording()) | ||
| recorderWindow.StartRecording(); | ||
| #endif | ||
| } | ||
|
|
||
| void StopRecording() | ||
| { | ||
| #if UNITY_EDITOR | ||
| if (!m_AutoRecord) | ||
| return; | ||
|
|
||
| Debug.Log("Stopping Recording"); | ||
| RecorderWindow recorderWindow = GetRecorderWindow(); | ||
| if (recorderWindow.IsRecording()) | ||
| recorderWindow.StopRecording(); | ||
| #endif | ||
| } | ||
|
|
||
| void UpdateStepNumberText() | ||
| { | ||
| if (!m_ShowStepNumber) | ||
| return; | ||
|
|
||
| var result = Regex.Match(m_Models[m_CurrentModelIndex].name, @".*-(\d+)$"); | ||
|
|
||
| string newText = ""; | ||
| if (result.Success && result.Groups.Count > 0) | ||
| { | ||
| var steps = Int32.Parse(result.Groups[1].Captures[0].Value); | ||
|
|
||
| int round = m_StepNumberRounding; | ||
| steps += round / 2; | ||
| steps /= round; | ||
| steps *= round; | ||
|
|
||
| newText = $"After {steps:n0} steps"; | ||
| } | ||
|
|
||
| textMeshComponent?.SetText(newText); | ||
| } | ||
|
|
||
| void SetModel() | ||
| { | ||
| if (m_CurrentModelIndex < 0 || m_CurrentModelIndex >= m_Models.Count) | ||
| return; | ||
|
|
||
| m_Agent.SetModel(m_Models[m_CurrentModelIndex].name, m_Models[m_CurrentModelIndex]); | ||
| m_CurrentlySetModelIndex = m_CurrentModelIndex; | ||
|
|
||
| UpdateStepNumberText(); | ||
|
|
||
| if (m_ResetAgentOnModelChange) | ||
| m_Agent.EndEpisode(); | ||
| } | ||
|
|
||
| void FixedUpdate() | ||
| { | ||
| if (m_Start) | ||
| { | ||
| m_Start = false; | ||
| m_Pause = false; | ||
| StartRecording(); | ||
| } | ||
|
|
||
| if (m_Reset) | ||
| { | ||
| StopRecording(); | ||
| Reset(); | ||
| m_Pause = true; | ||
| m_Start = false; | ||
| } | ||
|
|
||
| if (m_Pause && !m_ForceNext) | ||
| return; | ||
|
|
||
| if (m_CurrentlySetModelIndex != m_CurrentModelIndex) | ||
| { | ||
| SetModel(); | ||
| } | ||
|
|
||
| m_StepsSinceLastSwitch++; | ||
|
|
||
| if (m_StepsSinceLastSwitch >= m_SecondsBetweenSwitches * k_FixedUpdatePerSecond || m_ForceNext) | ||
| { | ||
| m_ForceNext = false; | ||
| m_StepsSinceLastSwitch = 0; | ||
| m_CurrentModelIndex++; | ||
|
|
||
| if (m_CurrentModelIndex == m_Models.Count) | ||
| { | ||
| if (m_Loop) | ||
| { | ||
| m_CurrentModelIndex = 0; | ||
| } | ||
| else | ||
| { | ||
| Application.Quit(0); | ||
| #if UNITY_EDITOR | ||
| EditorApplication.isPlaying = false; | ||
| #endif | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| SetModel(); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Project/Assets/ML-Agents/Examples/SharedAssets/Scripts/ModelCarousel.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m_ForceNexthas not been set totrueanywhere in the code. If it's related tothen use "Force Next" to advance., is there a place we need to set it totrue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there is. It's in the editor inspector window when you click on an object this has this script attached to it. This is a monobehaviour script, so any public property shows up in the inspector window for any object that has this script attached to it.