浏览代码

#34: Analytics v2 (Scenario Information)

/0.9.0.preview.1_staging
GitHub Enterprise 3 年前
当前提交
f2b625c8
共有 16 个文件被更改,包括 1007 次插入118 次删除
  1. 8
      com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
  2. 6
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  3. 301
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs
  4. 210
      com.unity.perception/Runtime/PerceptionAnalytics.cs
  5. 3
      com.unity.perception/Runtime/PerceptionAnalytics.cs.meta
  6. 360
      com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs
  7. 3
      com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs.meta
  8. 50
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/AllMembersAndParametersTestRandomizer.cs
  9. 11
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/AllMembersAndParametersTestRandomizer.cs.meta
  10. 24
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleAnimationPoseConfig.asset
  11. 8
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleAnimationPoseConfig.asset.meta
  12. 23
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleIdLabelConfig.asset
  13. 8
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleIdLabelConfig.asset.meta
  14. 8
      TestProjects/PerceptionHDRP/Assets/Scenes.meta
  15. 3
      com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs.meta
  16. 99
      com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs

8
com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs


using UnityEditor.Build.Reporting;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.Perception;
using UnityEngine.Perception.Analytics;
using UnityEngine.Perception.Randomization.Samplers;
using UnityEngine.Perception.Randomization.Scenarios;
using UnityEngine.SceneManagement;

currentScenario = FindObjectOfType<ScenarioBase>()
};
var runGuid = Guid.NewGuid();
PerceptionEditorAnalytics.ReportRunInUnitySimulationStarted(
PerceptionAnalytics.ReportRunInUnitySimulationStarted(
runGuid,
m_RunParameters.totalIterations,
m_RunParameters.instanceCount,

}
catch (Exception e)
{
PerceptionEditorAnalytics.ReportRunInUnitySimulationFailed(runGuid, e.Message);
PerceptionAnalytics.ReportRunInUnitySimulationFailed(runGuid, e.Message);
throw;
}
finally

run.Execute();
// Cleanup
PerceptionEditorAnalytics.ReportRunInUnitySimulationSucceeded(runGuid, run.executionId);
PerceptionAnalytics.ReportRunInUnitySimulationSucceeded(runGuid, run.executionId);
// Set new Player Preferences
PlayerPrefs.SetString("SimWindow/runName", m_RunParameters.runName);

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


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.Simulation;
using UnityEngine.Perception.Analytics;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.SceneManagement;
namespace UnityEngine.Perception.Randomization.Scenarios
{

{
foreach (var randomizer in activeRandomizers)
randomizer.ScenarioComplete();
OnComplete();
state = State.Idle;
OnIdle();

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


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Unity.Collections;
using UnityEngine.Perception.Analytics;
using UnityEngine.Perception.RandomizationTests.ScenarioTests;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;

{
GameObject m_TestObject;
TestFixedLengthScenario m_Scenario;
static string RemoveWhitespace(string str) =>
string.Join("", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
[SetUp]
public void Setup()

m_Scenario = m_TestObject.AddComponent<TestFixedLengthScenario>();
m_Scenario.AddRandomizer(new RotationRandomizer());
string RemoveWhitespace(string str) =>
string.Join("", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
var expectedConfigAsset = (TextAsset)Resources.Load("SampleScenarioConfiguration");
var expectedText = RemoveWhitespace(expectedConfigAsset.text);
var scenarioJson = RemoveWhitespace(m_Scenario.SerializeToJson());

Assert.AreEqual(0, m_Scenario.currentIteration);
yield return null;
}
Assert.AreEqual(1, m_Scenario.currentIteration);
}

Assert.True(m_Scenario.state == ScenarioBase.State.Playing);
yield return null;
}
Assert.True(m_Scenario.state == ScenarioBase.State.Idle);
}

Assert.AreNotEqual(seeds[i], SamplerState.NextRandomState());
}
[UnityTest]
public IEnumerator IterationCorrectlyDelays()
{

// State: currentIteration = 0
Assert.AreEqual(0, m_Scenario.currentIteration);
yield return null;
}
[UnityTest]
public IEnumerator ScenarioCompletedAnalyticsSerializesCorrectly()
{
// Perception Camera Serialization
var perceptionCamera = m_TestObject.AddComponent<PerceptionCamera>();
perceptionCamera.captureTriggerMode = CaptureTriggerMode.Scheduled;
perceptionCamera.firstCaptureFrame = 2;
perceptionCamera.framesBetweenCaptures = 10;
// Labeler serialization
var sampleIdLabelConfig = Resources.Load<IdLabelConfig>("sampleIdLabelConfig");
var sampleAnimationPoseConfig = Resources.Load<AnimationPoseConfig>("sampleAnimationPoseConfig");
var emptyKeypointTemplate = ScriptableObject.CreateInstance<KeypointTemplate>();
emptyKeypointTemplate.keypoints = new KeypointDefinition[] { };
emptyKeypointTemplate.skeleton = new SkeletonDefinition[] { };
perceptionCamera.AddLabeler(new BoundingBox2DLabeler(sampleIdLabelConfig));
perceptionCamera.AddLabeler(new RenderedObjectInfoLabeler(sampleIdLabelConfig));
perceptionCamera.AddLabeler(new KeypointLabeler()
{
idLabelConfig = sampleIdLabelConfig,
objectFilter = KeypointObjectFilter.Visible,
activeTemplate = emptyKeypointTemplate,
animationPoseConfigs = new List<AnimationPoseConfig>()
{
sampleAnimationPoseConfig, sampleAnimationPoseConfig
}
});
// Randomizer Serialization
var randomizers = new List<Randomizer>();
var testRandomizer = new AllMembersAndParametersTestRandomizer();
testRandomizer.colorRgbCategoricalParam.SetOptions(new (Color, float)[]
{
(Color.black, 0.4f),
(Color.blue, 0.93f),
(Color.red, 0.23f)
});
randomizers.Add(testRandomizer);
yield return null;
// Scenario Completed Serialization
var scenarioCompletedData = ScenarioCompletedData.FromCameraAndRandomizers(perceptionCamera, randomizers);
#region expectedRandomizerJson
var expectedRandomizerJson = RemoveWhitespace($@"
{{
""platform"": ""{Application.platform}"",
""perceptionCamera"": {{
""captureTriggerMode"": ""Scheduled"",
""startAtFrame"": 2,
""framesBetweenCaptures"": 10
}},
""labelers"": [
{{ ""name"": ""BoundingBox2DLabeler"", ""labelConfigCount"": 3, ""objectFilter"": """", ""animationPoseCount"": 0 }},
{{ ""name"": ""RenderedObjectInfoLabeler"", ""labelConfigCount"": 3, ""objectFilter"": """", ""animationPoseCount"": 0 }},
{{ ""name"": ""KeypointLabeler"", ""labelConfigCount"": 3, ""objectFilter"": ""Visible"", ""animationPoseCount"": 2 }}
],
""randomizers"": [
{{
""name"": ""AllMembersAndParametersTestRandomizer"",
""members"": [
{{
""name"": ""booleanMember"",
""value"": ""False"",
""type"": ""System.Boolean""
}},
{{
""name"": ""intMember"",
""value"": ""4"",
""type"": ""System.Int32""
}},
{{
""name"": ""uintMember"",
""value"": ""2"",
""type"": ""System.UInt32""
}},
{{
""name"": ""floatMember"",
""value"": ""5"",
""type"": ""System.Single""
}},
{{
""name"": ""vector2Member"",
""value"": ""(4.0, 7.0)"",
""type"": ""UnityEngine.Vector2""
}},
{{
""name"": ""unsupportedMember"",
""value"": ""UnityEngine.Perception.Randomization.Samplers.UniformSampler"",
""type"": ""UnityEngine.Perception.Randomization.Samplers.UniformSampler""
}}
],
""parameters"": [
{{
""name"": ""booleanParam"",
""type"": ""BooleanParameter"",
""fields"": [
{{
""name"": ""value"",
""distribution"": ""Constant"",
""value"": 1.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""floatParam"",
""type"": ""FloatParameter"",
""fields"": [
{{
""name"": ""value"",
""distribution"": ""AnimationCurve"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""integerParam"",
""type"": ""IntegerParameter"",
""fields"": [
{{
""name"": ""value"",
""distribution"": ""Uniform"",
""value"": 0.0,
""rangeMinimum"": -3.0,
""rangeMaximum"": 7.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""vector2Param"",
""type"": ""Vector2Parameter"",
""fields"": [
{{
""name"": ""x"",
""distribution"": ""Constant"",
""value"": 2.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""y"",
""distribution"": ""Uniform"",
""value"": 0.0,
""rangeMinimum"": -4.0,
""rangeMaximum"": 8.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""vector3Param"",
""type"": ""Vector3Parameter"",
""fields"": [
{{
""name"": ""x"",
""distribution"": ""Normal"",
""value"": 0.0,
""rangeMinimum"": -5.0,
""rangeMaximum"": 9.0,
""mean"": 4.0,
""stdDev"": 2.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""y"",
""distribution"": ""Constant"",
""value"": 3.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""z"",
""distribution"": ""AnimationCurve"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""vector4Param"",
""type"": ""Vector4Parameter"",
""fields"": [
{{
""name"": ""x"",
""distribution"": ""Normal"",
""value"": 0.0,
""rangeMinimum"": -5.0,
""rangeMaximum"": 9.0,
""mean"": 4.0,
""stdDev"": 2.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""y"",
""distribution"": ""Constant"",
""value"": 3.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""z"",
""distribution"": ""AnimationCurve"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}},
{{
""name"": ""w"",
""distribution"": ""Uniform"",
""value"": 0.0,
""rangeMinimum"": -12.0,
""rangeMaximum"": 42.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 0
}}
]
}},
{{
""name"": ""colorRgbCategoricalParam"",
""type"": ""ColorRgbCategoricalParameter"",
""fields"": [
{{
""name"": ""values"",
""distribution"": ""Categorical"",
""value"": 0.0,
""rangeMinimum"": 0.0,
""rangeMaximum"": 0.0,
""mean"": 0.0,
""stdDev"": 0.0,
""categoricalParameterCount"": 3
}}
]
}}
]
}}
]
}}
");
#endregion
var actualRandomizerJson = RemoveWhitespace(JsonConvert.SerializeObject(scenarioCompletedData));
Assert.AreEqual(expectedRandomizerJson, actualRandomizerJson);
Object.DestroyImmediate(emptyKeypointTemplate);
}
PerceptionCamera SetupPerceptionCamera()

210
com.unity.perception/Runtime/PerceptionAnalytics.cs


using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Simulation;
using UnityEngine.Analytics;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Scenarios;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UnityEngine.Perception.Analytics
{
/// <summary>
/// Editor and Runtime analytics for the Perception package.
/// </summary>
/// <remarks>
/// To add an event:
/// 1. Create a constant with the name of the event (eg: <see cref="k_EventScenarioInformation"/>)
/// 2. Add the constant to <see cref="allEvents" />
/// 3. Create a function that will report data for the event and at the start of it call
/// <see cref="TryRegisterPerceptionAnalyticsEvent" /> with the event name defined in step 1.
/// Note: Remember to use the conditional "#if UNITY_EDITOR" if adding editor analytics.
/// </remarks>
public static class PerceptionAnalytics
{
const string k_VendorKey = "unity.perception";
const int k_MaxElementsInStruct = 100;
const int k_MaxEventsPerHour = 100;
#region Setup
[RuntimeInitializeOnLoadMethod]
static void OnInitializeOnLoad()
{
Manager.Instance.ShutdownNotification += OnSimulationShutdown;
}
static void OnSimulationShutdown()
{
var perceptionCamera = Object.FindObjectOfType<PerceptionCamera>();
ReportScenarioInformation(
perceptionCamera,
ScenarioBase.activeScenario
);
}
#endregion
/// <summary>
/// Stores whether each event has been registered successfully or not.
/// </summary>
static Dictionary<AnalyticsEvent, bool> s_EventRegistrationStatus = new Dictionary<AnalyticsEvent, bool>();
#region Event Definitions
static readonly AnalyticsEvent k_EventScenarioInformation = new AnalyticsEvent(
"perceptionScenarioInformation", AnalyticsEventType.RuntimeAndEditor, 1
);
static readonly AnalyticsEvent k_EventRunInUnitySimulation = new AnalyticsEvent(
"runinunitysimulation", AnalyticsEventType.Editor, 1
);
/// <summary>
/// All supported events. If an event does not exist in this list, an error will be thrown during
/// <see cref="TryRegisterPerceptionAnalyticsEvent" />.
/// </summary>
static IEnumerable<AnalyticsEvent> allEvents => new[]
{
k_EventScenarioInformation,
k_EventRunInUnitySimulation
};
#endregion
#region Helpers
/// <summary>
/// Tries to register an event and returns whether it was registered successfully. The result is also cached in
/// the <see cref="s_EventRegistrationStatus" /> dictionary.
/// </summary>
/// <param name="theEvent">The name of the event.</param>
/// <returns>Whether the event was successfully registered/</returns>
static bool TryRegisterPerceptionAnalyticsEvent(AnalyticsEvent theEvent)
{
// Make sure the event exists in the dictionary
if (!s_EventRegistrationStatus.ContainsKey(theEvent))
{
if (allEvents.Contains(theEvent))
s_EventRegistrationStatus[theEvent] = false;
else
throw new NotSupportedException($"Unrecognized event {theEvent} not included in {nameof(allEvents)}.");
}
// If registered previously, return true
if (s_EventRegistrationStatus[theEvent])
return true;
// Try registering the event and update the dictionary accordingly
s_EventRegistrationStatus[theEvent] = true;
#if UNITY_EDITOR
var status = EditorAnalytics.RegisterEventWithLimit(theEvent.name, k_MaxEventsPerHour, k_MaxElementsInStruct, k_VendorKey);
#else
var status = UnityEngine.Analytics.Analytics.RegisterEvent(theEvent.name, k_MaxEventsPerHour, k_MaxElementsInStruct, k_VendorKey);
#endif
s_EventRegistrationStatus[theEvent] &= status == AnalyticsResult.Ok;
return s_EventRegistrationStatus[theEvent];
}
/// <summary>
/// Based on the value of type for <see cref="theEvent" />, sends an Editor Analytics event,
/// a Runtime Analytics event, or both.
/// </summary>
/// <param name="theEvent">The analytics event.</param>
/// <param name="data">Payload of the event.</param>
static void SendPerceptionAnalyticsEvent(AnalyticsEvent theEvent, object data)
{
#if UNITY_EDITOR
if (theEvent.type == AnalyticsEventType.Editor || theEvent.type == AnalyticsEventType.RuntimeAndEditor)
{
EditorAnalytics.SendEventWithLimit(theEvent.name, data, theEvent.versionId);
}
#else
if (theEvent.type == AnalyticsEventType.Runtime || theEvent.type == AnalyticsEventType.RuntimeAndEditor)
{
UnityEngine.Analytics.Analytics.SendEvent(theEvent.name, data, theEvent.versionId, theEvent.prefix);
}
#endif
}
#endregion
#region Event: Scenario Information
/// <summary>
/// Which labelers will be identified and included in the analytics information.
/// </summary>
public static readonly string[] labelerAllowList = new[]
{
"BoundingBox3DLabeler", "BoundingBox2DLabeler", "InstanceSegmentationLabeler",
"KeypointLabeler", "ObjectCountLabeler", "SemanticSegmentationLabeler", "RenderedObjectInfoLabeler"
};
static void ReportScenarioInformation(
PerceptionCamera cam,
ScenarioBase scenario
)
{
if (!TryRegisterPerceptionAnalyticsEvent(k_EventScenarioInformation))
return;
var randomizers = scenario ? scenario.activeRandomizers : new List<Randomizer>();
var data = ScenarioCompletedData.FromCameraAndRandomizers(cam, randomizers);
SendPerceptionAnalyticsEvent(k_EventScenarioInformation, data);
}
#endregion
#region Event: Run In Unity Simulation
public static void ReportRunInUnitySimulationStarted(Guid runId, int totalIterations, int instanceCount, string existingBuildId)
{
if (!TryRegisterPerceptionAnalyticsEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
totalIterations = totalIterations,
instanceCount = instanceCount,
existingBuildId = existingBuildId,
runStatus = RunStatus.Started.ToString()
};
SendPerceptionAnalyticsEvent(k_EventRunInUnitySimulation, data);
}
public static void ReportRunInUnitySimulationFailed(Guid runId, string errorMessage)
{
if (!TryRegisterPerceptionAnalyticsEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
errorMessage = errorMessage,
runStatus = RunStatus.Failed.ToString()
};
SendPerceptionAnalyticsEvent(k_EventRunInUnitySimulation, data);
}
public static void ReportRunInUnitySimulationSucceeded(Guid runId, string runExecutionId)
{
if (!TryRegisterPerceptionAnalyticsEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
runExecutionId = runExecutionId,
runStatus = RunStatus.Succeeded.ToString()
};
SendPerceptionAnalyticsEvent(k_EventRunInUnitySimulation, data);
}
#endregion
}
}

3
com.unity.perception/Runtime/PerceptionAnalytics.cs.meta


fileFormatVersion: 2
guid: 38657354e82b4c9b8370c9c55a39b1e3
timeCreated: 1629939608

360
com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Unity.Simulation;
using UnityEngine.Assertions;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;
namespace UnityEngine.Perception.Analytics
{
#region Common
enum AnalyticsEventType
{
Runtime,
Editor,
RuntimeAndEditor
}
struct AnalyticsEvent
{
public string name { get; private set; }
public AnalyticsEventType type { get; private set; }
public int versionId { get; private set; }
public string prefix { get; private set; }
public AnalyticsEvent(string name, AnalyticsEventType type, int versionId, string prefix = "")
{
this.name = name;
this.type = type;
this.versionId = versionId;
this.prefix = prefix;
}
}
#endregion
#region Run In Unity Simulation
[Serializable]
enum RunStatus
{
Started,
Failed,
Succeeded
}
[Serializable]
struct RunInUnitySimulationData
{
public string runId;
public int totalIterations;
public int instanceCount;
public string existingBuildId;
public string errorMessage;
public string runExecutionId;
public string runStatus;
}
#endregion
#region Scenario Information
[Serializable]
public class PerceptionCameraData
{
public string captureTriggerMode;
// same as "firstCaptureFrame" of the Perception Camera
public int startAtFrame;
public int framesBetweenCaptures;
}
[Serializable]
public class LabelerData
{
public string name;
public int labelConfigCount;
public string objectFilter = "";
public int animationPoseCount;
public static LabelerData FromLabeler(CameraLabeler labeler)
{
var labelerType = labeler.GetType();
var labelerName = labelerType.Name;
if (!PerceptionAnalytics.labelerAllowList.Contains(labelerName))
return null;
var labelerData = new LabelerData()
{
name = labelerName
};
switch (labeler)
{
case BoundingBox3DLabeler bb3dl:
labelerData.labelConfigCount = bb3dl.idLabelConfig.labelEntries.Count;
break;
case BoundingBox2DLabeler bb2dl:
labelerData.labelConfigCount = bb2dl.idLabelConfig.labelEntries.Count;
break;
case InstanceSegmentationLabeler isl:
labelerData.labelConfigCount = isl.idLabelConfig.labelEntries.Count;
break;
case KeypointLabeler kpl:
labelerData.labelConfigCount = kpl.idLabelConfig.labelEntries.Count;
labelerData.objectFilter = kpl.objectFilter.ToString();
labelerData.animationPoseCount = kpl.animationPoseConfigs.Count;
break;
case ObjectCountLabeler ocl:
labelerData.labelConfigCount = ocl.labelConfig.labelEntries.Count;
break;
case SemanticSegmentationLabeler ssl:
labelerData.labelConfigCount = ssl.labelConfig.labelEntries.Count;
break;
case RenderedObjectInfoLabeler rol:
labelerData.labelConfigCount = rol.idLabelConfig.labelEntries.Count;
break;
default:
labelerData = null;
break;
}
return labelerData;
}
}
[Serializable]
public class MemberData
{
public string name;
public string value;
public string type;
}
[Serializable]
public class ParameterField
{
public string name;
public string distribution;
public float value;
public float rangeMinimum;
public float rangeMaximum;
public float mean;
public float stdDev;
public int categoricalParameterCount;
public static ParameterField ExtractSamplerInformation(ISampler sampler, string fieldName = "value")
{
switch (sampler)
{
case AnimationCurveSampler _:
return new ParameterField()
{
name = fieldName,
distribution = "AnimationCurve"
};
case ConstantSampler cs:
return new ParameterField()
{
name = fieldName,
distribution = "Constant",
value = cs.value
};
case NormalSampler ns:
return new ParameterField()
{
name = fieldName,
distribution = "Normal",
mean = ns.mean,
stdDev = ns.standardDeviation,
rangeMinimum = ns.range.minimum,
rangeMaximum = ns.range.maximum
};
case UniformSampler us:
return new ParameterField()
{
name = fieldName,
distribution = "Uniform",
rangeMinimum = us.range.minimum,
rangeMaximum = us.range.maximum
};
default:
return null;
}
}
public static List<ParameterField> ExtractSamplersInformation(IEnumerable<ISampler> samplers, IEnumerable<string> indexToNameMapping)
{
var samplersList = samplers.ToList();
var indexToNameMappingList = indexToNameMapping.ToList();
if (samplersList.Count > indexToNameMappingList.Count)
throw new Exception("Insufficient names provided for mapping ParameterFields");
return samplersList.Select((t, i) => ExtractSamplerInformation(t, indexToNameMappingList[i])).ToList();
}
}
[Serializable]
public class ParameterData
{
public string name;
public string type;
public List<ParameterField> fields;
}
[Serializable]
public class RandomizerData
{
public string name;
public MemberData[] members;
public ParameterData[] parameters;
public static RandomizerData FromRandomizer(Randomizer randomizer)
{
if (randomizer == null)
return null;
var data = new RandomizerData()
{
name = randomizer.GetType().Name,
};
var randomizerType = randomizer.GetType();
// Filter out randomizers which could be considered personally identifiable.
if (randomizerType.Namespace == null || !randomizerType.Namespace.StartsWith("UnityEngine.Perception"))
return null;
// Naming configuration
var vectorFieldNames = new[] { "x", "y", "z", "w" };
var hsvaFieldNames = new[] { "hue", "saturation", "value", "alpha" };
var rgbFieldNames = new[] { "red", "green", "blue" };
// Add member fields and parameters separately
var members = new List<MemberData>();
var parameters = new List<ParameterData>();
foreach (var field in randomizerType.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
var member = field.GetValue(randomizer);
var memberType = member.GetType();
// If member is either a categorical or numeric parameter
if (memberType.IsSubclassOf(typeof(Parameter)))
{
var pd = new ParameterData()
{
name = field.Name,
type = memberType.Name,
fields = new List<ParameterField>()
};
// All included parameter types
switch (member)
{
case CategoricalParameterBase cp:
pd.fields.Add(new ParameterField()
{
name = "values",
distribution = "Categorical",
categoricalParameterCount = cp.probabilities.Count
});
break;
case BooleanParameter bP:
pd.fields.Add(ParameterField.ExtractSamplerInformation(bP.value));
break;
case FloatParameter fP:
pd.fields.Add(ParameterField.ExtractSamplerInformation(fP.value));
break;
case IntegerParameter iP:
pd.fields.Add(ParameterField.ExtractSamplerInformation(iP.value));
break;
case Vector2Parameter v2P:
pd.fields.AddRange(ParameterField.ExtractSamplersInformation(v2P.samplers, vectorFieldNames));
break;
case Vector3Parameter v3P:
pd.fields.AddRange(ParameterField.ExtractSamplersInformation(v3P.samplers, vectorFieldNames));
break;
case Vector4Parameter v4P:
pd.fields.AddRange(ParameterField.ExtractSamplersInformation(v4P.samplers, vectorFieldNames));
break;
case ColorHsvaParameter hsvaP:
pd.fields.AddRange(ParameterField.ExtractSamplersInformation(hsvaP.samplers, hsvaFieldNames));
break;
case ColorRgbParameter rgbP:
pd.fields.AddRange(ParameterField.ExtractSamplersInformation(rgbP.samplers, rgbFieldNames));
break;
}
parameters.Add(pd);
}
else
{
members.Add(new MemberData()
{
name = field.Name,
type = memberType.FullName,
value = member.ToString()
});
}
}
data.members = members.ToArray();
data.parameters = parameters.ToArray();
return data;
}
}
[Serializable]
public class ScenarioCompletedData
{
public string platform;
public PerceptionCameraData perceptionCamera;
public LabelerData[] labelers;
public RandomizerData[] randomizers;
internal static ScenarioCompletedData FromCameraAndRandomizers(
PerceptionCamera cam,
IEnumerable<Randomizer> randomizers
)
{
var data = new ScenarioCompletedData()
{
platform = (Configuration.Instance.IsSimulationRunningInCloud()) ? "USim": Application.platform.ToString()
};
if (cam != null)
{
// Perception Camera Data
data.perceptionCamera = new PerceptionCameraData()
{
captureTriggerMode = cam.captureTriggerMode.ToString(),
startAtFrame = cam.firstCaptureFrame,
framesBetweenCaptures = cam.framesBetweenCaptures
};
// Labeler Data
data.labelers = cam.labelers
.Select(LabelerData.FromLabeler)
.Where(labeler => labeler != null).ToArray();
}
var randomizerList = randomizers.ToArray();
if (randomizerList.Any())
{
data.randomizers = randomizerList
.Select(RandomizerData.FromRandomizer)
.Where(x => x != null).ToArray();
}
else
{
data.randomizers = new RandomizerData[] { };
}
return data;
}
}
#endregion
}

3
com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs.meta


fileFormatVersion: 2
guid: 78500bfc6066482ca6af77c3586dcbd2
timeCreated: 1632680191

50
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/AllMembersAndParametersTestRandomizer.cs


using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;
namespace UnityEngine.Perception.RandomizationTests.ScenarioTests
{
public class AllMembersAndParametersTestRandomizer: Randomizer
{
// Members
public bool booleanMember = false;
public int intMember = 4;
public uint uintMember = 2;
public float floatMember = 5;
public Vector2 vector2Member = new Vector2(4, 7);
public UniformSampler unsupportedMember = new UniformSampler();
// Parameters
public BooleanParameter booleanParam = new BooleanParameter()
{
value = new ConstantSampler(1)
};
public FloatParameter floatParam = new FloatParameter()
{
value = new AnimationCurveSampler()
};
public IntegerParameter integerParam = new IntegerParameter()
{
value = new UniformSampler(-3, 7)
};
public Vector2Parameter vector2Param = new Vector2Parameter()
{
x = new ConstantSampler(2),
y = new UniformSampler(-4, 8)
};
public Vector3Parameter vector3Param = new Vector3Parameter()
{
x = new NormalSampler(-5, 9, 4, 2),
y = new ConstantSampler(3),
z = new AnimationCurveSampler()
};
public Vector4Parameter vector4Param = new Vector4Parameter()
{
x = new NormalSampler(-5, 9, 4, 2),
y = new ConstantSampler(3),
z = new AnimationCurveSampler(),
w = new UniformSampler(-12, 42)
};
public ColorRgbCategoricalParameter colorRgbCategoricalParam = new ColorRgbCategoricalParameter();
};
}

11
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/AllMembersAndParametersTestRandomizer.cs.meta


fileFormatVersion: 2
guid: 5dcdd497a48d3405489861ef856a8495
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

24
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleAnimationPoseConfig.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4c69656f5dd14516a3a18e42b3b43a4e, type: 3}
m_Name: sampleAnimationPoseConfig
m_EditorClassIdentifier:
animationClip: {fileID: 0}
m_Timestamps:
- startOffsetPercent: 0
poseLabel: a
- startOffsetPercent: 24
poseLabel: b
- startOffsetPercent: 45
poseLabel: c
- startOffsetPercent: 89
poseLabel: d

8
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleAnimationPoseConfig.asset.meta


fileFormatVersion: 2
guid: 92646dbc24d95413084b0787214fdcb3
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

23
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleIdLabelConfig.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2f09f279848e42cea259348b13bce4c5, type: 3}
m_Name: sampleIdLabelConfig
m_EditorClassIdentifier:
m_LabelEntries:
- label: Test 1
id: 1
- label: Test 2
id: 2
- label: Test 3
id: 3
autoAssignIds: 1
startingLabelId: 1

8
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/sampleIdLabelConfig.asset.meta


fileFormatVersion: 2
guid: c3dce9f8036624486bdc1e97f0934aa1
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

8
TestProjects/PerceptionHDRP/Assets/Scenes.meta


fileFormatVersion: 2
guid: 98cfbce5bd48d9d4d99e5c8b8a1ee97b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

3
com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs.meta


fileFormatVersion: 2
guid: 3216e9e700a34acc8ac808ffccb8297c
timeCreated: 1607709612

99
com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs


using System;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Analytics;
namespace UnityEditor.Perception.Randomization
{
static class PerceptionEditorAnalytics
{
const string k_VendorKey = "unity.perception";
const string k_RunInUnitySimulationName = "runinunitysimulation";
static int k_MaxItems = 100;
static int k_MaxEventsPerHour = 100;
static bool s_IsRegistered;
static bool TryRegisterEvents()
{
if (s_IsRegistered)
return true;
var success = true;
success &= EditorAnalytics.RegisterEventWithLimit(k_RunInUnitySimulationName, k_MaxEventsPerHour, k_MaxItems,
k_VendorKey) == AnalyticsResult.Ok;
s_IsRegistered = success;
return success;
}
public static void ReportRunInUnitySimulationStarted(Guid runId, int totalIterations, int instanceCount, string existingBuildId)
{
if (!TryRegisterEvents())
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
totalIterations = totalIterations,
instanceCount = instanceCount,
existingBuildId = existingBuildId,
runStatus = RunStatus.Started.ToString()
};
EditorAnalytics.SendEventWithLimit(k_RunInUnitySimulationName, data);
}
public static void ReportRunInUnitySimulationFailed(Guid runId, string errorMessage)
{
if (!TryRegisterEvents())
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
errorMessage = errorMessage,
runStatus = RunStatus.Failed.ToString()
};
EditorAnalytics.SendEventWithLimit(k_RunInUnitySimulationName, data);
}
public static void ReportRunInUnitySimulationSucceeded(Guid runId, string runExecutionId)
{
if (!TryRegisterEvents())
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
runExecutionId = runExecutionId,
runStatus = RunStatus.Succeeded.ToString()
};
EditorAnalytics.SendEventWithLimit(k_RunInUnitySimulationName, data);
}
enum RunStatus
{
Started,
Failed,
Succeeded
}
struct RunInUnitySimulationData
{
[UsedImplicitly]
public string runId;
[UsedImplicitly]
public int totalIterations;
[UsedImplicitly]
public int instanceCount;
[UsedImplicitly]
public string existingBuildId;
[UsedImplicitly]
public string errorMessage;
[UsedImplicitly]
public string runExecutionId;
[UsedImplicitly]
public string runStatus;
}
}
}
正在加载...
取消
保存