浏览代码

Fixing LabelEntryMatchCache bug where instance ids that do not match any entries are reported as matching the first entry. (#60)

/main
GitHub 4 年前
当前提交
65b8bd28
共有 6 个文件被更改,包括 142 次插入2 次删除
  1. 1
      com.unity.perception/Runtime/GroundTruth/GroundTruthLabelSetupSystem.cs
  2. 23
      com.unity.perception/Runtime/GroundTruth/Labeling/IdLabelEntry.cs
  3. 9
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelEntryMatchCache.cs
  4. 7
      com.unity.perception/Runtime/GroundTruth/Labeling/Labeling.cs
  5. 93
      com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs
  6. 11
      com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs.meta

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


{
instanceId = instanceId
});
labeling.SetInstanceId(instanceId);
});
}

23
com.unity.perception/Runtime/GroundTruth/Labeling/IdLabelEntry.cs


/// An entry for <see cref="IdLabelConfig"/> mapping a label to an integer id.
/// </summary>
[Serializable]
public struct IdLabelEntry : ILabelEntry
public struct IdLabelEntry : ILabelEntry, IEquatable<IdLabelEntry>
{
string ILabelEntry.label => this.label;
/// <summary>

/// The id to associate with the label.
/// </summary>
public int id;
/// <inheritdoc/>
public bool Equals(IdLabelEntry other)
{
return label == other.label && id == other.id;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return obj is IdLabelEntry other && Equals(other);
}
/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
return ((label != null ? label.GetHashCode() : 0) * 397) ^ id;
}
}
}
}

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


const int k_StartingObjectCount = 1 << 8;
NativeList<ushort> m_InstanceIdToLabelEntryIndexLookup;
IdLabelConfig m_IdLabelConfig;
ushort m_DefaultValue;
public LabelEntryMatchCache(IdLabelConfig idLabelConfig)
{

{
labelEntry = default;
index = -1;
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId || m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] == m_DefaultValue)
return false;
index = m_InstanceIdToLabelEntryIndexLookup[(int)instanceId];

{
if (m_IdLabelConfig.TryGetMatchingConfigurationEntry(labeling, out _, out var index))
{
m_DefaultValue = ushort.MaxValue;
Debug.Assert(index < m_DefaultValue, "Too many entries in the label config");
var oldLength = m_InstanceIdToLabelEntryIndexLookup.Length;
for (int i = oldLength; i < instanceId; i++)
m_InstanceIdToLabelEntryIndexLookup[i] = m_DefaultValue;
}
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = (ushort)index;
}

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


[FormerlySerializedAs("classes")]
public List<string> labels = new List<string>();
public uint instanceId { get; private set; }
internal void SetInstanceId(uint instanceId)
{
this.instanceId = instanceId;
}
void Awake()
{
m_Entity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity();

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


using System.Collections;
using NUnit.Framework;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.TestTools;
namespace GroundTruthTests
{
[TestFixture]
public class LabelEntryMatchCacheTests : GroundTruthTestBase
{
[Test]
public void TryGet_ReturnsFalse_ForInvalidInstanceId()
{
var config = new IdLabelConfig();
using (var cache = new LabelEntryMatchCache(config))
{
Assert.IsFalse(cache.TryGetLabelEntryFromInstanceId(100, out var labelEntry, out var index));
Assert.AreEqual(-1, index);
Assert.AreEqual(default(IdLabelEntry), labelEntry);
}
}
[UnityTest]
public IEnumerator TryGet_ReturnsTrue_ForMatchingLabel()
{
var label = "label";
var labeledPlane = TestHelper.CreateLabeledPlane(label: label);
AddTestObjectForCleanup(labeledPlane);
var config = new IdLabelConfig();
config.Init(new[]
{
new IdLabelEntry()
{
id = 1,
label = label
},
});
using (var cache = new LabelEntryMatchCache(config))
{
//allow label to be registered
yield return null;
Assert.IsTrue(cache.TryGetLabelEntryFromInstanceId(labeledPlane.GetComponent<Labeling>().instanceId, out var labelEntry, out var index));
Assert.AreEqual(0, index);
Assert.AreEqual(config.labelEntries[0], labelEntry);
}
}
[UnityTest]
public IEnumerator TryGet_ReturnsFalse_ForNonMatchingLabel()
{
var label = "label";
var labeledPlane = TestHelper.CreateLabeledPlane(label: label);
AddTestObjectForCleanup(labeledPlane);
var config = new IdLabelConfig();
using (var cache = new LabelEntryMatchCache(config))
{
//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);
Assert.AreEqual(default(IdLabelEntry), labelEntry);
}
}
[UnityTest]
public IEnumerator TryGet_ReturnsFalse_ForNonMatchingLabel_WithOtherMatches()
{
var label = "label";
//only way to guarantee registration order is to run frames.
//We want to ensure labeledPlane is registered before labeledPlane2 so that the cache does not early out
var labeledPlane = TestHelper.CreateLabeledPlane(label: "foo");
AddTestObjectForCleanup(labeledPlane);
yield return null;
var labeledPlane2 = TestHelper.CreateLabeledPlane(label: label);
AddTestObjectForCleanup(labeledPlane2);
var config = new IdLabelConfig();
config.Init(new[]
{
new IdLabelEntry()
{
id = 1,
label = label
},
});
using (var cache = new LabelEntryMatchCache(config))
{
//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);
Assert.AreEqual(default(IdLabelEntry), labelEntry);
}
}
}
}

11
com.unity.perception/Tests/Runtime/GroundTruthTests/LabelEntryMatchCacheTests.cs.meta


fileFormatVersion: 2
guid: 8404035e89b3b1b4e87136d8a512b6f5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存