浏览代码

simplified coroutine manipulation in scenario tests

/usim-randomization
Steven Leal 4 年前
当前提交
2124e3f5
共有 6 个文件被更改,包括 67 次插入72 次删除
  1. 2
      com.unity.perception/Documentation~/Randomization/Tutorial.md
  2. 8
      com.unity.perception/Editor/Randomization/ParameterElement.cs
  3. 2
      com.unity.perception/Editor/Randomization/Uxml/ParameterElement.uxml
  4. 1
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  5. 51
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  6. 75
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs

2
com.unity.perception/Documentation~/Randomization/Tutorial.md


6. Building a simulation runtime
7. Modifying scenario constants
By the end of this guide, the user should have a new project that generates the perception data necessary to train a model to recognize a cube from a solid colored background.
By the end of this guide, the user should have a new project that generates the perception data necessary to train a model to identify a cube from a solid colored background.
Note: Before beginning the tutorial, follow [this guide](../SetupSteps.md) to install the perception package into a new Unity project.

8
com.unity.perception/Editor/Randomization/ParameterElement.cs


using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.Perception.Randomization.Parameters;

var frequencyField = this.Q<EnumField>("application-frequency");
frequencyField.BindProperty(m_SerializedProperty.FindPropertyRelative("target.applicationFrequency"));
var targetField = this.Q<PropertyField>("target");
targetField.BindProperty(m_TargetGameObjectProperty);
var targetField = this.Q<ObjectField>("target");
targetField.objectType = typeof(GameObject);
targetField.value = m_TargetGameObjectProperty.objectReferenceValue;
if (evt.newValue == parameter.target.gameObject)
return;
ClearTarget();
m_TargetGameObjectProperty.objectReferenceValue = (GameObject)evt.newValue;
m_SerializedProperty.serializedObject.ApplyModifiedProperties();

2
com.unity.perception/Editor/Randomization/Uxml/ParameterElement.uxml


<VisualElement name="properties" class="parameter-properties-container" style="margin-bottom: 2px;">
<Box>
<editor:PropertyField label="Target GameObject" name="target"/>
<editor:ObjectField label="Target GameObject" name="target"/>
<VisualElement name="target-container">
<VisualElement class="unity-base-field">
<Label text="Target Property" class="unity-base-field__label"/>

1
com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs


using Unity.Simulation;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Perception.Randomization.Scenarios;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
using UnityEngine.Serialization;

51
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


public abstract class ScenarioBase : MonoBehaviour
{
static ScenarioBase s_ActiveScenario;
bool m_FirstFrame = true;
bool m_FirstIteration = true;
bool m_SkipFrame = true;
bool m_FirstScenarioFrame = true;
/// <summary>
/// If true, this scenario will quit the Unity application when it's finished executing

void Update()
{
// TODO: remove this check when the perception camera can capture the first frame of output
if (m_FirstFrame)
if (m_SkipFrame)
{
m_SkipFrame = false;
}
if (currentIterationFrame == 0 && !m_FirstIteration)
OnIterationTeardown();
// Iterate Scenario
if (m_FirstScenarioFrame)
{
m_FirstScenarioFrame = false;
}
else
{
currentIterationFrame++;
framesSinceInitialization++;
if (isIterationComplete)
{
currentIteration++;
currentIterationFrame = 0;
OnIterationTeardown();
}
}
// Quit if scenario is complete
if (isScenarioComplete)
{
OnComplete();

UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
Application.Quit();
return;
// Perform new iteration tasks
if (currentIterationFrame == 0)
{
DatasetCapture.StartNewSequence();

OnIterationSetup();
}
// Perform new frame tasks
}
void LateUpdate()
{
// TODO: remove this check when the perception camera can capture the first frame of output
if (m_FirstFrame)
{
m_FirstFrame = false;
return;
}
currentIterationFrame++;
framesSinceInitialization++;
if (isIterationComplete)
{
m_FirstIteration = false;
currentIteration++;
currentIterationFrame = 0;
}
}
}
}

75
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs


}
// TODO: update this function once the perception camera doesn't skip the first frame
IEnumerator CreateNewScenario()
IEnumerator CreateNewScenario(int totalIterations, int framesPerIteration)
yield return null;
m_Scenario.constants.totalIterations = totalIterations;
m_Scenario.constants.framesPerIteration = framesPerIteration;
yield return null; // Skip Start() frame
yield return null; // Skip first Update() frame
yield return CreateNewScenario();
yield return CreateNewScenario(10, 10);
m_Scenario.serializedConstantsFileName = "perception_serialization_test";
var constants = new FixedLengthScenario.Constants

[UnityTest]
public IEnumerator IterationsCanLastMultipleFrames()
{
yield return CreateNewScenario();
const int testIterationFrameCount = 5;
m_Scenario.constants.framesPerIteration = testIterationFrameCount;
for (var i = 0; i < testIterationFrameCount; i++)
const int frameCount = 5;
yield return CreateNewScenario(1, frameCount);
for (var i = 0; i < frameCount; i++)
{
Assert.AreEqual(0, m_Scenario.currentIteration);
yield return null;

[UnityTest]
public IEnumerator FinishesWhenIsScenarioCompleteIsTrue()
{
yield return CreateNewScenario();
const int testIterationTotal = 5;
m_Scenario.constants.framesPerIteration = 1;
m_Scenario.constants.totalIterations = testIterationTotal;
for (var i = 0; i < testIterationTotal; i++)
const int iterationCount = 5;
yield return CreateNewScenario(iterationCount, 1);
for (var i = 0; i < iterationCount; i++)
{
Assert.False(m_Scenario.isScenarioComplete);
yield return null;

[UnityTest]
public IEnumerator AppliesParametersEveryFrame()
{
yield return CreateNewScenario();
m_Scenario.constants.framesPerIteration = 5;
m_Scenario.constants.totalIterations = 1;
var config = m_TestObject.AddComponent<ParameterConfiguration>();
var parameter = config.AddParameter<Vector3Parameter>();
parameter.x = new UniformSampler(1, 2);

m_TestObject, m_TestObject.transform, "position", ParameterApplicationFrequency.EveryFrame);
var initialPosition = new Vector3();
m_TestObject.transform.position = initialPosition;
var initialPosition = Vector3.zero;
yield return CreateNewScenario(1, 5);
// ReSharper disable once Unity.InefficientPropertyAccess
Assert.AreNotEqual(initialPosition, m_TestObject.transform.position);
// ReSharper disable once Unity.InefficientPropertyAccess
initialPosition = m_TestObject.transform.position;
// ReSharper disable once Unity.InefficientPropertyAccess
Assert.AreNotEqual(initialPosition, m_TestObject.transform.position);
}

parameter.z = new UniformSampler(1, 2);
var transform = m_TestObject.transform;
transform.position = new Vector3();
var prevPosition = new Vector3();
transform.position = prevPosition;
yield return CreateNewScenario();
m_Scenario.constants.framesPerIteration = 2;
m_Scenario.constants.totalIterations = 2;
yield return CreateNewScenario(2, 2);
yield return new WaitForEndOfFrame();
var initialPosition = transform.position;
Assert.AreNotEqual(new Vector3(), initialPosition);
Assert.AreNotEqual(prevPosition, transform.position);
// ReSharper disable once Unity.InefficientPropertyAccess
prevPosition = transform.position;
yield return new WaitForEndOfFrame();
yield return null;
// ReSharper disable once Unity.InefficientPropertyAccess
Assert.AreEqual(prevPosition, transform.position);
var nextPosition = transform.position;
Assert.AreEqual(initialPosition, nextPosition);
prevPosition = transform.position;
yield return new WaitForEndOfFrame();
yield return null;
Assert.AreNotEqual(nextPosition, transform.position);
Assert.AreNotEqual(prevPosition, transform.position);
}
[UnityTest]

yield return CreateNewScenario();
m_Scenario.constants.framesPerIteration = 2;
m_Scenario.constants.totalIterations = 2;
// Skip first frame
yield return new WaitForEndOfFrame();
yield return CreateNewScenario(2, 2);
yield return new WaitForEndOfFrame();
yield return null;
yield return new WaitForEndOfFrame();
yield return null;
Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, 0);
}

正在加载...
取消
保存