浏览代码

implemented for all ground truth generators

/main
Mohsen Kamalzadeh 4 年前
当前提交
bd7064be
共有 9 个文件被更改,包括 172 次插入76 次删除
  1. 4
      com.unity.perception/Runtime/GroundTruth/InstanceIdToColorMapping.cs
  2. 97
      com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs
  3. 21
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs
  4. 9
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs
  5. 14
      com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs
  6. 5
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs
  7. 16
      com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs
  8. 38
      com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs
  9. 44
      com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs

4
com.unity.perception/Runtime/GroundTruth/InstanceIdToColorMapping.cs


const uint k_HslCount = 64;
const uint k_ColorsPerAlpha = 256 * 256 * 256;
const uint k_InvalidPackedColor = 255; // packed uint for color (0, 0, 0, 255);
static readonly Color32 k_InvalidColor = new Color(0, 0, 0, 255);
public static readonly Color32 invalidColor = new Color(0, 0, 0, 255);
static readonly float k_GoldenRatio = (1 + Mathf.Sqrt(5)) / 2;
const int k_HuesInEachValue = 30;

/// <returns>Returns true if the ID was mapped to a non-black color, otherwise returns false</returns>
public static bool TryGetColorFromInstanceId(uint id, out Color32 color)
{
color = k_InvalidColor;
color = invalidColor;
if (id > maxId) return false;
var packed = GetColorForId(id);

97
com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs


void ProcessLabel(Labeling labeledEntity)
{
// Cache out the data of a labeled game object the first time we see it, this will
// save performance each frame. Also checks to see if a labeled game object can be annotated.
if (!m_KnownStatus.ContainsKey(labeledEntity.instanceId))
if (idLabelConfig.TryGetLabelEntryFromInstanceId(labeledEntity.instanceId, out var labelEntry))
var cached = new CachedData()
// Cache out the data of a labeled game object the first time we see it, this will
// save performance each frame. Also checks to see if a labeled game object can be annotated.
if (!m_KnownStatus.ContainsKey(labeledEntity.instanceId))
status = false,
animator = null,
keypoints = new KeypointEntry(),
overrides = new List<(JointLabel, int)>()
};
var cached = new CachedData()
{
status = false,
animator = null,
keypoints = new KeypointEntry(),
overrides = new List<(JointLabel, int)>()
};
if (idLabelConfig.TryGetLabelEntryFromInstanceId(labeledEntity.instanceId, out var labelEntry))
{
var entityGameObject = labeledEntity.gameObject;
cached.keypoints.instance_id = labeledEntity.instanceId;

cached.status = true;
}
}
m_KnownStatus[labeledEntity.instanceId] = cached;
m_KnownStatus[labeledEntity.instanceId] = cached;
}
var cachedData = m_KnownStatus[labeledEntity.instanceId];
if (cachedData.status)
{
var animator = cachedData.animator;
var keypoints = cachedData.keypoints.keypoints;
var cachedData = m_KnownStatus[labeledEntity.instanceId];
// Go through all of the rig keypoints and get their location
for (var i = 0; i < activeTemplate.keypoints.Length; i++)
if (cachedData.status)
var pt = activeTemplate.keypoints[i];
if (pt.associateToRig)
var animator = cachedData.animator;
var keypoints = cachedData.keypoints.keypoints;
// Go through all of the rig keypoints and get their location
for (var i = 0; i < activeTemplate.keypoints.Length; i++)
var bone = animator.GetBoneTransform(pt.rigLabel);
if (bone != null)
var pt = activeTemplate.keypoints[i];
if (pt.associateToRig)
var loc = ConvertToScreenSpace(bone.position);
keypoints[i].index = i;
keypoints[i].x = loc.x;
keypoints[i].y = loc.y;
keypoints[i].state = 2;
var bone = animator.GetBoneTransform(pt.rigLabel);
if (bone != null)
{
var loc = ConvertToScreenSpace(bone.position);
keypoints[i].index = i;
keypoints[i].x = loc.x;
keypoints[i].y = loc.y;
keypoints[i].state = 2;
}
}
// Go through all of the additional or override points defined by joint labels and get
// their locations
foreach (var (joint, idx) in cachedData.overrides)
{
var loc = ConvertToScreenSpace(joint.transform.position);
keypoints[idx].index = idx;
keypoints[idx].x = loc.x;
keypoints[idx].y = loc.y;
keypoints[idx].state = 2;
}
// Go through all of the additional or override points defined by joint labels and get
// their locations
foreach (var (joint, idx) in cachedData.overrides)
{
var loc = ConvertToScreenSpace(joint.transform.position);
keypoints[idx].index = idx;
keypoints[idx].x = loc.x;
keypoints[idx].y = loc.y;
keypoints[idx].state = 2;
}
cachedData.keypoints.pose = "unset";
cachedData.keypoints.pose = "unset";
if (cachedData.animator != null)
{
cachedData.keypoints.pose = GetPose(cachedData.animator);
}
if (cachedData.animator != null)
{
cachedData.keypoints.pose = GetPose(cachedData.animator);
}
m_AsyncAnnotations[m_CurrentFrame].keypoints[labeledEntity.instanceId] = cachedData.keypoints;
m_AsyncAnnotations[m_CurrentFrame].keypoints[labeledEntity.instanceId] = cachedData.keypoints;
}
}
}

21
com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs


if (m_IdLabelConfig.TryGetMatchingConfigurationEntry(labeling, out _, out var index))
{
Debug.Assert(index < k_DefaultValue, "Too many entries in the label config");
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
if (labeling.enabled)
var oldLength = m_InstanceIdToLabelEntryIndexLookup.Length;
m_InstanceIdToLabelEntryIndexLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
{
var oldLength = m_InstanceIdToLabelEntryIndexLookup.Length;
m_InstanceIdToLabelEntryIndexLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
for (var i = oldLength; i < instanceId; i++)
m_InstanceIdToLabelEntryIndexLookup[i] = k_DefaultValue;
for (var i = oldLength; i < instanceId; i++)
m_InstanceIdToLabelEntryIndexLookup[i] = k_DefaultValue;
}
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = (ushort)index;
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = (ushort)index;
else if (m_InstanceIdToLabelEntryIndexLookup.Length > instanceId)
{
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = k_DefaultValue;
}
else if (m_InstanceIdToLabelEntryIndexLookup.Length > (int)instanceId)
else if (m_InstanceIdToLabelEntryIndexLookup.Length > instanceId)
{
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = k_DefaultValue;
}

9
com.unity.perception/Runtime/GroundTruth/Labeling/LabelManager.cs


}
/// <summary>
/// Registers a labeling component
/// </summary>
/// <param name="labeling">the component to register</param>
internal void Register(Labeling labeling)
{
m_LabelsPendingRegistration.Add(labeling);
}
/// <summary>
/// Unregisters a labeling component
/// </summary>
/// <param name="labeling">the component to unregister</param>

14
com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs


using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.Serialization;

/// </summary>
public uint instanceId { get; private set; }
void Awake()
void OnDestroy()
{
labelManager.Unregister(this);
}
void OnEnable()
labelManager.Register(this);
RefreshLabeling();
void OnDestroy()
void OnDisable()
labelManager.Unregister(this);
RefreshLabeling();
}
void Reset()

5
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/InstanceSegmentationCrossPipelinePass.cs


if (!found)
Debug.LogError($"Could not get a unique color for {instanceId}");
mpb.SetVector(k_SegmentationIdProperty, (Color)color);
if (labeling.enabled)
mpb.SetVector(k_SegmentationIdProperty, (Color)color);
else
mpb.SetVector(k_SegmentationIdProperty, (Color) InstanceIdToColorMapping.invalidColor);
#if PERCEPTION_DEBUG
Debug.Log($"Assigning id. Frame {Time.frameCount} id {id}");
#endif

16
com.unity.perception/Runtime/GroundTruth/RenderPasses/CrossPipelinePasses/SemanticSegmentationCrossPipelinePass.cs


{
var entry = new SemanticSegmentationLabelEntry();
var found = false;
foreach (var l in m_LabelConfig.labelEntries)
if (labeling.enabled)
if (labeling.labels.Contains(l.label))
foreach (var l in m_LabelConfig.labelEntries)
entry = l;
found = true;
break;
if (labeling.labels.Contains(l.label))
{
entry = l;
found = true;
break;
}
}
}

else
mpb.SetVector(k_LabelingId, Color.black);
}
}
}

38
com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs


}
}
[UnityTest]
public IEnumerator TryGet_ReturnsFalse_ForMatchingLabelWithDisabledLabelingComponent()
{
var label = "label";
var labeledPlane = TestHelper.CreateLabeledPlane(label: label);
AddTestObjectForCleanup(labeledPlane);
var config = ScriptableObject.CreateInstance<IdLabelConfig>();
var labeling = labeledPlane.GetComponent<Labeling>();
config.Init(new[]
{
new IdLabelEntry()
{
id = 1,
label = label
},
});
using (var cache = new LabelEntryMatchCache(config, Allocator.Persistent))
{
labeling.enabled = false;
//allow label to be registered
yield return null;
Assert.IsFalse(cache.TryGetLabelEntryFromInstanceId(labeledPlane.GetComponent<Labeling>().instanceId, out var labelEntry, out var index));
Assert.AreEqual(-1, index);
labeling.enabled = true;
yield return null;
Assert.IsTrue(cache.TryGetLabelEntryFromInstanceId(labeledPlane.GetComponent<Labeling>().instanceId, out labelEntry, out index));
Assert.AreEqual(0, index);
Assert.AreEqual(config.labelEntries[0], labelEntry);
labeling.enabled = false;
yield return null;
Assert.IsFalse(cache.TryGetLabelEntryFromInstanceId(labeledPlane.GetComponent<Labeling>().instanceId, out labelEntry, out index));
Assert.AreEqual(-1, index);
}
}
}
}

44
com.unity.perception/Tests/Runtime/GroundTruthTests/SegmentationGroundTruthTests.cs


}
[UnityTest]
public IEnumerator SemanticSegmentationPass_WithMatchingButDisabledLabel_ProducesBlack()
{
int timesSegmentationImageReceived = 0;
var expectedPixelValue = new Color32(0, 0, 0, 255);
void OnSegmentationImageReceived(NativeArray<Color32> data)
{
timesSegmentationImageReceived++;
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data.ToArray());
}
var cameraObject = SetupCameraSemanticSegmentation(a => OnSegmentationImageReceived(a.data), false);
var gameObject = TestHelper.CreateLabeledPlane();
gameObject.GetComponent<Labeling>().enabled = false;
AddTestObjectForCleanup(gameObject);
yield return null;
//destroy the object to force all pending segmented image readbacks to finish and events to be fired.
DestroyTestObject(cameraObject);
Assert.AreEqual(1, timesSegmentationImageReceived);
}
[UnityTest]
public IEnumerator InstanceSegmentationPass_WithMatchingButDisabledLabel_ProducesBlack()
{
int timesSegmentationImageReceived = 0;
var expectedPixelValue = new Color32(0, 0, 0, 255);
void OnSegmentationImageReceived(NativeArray<Color32> data)
{
CollectionAssert.AreEqual(Enumerable.Repeat(expectedPixelValue, data.Length), data);
timesSegmentationImageReceived++;
}
var cameraObject = SetupCameraInstanceSegmentation((frame, data, renderTexture) => OnSegmentationImageReceived(data));
var gameObject = TestHelper.CreateLabeledPlane();
gameObject.GetComponent<Labeling>().enabled = false;
AddTestObjectForCleanup(gameObject);
yield return null;
//destroy the object to force all pending segmented image readbacks to finish and events to be fired.
DestroyTestObject(cameraObject);
Assert.AreEqual(1, timesSegmentationImageReceived);
}
[UnityTest]
public IEnumerator SemanticSegmentationPass_WithEmptyFrame_ProducesBlack([Values(false, true)] bool showVisualizations)
{
int timesSegmentationImageReceived = 0;

正在加载...
取消
保存