浏览代码

merged both runtime and editor analytics into one file/implementation

/0.9.0.preview.1_staging
Aryan Mann 3 年前
当前提交
6d2ca06a
共有 16 个文件被更改,包括 841 次插入484 次删除
  1. 8
      com.unity.perception/Editor/Randomization/Editors/RunInUnitySimulationWindow.cs
  2. 3
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  3. 4
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/AllMembersAndParametersTestRandomizer.cs
  4. 83
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs
  5. 51
      TestProjects/PerceptionHDRP/Assets/Scenes/AllMembersAndParametersTestRandomizer.cs
  6. 3
      TestProjects/PerceptionHDRP/Assets/Scenes/AllMembersAndParametersTestRandomizer.cs.meta
  7. 1
      TestProjects/PerceptionHDRP/randomizerAnalyticsSerializationExample.json
  8. 232
      com.unity.perception/Runtime/PerceptionAnalytics.cs
  9. 283
      com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs
  10. 3
      com.unity.perception/Runtime/PerceptionAnalyticsStructs.cs.meta
  11. 209
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/randomizerAnalyticsSerializationExample.json
  12. 7
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/randomizerAnalyticsSerializationExample.json.meta
  13. 3
      com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs.meta
  14. 99
      com.unity.perception/Editor/Randomization/Editors/PerceptionEditorAnalytics.cs
  15. 336
      com.unity.perception/Runtime/PerceptionEngineAnalytics.cs
  16. 0
      /com.unity.perception/Runtime/PerceptionAnalytics.cs.meta

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);

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


using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.Perception.Analytics;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;

.Select(obj => obj.GetComponentInChildren<PerceptionCamera>())
.FirstOrDefault(x => x != null);
PerceptionEngineAnalytics.ReportScenarioCompleted(
PerceptionAnalytics.ReportScenarioCompleted(
perceptionCamera,
activeScenario.randomizers
);

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


using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;

public uint uintMember = 2;
public float floatMember = 5;
public Vector2 vector2Member = new Vector2(4, 7);
public PerceptionEngineAnalytics.MemberData unsupportedMember = new PerceptionEngineAnalytics.MemberData();
public UniformSampler unsupportedMember = new UniformSampler();
// Parameters
public BooleanParameter booleanParam = new BooleanParameter()
{

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


using NUnit.Framework;
using UnityEngine;
using UnityEngine.Perception;
using UnityEngine.Perception.Analytics;
using UnityEngine.Perception.Randomization.Randomizers.SampleRandomizers;
using UnityEngine.Perception.Randomization.Samplers;
using UnityEngine.Perception.GroundTruth;

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

m_TestObject = new GameObject();
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);

(Color.red, 0.23f)
});
var randomizerData =
PerceptionEngineAnalytics.RandomizerData.FromRandomizer(testRandomizer);
RandomizerData.FromRandomizer(testRandomizer);
/*
new PerceptionEngineAnalytics.RandomizerData()
new RandomizerData()
new PerceptionEngineAnalytics.MemberData()
new MemberData()
new PerceptionEngineAnalytics.MemberData()
new MemberData()
new PerceptionEngineAnalytics.MemberData()
new MemberData()
new PerceptionEngineAnalytics.MemberData()
new MemberData()
new PerceptionEngineAnalytics.MemberData()
new MemberData()
new PerceptionEngineAnalytics.MemberData()
new MemberData()
{
name = "unsupportedMember",
type = "UnityEngine.Perception.PerceptionEngineAnalytics+MemberData",

parameters = new[]
{
new PerceptionEngineAnalytics.ParameterData()
new ParameterData()
fields = new List<PerceptionEngineAnalytics.ParameterField>()
fields = new List<ParameterField>()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "Constant",
name = "value",

},
new PerceptionEngineAnalytics.ParameterData()
new ParameterData()
fields = new List<PerceptionEngineAnalytics.ParameterField>()
fields = new List<ParameterField>()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "AnimationCurve",
name = "value",

new PerceptionEngineAnalytics.ParameterData()
new ParameterData()
fields = new List<PerceptionEngineAnalytics.ParameterField>()
fields = new List<ParameterField>()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "Uniform",
name = "value",

},
new PerceptionEngineAnalytics.ParameterData()
new ParameterData()
fields = new List<PerceptionEngineAnalytics.ParameterField>()
fields = new List<ParameterField>()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "Uniform",
name = "y",

},
new PerceptionEngineAnalytics.ParameterData()
new ParameterData()
fields = new List<PerceptionEngineAnalytics.ParameterField>()
fields = new List<ParameterField>()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "Normal",
name = "x",

new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "AnimationCurve",
name = "z",

new PerceptionEngineAnalytics.ParameterData()
new ParameterData()
fields = new List<PerceptionEngineAnalytics.ParameterField>()
fields = new List<ParameterField>()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "Normal",
name = "x",

new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "Uniform",
name = "w",

},
new PerceptionEngineAnalytics.ParameterData()
new ParameterData()
fields = new List<PerceptionEngineAnalytics.ParameterField>()
fields = new List<ParameterField>()
new PerceptionEngineAnalytics.ParameterField()
new ParameterField()
{
distribution = "Categorical",
name = "values",

}
}
};
*/
var expectedSerializedValueJson = JsonConvert.SerializeObject(expectedSerializedValue);
var serializedValueJson = JsonConvert.SerializeObject(randomizerData);
var expectedSerializedValueJson = RemoveWhitespace(((TextAsset)Resources.Load("randomizerAnalyticsSerializationExample")).text);
var serializedValueJson = RemoveWhitespace(JsonConvert.SerializeObject(randomizerData));
Assert.AreEqual(expectedSerializedValueJson, serializedValueJson);
}

51
TestProjects/PerceptionHDRP/Assets/Scenes/AllMembersAndParametersTestRandomizer.cs


using UnityEngine.Perception.Analytics;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;
namespace UnityEngine.Perception
{
[AddRandomizerMenu("Test/All Members And Parameters")]
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 MemberData unsupportedMember = new MemberData();
// 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();
};
}

3
TestProjects/PerceptionHDRP/Assets/Scenes/AllMembersAndParametersTestRandomizer.cs.meta


fileFormatVersion: 2
guid: 03efc69674f647478374cd5d8f746529
timeCreated: 1632447484

1
TestProjects/PerceptionHDRP/randomizerAnalyticsSerializationExample.json


{"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.PerceptionEngineAnalytics+MemberData","type":"UnityEngine.Perception.PerceptionEngineAnalytics+MemberData"}],"parameters":[{"name":"booleanParam","type":"BooleanParameter","fields":[{"name":"value","distribution":"Constant","value":1.0,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648}]},{"name":"floatParam","type":"FloatParameter","fields":[{"name":"value","distribution":"AnimationCurve","value":-2.14748365E+09,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648}]},{"name":"integerParam","type":"IntegerParameter","fields":[{"name":"value","distribution":"Uniform","value":-2.14748365E+09,"rangeMinimum":-3.0,"rangeMaximum":7.0,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648}]},{"name":"vector2Param","type":"Vector2Parameter","fields":[{"name":"x","distribution":"Constant","value":2.0,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648},{"name":"y","distribution":"Uniform","value":-2.14748365E+09,"rangeMinimum":-4.0,"rangeMaximum":8.0,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648}]},{"name":"vector3Param","type":"Vector3Parameter","fields":[{"name":"x","distribution":"Normal","value":-2.14748365E+09,"rangeMinimum":-5.0,"rangeMaximum":9.0,"mean":4.0,"stdDev":2.0,"categoricalParameterCount":-2147483648},{"name":"y","distribution":"Constant","value":3.0,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648},{"name":"z","distribution":"AnimationCurve","value":-2.14748365E+09,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648}]},{"name":"vector4Param","type":"Vector4Parameter","fields":[{"name":"x","distribution":"Normal","value":-2.14748365E+09,"rangeMinimum":-5.0,"rangeMaximum":9.0,"mean":4.0,"stdDev":2.0,"categoricalParameterCount":-2147483648},{"name":"y","distribution":"Constant","value":3.0,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648},{"name":"z","distribution":"AnimationCurve","value":-2.14748365E+09,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648},{"name":"w","distribution":"Uniform","value":-2.14748365E+09,"rangeMinimum":-12.0,"rangeMaximum":42.0,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":-2147483648}]},{"name":"colorRgbCategoricalParam","type":"ColorRgbCategoricalParameter","fields":[{"name":"values","distribution":"Categorical","value":-2.14748365E+09,"rangeMinimum":-2.14748365E+09,"rangeMaximum":-2.14748365E+09,"mean":-2.14748365E+09,"stdDev":-2.14748365E+09,"categoricalParameterCount":3}]}]}

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


using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Analytics;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Randomizers;
#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="TryRegisterEvent" /> 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
{
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 AnalyticsEvent(string name, AnalyticsEventType type, int versionId)
{
this.name = name;
this.type = type;
this.versionId = versionId;
}
}
const string k_VendorKey = "unity.perception";
const int k_MaxElementsInStruct = 100;
const int k_MaxEventsPerHour = 100;
static Dictionary<AnalyticsEvent, bool> s_EventRegistrationStatus = new Dictionary<AnalyticsEvent, bool>();
#region Event Definition
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="TryRegisterEvent" />.
/// </summary>
static IEnumerable<AnalyticsEvent> allEvents => new[]
{
k_EventScenarioInformation,
k_EventRunInUnitySimulation
};
#endregion
#region Common
/// <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 TryRegisterEvent(AnalyticsEvent theEvent)
{
if (!s_EventRegistrationStatus.ContainsKey(theEvent))
{
if (allEvents.Contains(theEvent))
s_EventRegistrationStatus[theEvent] = false;
else
Debug.LogError($"Unrecognized event {theEvent} not included in {nameof(allEvents)}.");
}
if (s_EventRegistrationStatus[theEvent])
return true;
s_EventRegistrationStatus[theEvent] = true;
#if UNITY_EDITOR
var status = EditorAnalytics.RegisterEventWithLimit(theEvent.name, k_MaxEventsPerHour, k_MaxElementsInStruct, k_VendorKey);
#else
var status = Analytics.Analytics.RegisterEvent(theEvent.name, k_MaxEventsPerHour, k_MaxElementsInStruct, k_VendorKey);
#endif
s_EventRegistrationStatus[theEvent] &= status == AnalyticsResult.Ok;
Debug.Log($"Registering event {theEvent.name}. Operation {(s_EventRegistrationStatus[theEvent] ? "" : "un")}successful.");
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>
/// <param name="prefix">If Runtime event, what is the prefix ("domain.schema")?</param>
static void SendEvent(AnalyticsEvent theEvent, object data, string prefix = "")
{
Debug.Log($"Reporting {theEvent.name}.");
#if UNITY_EDITOR
if (theEvent.type == AnalyticsEventType.Editor || theEvent.type == AnalyticsEventType.RuntimeAndEditor)
{
EditorAnalytics.SendEventWithLimit(theEvent.name, data, theEvent.versionId);
}
#endif
if (theEvent.type == AnalyticsEventType.Runtime || theEvent.type == AnalyticsEventType.RuntimeAndEditor)
{
UnityEngine.Analytics.Analytics.SendEvent(theEvent.name, data, theEvent.versionId, prefix);
}
}
#endregion
#region Event: Scenario Information
/// <summary>
/// Which labelers will be identified and included in the analytics information.
/// </summary>
public static readonly Type[] labelerAllowList = new[]
{
typeof(BoundingBox3DLabeler), typeof(BoundingBox2DLabeler), typeof(InstanceSegmentationLabeler),
typeof(KeypointLabeler), typeof(ObjectCountLabeler), typeof(SemanticSegmentationLabeler)
};
internal static void ReportScenarioCompleted(PerceptionCamera cam, IEnumerable<Randomizer> randomizers)
{
try
{
if (!TryRegisterEvent(k_EventScenarioInformation))
return;
var data = new ScenarioCompletedData();
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();
}
SendEvent(k_EventScenarioInformation, data);
}
catch
{
//ignored
}
}
#endregion
#region Event: Run In Unity Simulation
public static void ReportRunInUnitySimulationStarted(Guid runId, int totalIterations, int instanceCount, string existingBuildId)
{
if (!TryRegisterEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
totalIterations = totalIterations,
instanceCount = instanceCount,
existingBuildId = existingBuildId,
runStatus = RunStatus.Started.ToString()
};
SendEvent(k_EventRunInUnitySimulation, data);
}
public static void ReportRunInUnitySimulationFailed(Guid runId, string errorMessage)
{
if (!TryRegisterEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
errorMessage = errorMessage,
runStatus = RunStatus.Failed.ToString()
};
SendEvent(k_EventRunInUnitySimulation, data);
}
public static void ReportRunInUnitySimulationSucceeded(Guid runId, string runExecutionId)
{
if (!TryRegisterEvent(k_EventRunInUnitySimulation))
return;
var data = new RunInUnitySimulationData
{
runId = runId.ToString(),
runExecutionId = runExecutionId,
runStatus = RunStatus.Succeeded.ToString()
};
SendEvent(k_EventRunInUnitySimulation, data);
}
#endregion
}
}

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;
namespace UnityEngine.Perception.Analytics
{
#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;
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();
if (!PerceptionAnalytics.labelerAllowList.Contains(labelerType))
return null;
var labelerData = new LabelerData()
{
name = labeler.GetType().Name
};
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;
}
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 = int.MinValue;
public float rangeMinimum = int.MinValue;
public float rangeMaximum = int.MinValue;
public float mean = int.MinValue;
public float stdDev = int.MinValue;
public int categoricalParameterCount = int.MinValue;
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();
// Only looks for randomizers included by the Perception package.
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;
}
#endregion
}

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


fileFormatVersion: 2
guid: 78500bfc6066482ca6af77c3586dcbd2
timeCreated: 1632680191

209
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/randomizerAnalyticsSerializationExample.json


{
"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":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
}
]
},
{
"name":"floatParam",
"type":"FloatParameter",
"fields":[
{
"name":"value",
"distribution":"AnimationCurve",
"value":-2.14748365E+09,
"rangeMinimum":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
}
]
},
{
"name":"integerParam",
"type":"IntegerParameter",
"fields":[
{
"name":"value",
"distribution":"Uniform",
"value":-2.14748365E+09,
"rangeMinimum":-3.0,
"rangeMaximum":7.0,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
}
]
},
{
"name":"vector2Param",
"type":"Vector2Parameter",
"fields":[
{
"name":"x",
"distribution":"Constant",
"value":2.0,
"rangeMinimum":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
},
{
"name":"y",
"distribution":"Uniform",
"value":-2.14748365E+09,
"rangeMinimum":-4.0,
"rangeMaximum":8.0,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
}
]
},
{
"name":"vector3Param",
"type":"Vector3Parameter",
"fields":[
{
"name":"x",
"distribution":"Normal",
"value":-2.14748365E+09,
"rangeMinimum":-5.0,
"rangeMaximum":9.0,
"mean":4.0,
"stdDev":2.0,
"categoricalParameterCount":-2147483648
},
{
"name":"y",
"distribution":"Constant",
"value":3.0,
"rangeMinimum":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
},
{
"name":"z",
"distribution":"AnimationCurve",
"value":-2.14748365E+09,
"rangeMinimum":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
}
]
},
{
"name":"vector4Param",
"type":"Vector4Parameter",
"fields":[
{
"name":"x",
"distribution":"Normal",
"value":-2.14748365E+09,
"rangeMinimum":-5.0,
"rangeMaximum":9.0,
"mean":4.0,
"stdDev":2.0,
"categoricalParameterCount":-2147483648
},
{
"name":"y",
"distribution":"Constant",
"value":3.0,
"rangeMinimum":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
},
{
"name":"z",
"distribution":"AnimationCurve",
"value":-2.14748365E+09,
"rangeMinimum":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
},
{
"name":"w",
"distribution":"Uniform",
"value":-2.14748365E+09,
"rangeMinimum":-12.0,
"rangeMaximum":42.0,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":-2147483648
}
]
},
{
"name":"colorRgbCategoricalParam",
"type":"ColorRgbCategoricalParameter",
"fields":[
{
"name":"values",
"distribution":"Categorical",
"value":-2.14748365E+09,
"rangeMinimum":-2.14748365E+09,
"rangeMaximum":-2.14748365E+09,
"mean":-2.14748365E+09,
"stdDev":-2.14748365E+09,
"categoricalParameterCount":3
}
]
}
]
}

7
com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/Resources/randomizerAnalyticsSerializationExample.json.meta


fileFormatVersion: 2
guid: cc8e181627cad4a7d99440e32e0c03ea
TextScriptImporter:
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;
}
}
}

336
com.unity.perception/Runtime/PerceptionEngineAnalytics.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;
namespace UnityEngine.Perception
{
public static class PerceptionEngineAnalytics
{
const string k_VendorKey = "unity.perception";
const int k_MaxElementsInStruct = 100;
const int k_MaxEventsPerHour = 100;
static bool s_AllEventsRegistered;
// Runtime Events
static string[] allEvents => new[] { k_EventScenarioCompleted };
const string k_EventScenarioCompleted = "perceptionScenarioCompleted";
#region Event: Scenario Completed
/// <summary>
/// Which labelers will be identified and included in the analytics information.
/// </summary>
static readonly Type[] k_LabelerAllowList = new[]
{
typeof(BoundingBox3DLabeler), typeof(BoundingBox2DLabeler), typeof(InstanceSegmentationLabeler),
typeof(KeypointLabeler), typeof(ObjectCountLabeler), typeof(SemanticSegmentationLabeler)
};
#region Data Structures
[Serializable]
public class PerceptionCameraData
{
public string captureTriggerMode;
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();
if (!k_LabelerAllowList.Contains(labelerType))
return null;
var labelerData = new LabelerData()
{
name = labeler.GetType().Name
};
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;
}
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 = int.MinValue;
public float rangeMinimum = int.MinValue;
public float rangeMaximum = int.MinValue;
public float mean = int.MinValue;
public float stdDev = int.MinValue;
public int categoricalParameterCount = int.MinValue;
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();
// Only looks for randomizers included by the Perception package.
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;
}
#endregion
internal static void ReportScenarioCompleted(PerceptionCamera cam, IEnumerable<Randomizer> randomizers)
{
if (!TryRegisterEvents())
return;
var data = new ScenarioCompletedData();
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();
}
EditorAnalytics.SendEventWithLimit(k_EventScenarioCompleted, data);
}
#endregion
static bool TryRegisterEvents()
{
if (s_AllEventsRegistered)
return true;
var success = true;
foreach (var eventName in allEvents)
{
success &= EditorAnalytics.RegisterEventWithLimit(eventName, k_MaxEventsPerHour, k_MaxElementsInStruct, k_VendorKey) == AnalyticsResult.Ok;
}
s_AllEventsRegistered = success;
return success;
}
}
}

/com.unity.perception/Runtime/PerceptionEngineAnalytics.cs.meta → /com.unity.perception/Runtime/PerceptionAnalytics.cs.meta

正在加载...
取消
保存