浏览代码

finished engine analytics implementation v1

/package-analytics
Aryan Mann 3 年前
当前提交
0f5df9bf
共有 9 个文件被更改,包括 559 次插入99 次删除
  1. 2
      TestProjects/PerceptionHDRP/Packages/manifest.json
  2. 7
      TestProjects/PerceptionHDRP/Packages/packages-lock.json
  3. 354
      com.unity.perception/Runtime/PerceptionEngineAnalytics.cs
  4. 210
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs
  5. 50
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/AllMembersAndParametersTestRandomizer.cs
  6. 11
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/AllMembersAndParametersTestRandomizer.cs.meta
  7. 8
      TestProjects/PerceptionHDRP/Assets/Scenes/SampleScene.meta
  8. 8
      TestProjects/PerceptionHDRP/Assets/StreamingAssets.meta
  9. 8
      com.unity.perception/Editor/Pyrception.meta

2
TestProjects/PerceptionHDRP/Packages/manifest.json


"com.unity.ide.vscode": "1.2.3",
"com.unity.package-validation-suite": "0.9.1-preview",
"com.unity.perception": "file:../../../com.unity.perception",
"com.unity.perception.rlib": "https://github.cds.internal.unity3d.com/unity/com.unity.perception.rlib.git?path=/com.unity.perception.rlib#master",
"com.unity.scripting.python": "4.0.0-exp.5",
"com.unity.simulation.capture": "0.0.10-preview.16",
"com.unity.simulation.core": "0.0.10-preview.21",
"com.unity.test-framework": "1.1.20",

7
TestProjects/PerceptionHDRP/Packages/packages-lock.json


},
"url": "https://packages.unity.com"
},
"com.unity.scripting.python": {
"version": "4.0.0-exp.5",
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.shadergraph": {
"version": "7.3.1",
"depth": 1,

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


using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine.Perception.Randomization.Parameters;
using EngineAnalytics = UnityEngine.Analytics.Analytics;
using UnityEngine.Perception.Randomization.Samplers;
static class PerceptionEngineAnalytics
public static class PerceptionEngineAnalytics
// Events
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
/// When a Scenario completes in editor, player, or USim
/// Which labelers will be identified and included in the analytics information.
const string k_ScenarioCompletedName = "perceptionscenariocompleted";
/*
static int k_MaxItems = 100;
static int k_MaxEventsPerHour = 100;
*/
static bool s_IsRegistered;
static readonly string[] LabelAllowlist = new[]
static readonly Type[] k_LabelerAllowList = new[]
"BoundingBox2DLabeler"
typeof(BoundingBox3DLabeler), typeof(BoundingBox2DLabeler), typeof(InstanceSegmentationLabeler),
typeof(KeypointLabeler), typeof(ObjectCountLabeler), typeof(SemanticSegmentationLabeler)
static bool TryRegisterEvents()
{
if (s_IsRegistered)
return true;
#region Data Structures
var success = true;
/*
success &= EngineAnalytics.RegisterEvent(k_ScenarioCompletedName, k_MaxEventsPerHour, k_MaxItems,
k_VendorKey) == AnalyticsResult.Ok;
*/
s_IsRegistered = success;
return success;
}
#region perceptionscenariocompleted
struct PerceptionCameraData
[Serializable]
public class PerceptionCameraData
[UsedImplicitly]
[UsedImplicitly]
[UsedImplicitly]
/*struct LabelerData
[Serializable]
public class LabelerData
[UsedImplicitly]
[UsedImplicitly]
}*/
struct MemberData
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
[UsedImplicitly]
[UsedImplicitly]
public object value;
public string value;
public string type;
struct ParameterData
[Serializable]
public class ParameterField
[UsedImplicitly]
[UsedImplicitly]
[UsedImplicitly]
public (string name, string distribution)[] values;
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();
}
struct RandomizerData
[Serializable]
public class ParameterData
[UsedImplicitly]
[UsedImplicitly]
public string type;
public List<ParameterField> fields;
}
[Serializable]
public class RandomizerData
{
public string name;
[UsedImplicitly]
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;
}
struct ScenarioCompletedData
[Serializable]
public class ScenarioCompletedData
[UsedImplicitly]
public string platform;
[UsedImplicitly]
public string[] labelers;
[UsedImplicitly]
public LabelerData[] labelers;
public static void ReportScenarioCompleted(PerceptionCamera cam, IEnumerable<Randomizer> randomizers)
#endregion
internal static void ReportScenarioCompleted(PerceptionCamera cam, IEnumerable<Randomizer> randomizers)
var randomizer = randomizers.First();
var randomizerType = randomizer.GetType();
var randomizerName = randomizerType.Name;
var randomizerFields = randomizerType.GetFields(BindingFlags.Public | BindingFlags.Instance);
var data = new ScenarioCompletedData()
var data = new ScenarioCompletedData();
if (cam != null)
perceptionCamera = (cam == null)
? new PerceptionCameraData()
: new PerceptionCameraData()
// Perception Camera Data
data.perceptionCamera = new PerceptionCameraData()
},
labelers = (cam == null)
? new string[]{}
: cam.labelers
.Select(labeler => labeler.GetType().Name)
.Where(labeler => LabelAllowlist.Contains(labeler)).ToArray(),
randomizers = null
};
// EngineAnalytics.SendEvent(k_ScenarioCompletedName, data);
};
// 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);
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;
}
}
}

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


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

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

yield return null;
for (var i = 0; i < 3; i++)
Assert.AreNotEqual(seeds[i], SamplerState.NextRandomState());
}
[Test]
public void ScenarioCompletedAnalyticsSerializesCorrectly()
{
// Setup test randomizer
var testRandomizer = new AllMembersAndParametersTestRandomizer();
testRandomizer.colorRgbCategoricalParam.SetOptions(new (Color, float)[]
{
(Color.black, 0.4f),
(Color.blue, 0.93f),
(Color.red, 0.23f)
});
var randomizerData =
PerceptionEngineAnalytics.RandomizerData.FromRandomizer(testRandomizer);
Assert.IsTrue(randomizerData != null);
// Parameters
var expectedSerializedValue =
new PerceptionEngineAnalytics.RandomizerData()
{
name = nameof(AllMembersAndParametersTestRandomizer),
members = new[]
{
new PerceptionEngineAnalytics.MemberData()
{
name = "booleanMember",
type = "System.Boolean",
value = "False"
},
new PerceptionEngineAnalytics.MemberData()
{
name = "intMember",
type = "System.Int32",
value = "4"
},
new PerceptionEngineAnalytics.MemberData()
{
name = "uintMember",
type = "System.UInt32",
value = "2"
},
new PerceptionEngineAnalytics.MemberData()
{
name = "floatMember",
type = "System.Single",
value = "5"
},
new PerceptionEngineAnalytics.MemberData()
{
name = "vector2Member",
type = "UnityEngine.Vector2",
value = "(4.0, 7.0)"
},
new PerceptionEngineAnalytics.MemberData()
{
name = "unsupportedMember",
type = "UnityEngine.Perception.PerceptionEngineAnalytics+MemberData",
value = "UnityEngine.Perception.PerceptionEngineAnalytics+MemberData"
}
},
parameters = new[]
{
new PerceptionEngineAnalytics.ParameterData()
{
name = "booleanParam",
type = "BooleanParameter",
fields = new List<PerceptionEngineAnalytics.ParameterField>()
{
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Constant",
name = "value",
value = 1
},
}
},
new PerceptionEngineAnalytics.ParameterData()
{
name = "floatParam",
type = "FloatParameter",
fields = new List<PerceptionEngineAnalytics.ParameterField>()
{
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "AnimationCurve",
name = "value",
}
}
},
new PerceptionEngineAnalytics.ParameterData()
{
name = "integerParam",
type = "IntegerParameter",
fields = new List<PerceptionEngineAnalytics.ParameterField>()
{
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Uniform",
name = "value",
rangeMinimum = -3, rangeMaximum = 7
}
}
},
new PerceptionEngineAnalytics.ParameterData()
{
name = "vector2Param",
type = "Vector2Parameter",
fields = new List<PerceptionEngineAnalytics.ParameterField>()
{
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Constant",
name = "x",
value = 2
},
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Uniform",
name = "y",
rangeMinimum = -4, rangeMaximum = 8
}
}
},
new PerceptionEngineAnalytics.ParameterData()
{
name = "vector3Param",
type = "Vector3Parameter",
fields = new List<PerceptionEngineAnalytics.ParameterField>()
{
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Normal",
name = "x",
rangeMinimum = -5, rangeMaximum = 9,
mean = 4, stdDev = 2
},
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Constant",
name = "y",
value = 3
},
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "AnimationCurve",
name = "z",
}
}
},
new PerceptionEngineAnalytics.ParameterData()
{
name = "vector4Param",
type = "Vector4Parameter",
fields = new List<PerceptionEngineAnalytics.ParameterField>()
{
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Normal",
name = "x",
rangeMinimum = -5, rangeMaximum = 9,
mean = 4, stdDev = 2
},
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Constant",
name = "y",
value = 3
},
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "AnimationCurve",
name = "z",
},
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Uniform",
name = "w",
rangeMinimum = -12, rangeMaximum = 42
}
}
},
new PerceptionEngineAnalytics.ParameterData()
{
name = "colorRgbCategoricalParam",
type = "ColorRgbCategoricalParameter",
fields = new List<PerceptionEngineAnalytics.ParameterField>()
{
new PerceptionEngineAnalytics.ParameterField()
{
distribution = "Categorical",
name = "values",
categoricalParameterCount = 3
}
}
}
}
};
var expectedSerializedValueJson = JsonConvert.SerializeObject(expectedSerializedValue);
var serializedValueJson = JsonConvert.SerializeObject(randomizerData);
Assert.AreEqual(expectedSerializedValueJson, serializedValueJson);
}
PerceptionCamera SetupPerceptionCamera()

50
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;
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 PerceptionEngineAnalytics.MemberData unsupportedMember = new PerceptionEngineAnalytics.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();
};
}

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:

8
TestProjects/PerceptionHDRP/Assets/Scenes/SampleScene.meta


fileFormatVersion: 2
guid: 26f408242584f494cae23179f77b81ad
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
TestProjects/PerceptionHDRP/Assets/StreamingAssets.meta


fileFormatVersion: 2
guid: 253c8b404a216fa4898bcc0fcc5c58b1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
com.unity.perception/Editor/Pyrception.meta


fileFormatVersion: 2
guid: 586f94aeebe844666bdf7cfa6bdc9cb1
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存