浏览代码

Updating runtime and test code to use new label id scheme.

/main
Jon Hogins 4 年前
当前提交
085fae0b
共有 7 个文件被更改,包括 71 次插入56 次删除
  1. 13
      TestProjects/PerceptionHDRP/Assets/LabelingConfiguration.asset
  2. 13
      TestProjects/PerceptionURP/Assets/ExampleLabelingConfiguration.asset
  3. 36
      com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs
  4. 31
      com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs
  5. 20
      com.unity.perception/Tests/Runtime/GroundTruthTests/BoundingBox2DTests.cs
  6. 9
      com.unity.perception/Tests/Runtime/GroundTruthTests/ObjectCountTests.cs
  7. 5
      com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs

13
TestProjects/PerceptionHDRP/Assets/LabelingConfiguration.asset


m_Script: {fileID: 11500000, guid: bad10bec3eccd8e49a9d725b2c30f74c, type: 3}
m_Name: LabelingConfiguration
m_EditorClassIdentifier:
LabelingConfigurations:
- label: Box
AutoAssignIds: 1
StartingLabelId: 1
LabelEntries:
- id: 1
label: Box
- label: Crate
- id: 2
label: Crate
- label: Cube
- id: 3
label: Cube
value: 30000

13
TestProjects/PerceptionURP/Assets/ExampleLabelingConfiguration.asset


m_Script: {fileID: 11500000, guid: bad10bec3eccd8e49a9d725b2c30f74c, type: 3}
m_Name: ExampleLabelingConfiguration
m_EditorClassIdentifier:
LabelingConfigurations:
- label: Box
AutoAssignIds: 1
StartingLabelId: 1
LabelEntries:
- id: 1
label: Box
- label: Cube
- id: 2
label: Cube
- label: Crate
- id: 3
label: Crate
value: 30000

36
com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs


ComputeBuffer m_InstanceIdPresenceMask;
ComputeBuffer m_InstanceIdToClassId;
ComputeBuffer m_ClassCounts;
NativeList<int> m_InstanceIdToClassIdLookup;
NativeList<int> m_InstanceIdToLabelIndexLookup;
HashSet<Camera> m_CamerasRendered = new HashSet<Camera>();
bool m_IdBuffersNeedUpdating;
bool m_DidComputeLastFrame;

public override void SetupMaterialProperties(MaterialPropertyBlock mpb, MeshRenderer meshRenderer, Labeling labeling, uint instanceId)
{
if (!m_InstanceIdToClassIdLookup.IsCreated)
if (!m_InstanceIdToLabelIndexLookup.IsCreated)
m_InstanceIdToClassIdLookup = new NativeList<int>(k_StartingObjectCount, Allocator.Persistent);
m_InstanceIdToLabelIndexLookup = new NativeList<int>(k_StartingObjectCount, Allocator.Persistent);
if (LabelingConfiguration.TryGetMatchingConfigurationIndex(labeling, out var index))
if (LabelingConfiguration.TryGetMatchingConfigurationEntry(labeling, out LabelEntry labelEntry, out var index))
if (m_InstanceIdToClassIdLookup.Length <= instanceId)
if (m_InstanceIdToLabelIndexLookup.Length <= instanceId)
m_InstanceIdToClassIdLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
m_InstanceIdToLabelIndexLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
m_InstanceIdToClassIdLookup[(int)instanceId] = index + 1;
m_InstanceIdToLabelIndexLookup[(int)instanceId] = index + 1;
}
}

var objectCount = k_StartingObjectCount;
UpdateIdBufferSizes(objectCount);
m_ClassCounts = new ComputeBuffer(LabelingConfiguration.LabelingConfigurations.Count + 1, UnsafeUtility.SizeOf<uint>(), ComputeBufferType.Structured);
m_ClassCounts = new ComputeBuffer(LabelingConfiguration.LabelEntries.Count + 1, UnsafeUtility.SizeOf<uint>(), ComputeBufferType.Structured);
RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
}

protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
//If there are no objects to label, skip the pass
if (!m_InstanceIdToClassIdLookup.IsCreated || m_InstanceIdToClassIdLookup.Length == 0)
if (!m_InstanceIdToLabelIndexLookup.IsCreated || m_InstanceIdToLabelIndexLookup.Length == 0)
var counts = new NativeArray<uint>(LabelingConfiguration.LabelingConfigurations.Count + 1, Allocator.Temp);
var counts = new NativeArray<uint>(LabelingConfiguration.LabelEntries.Count + 1, Allocator.Temp);
OnClassCountReadback(Time.frameCount, counts);
counts.Dispose();
return;

if (m_IdBuffersNeedUpdating)
{
UpdateIdBufferSizes(m_InstanceIdToClassIdLookup.Capacity);
m_InstanceIdToClassId.SetData(m_InstanceIdToClassIdLookup.AsArray());
UpdateIdBufferSizes(m_InstanceIdToLabelIndexLookup.Capacity);
m_InstanceIdToClassId.SetData(m_InstanceIdToLabelIndexLookup.AsArray());
}
//The following section kicks off the four kernels in LabeledObjectHistogram.compute

cmd.SetComputeBufferParam(m_ComputeShader, 3, "InstanceIdPresenceMask", m_InstanceIdPresenceMask);
cmd.SetComputeBufferParam(m_ComputeShader, 3, "InstanceIdToClassId", m_InstanceIdToClassId);
cmd.SetComputeBufferParam(m_ComputeShader, 3, "ClassCounts", m_ClassCounts);
cmd.DispatchCompute(m_ComputeShader, 3, m_InstanceIdToClassIdLookup.Length, 1, 1);
cmd.DispatchCompute(m_ComputeShader, 3, m_InstanceIdToLabelIndexLookup.Length, 1, 1);
var requestFrameCount = Time.frameCount;
cmd.RequestAsyncReadback(m_ClassCounts, request => OnClassCountReadback(requestFrameCount, request.GetData<uint>()));

m_ClassCounts = null;
WaitForAllRequests();
if (m_InstanceIdToClassIdLookup.IsCreated)
if (m_InstanceIdToLabelIndexLookup.IsCreated)
m_InstanceIdToClassIdLookup.Dispose();
m_InstanceIdToClassIdLookup = default;
m_InstanceIdToLabelIndexLookup.Dispose();
m_InstanceIdToLabelIndexLookup = default;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelingConfigurationEntry>, int> ClassCountsReceived;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> ClassCountsReceived;
void OnClassCountReadback(int requestFrameCount, NativeArray<uint> counts)
{

Debug.Log(sb);
#endif
ClassCountsReceived?.Invoke(new NativeSlice<uint>(counts, 1), LabelingConfiguration.LabelingConfigurations, requestFrameCount);
ClassCountsReceived?.Invoke(new NativeSlice<uint>(counts, 1), LabelingConfiguration.LabelEntries, requestFrameCount);
}
public void WaitForAllRequests()

31
com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs


/// <inheritdoc/>
public void SetupMaterialProperties(MaterialPropertyBlock mpb, MeshRenderer meshRenderer, Labeling labeling, uint instanceId)
{
if (m_LabelingConfiguration.TryGetMatchingConfigurationEntry(labeling, out var entry))
if (m_LabelingConfiguration.TryGetMatchingConfigurationEntry(labeling, out var entry, out var index))
{
if (m_InstanceIdToLabelEntryIndexLookup.Length <= instanceId)
{

m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = entry.id;
m_InstanceIdToLabelEntryIndexLookup[(int)instanceId] = index;
}
}

/// InstanceSegmentationRawData should be the raw data from a texture filled by <see cref="InstanceSegmentationUrpPass"/> or <see cref="InstanceSegmentationPass"/>
/// using the same LabelingConfiguration that was passed into this object.
/// </summary>
/// <param name="instanceSegmentationRawData"></param>
/// <param name="stride"></param>
/// <param name="boundingBoxOrigin"></param>
/// <param name="boundingBoxes"></param>
/// <param name="classCounts"></param>
/// <param name="allocator"></param>
public void Compute(NativeArray<uint> instanceSegmentationRawData, int stride, BoundingBoxOrigin boundingBoxOrigin, out NativeArray<RenderedObjectInfo> boundingBoxes, out NativeArray<uint> classCounts, Allocator allocator)
/// <param name="instanceSegmentationRawData">The raw instance segmentation image.</param>
/// <param name="stride">Stride of the image data. Should be equal to the width of the image.</param>
/// <param name="boundingBoxOrigin">Whether bounding boxes should be top-left or bottom-right-based.</param>
/// <param name="renderedObjectInfos">When this method returns, filled with RenderedObjectInfo entries for each object visible in the frame.</param>
/// <param name="perLabelEntryObjectCount">When the method returns, filled with a NativeArray with the count of objects for each entry in <see cref="LabelingConfiguration.LabelEntries"/> in the LabelingConfiguration passed into the constructor.</param>
/// <param name="allocator">The allocator to use for allocating renderedObjectInfos and perLabelEntryObjectCount.</param>
public void Compute(NativeArray<uint> instanceSegmentationRawData, int stride, BoundingBoxOrigin boundingBoxOrigin, out NativeArray<RenderedObjectInfo> renderedObjectInfos, out NativeArray<uint> perLabelEntryObjectCount, Allocator allocator)
{
const int jobCount = 24;
var height = instanceSegmentationRawData.Length / stride;

JobHandle.CompleteAll(handles);
}
classCounts = new NativeArray<uint>(m_LabelingConfiguration.LabelEntries.Count, allocator);
perLabelEntryObjectCount = new NativeArray<uint>(m_LabelingConfiguration.LabelEntries.Count, allocator);
var boundingBoxMap = new NativeHashMap<int, RenderedObjectInfo>(100, Allocator.Temp);
using (s_LabelMerge.Auto())
{

}
var keyValueArrays = boundingBoxMap.GetKeyValueArrays(Allocator.Temp);
boundingBoxes = new NativeArray<RenderedObjectInfo>(keyValueArrays.Keys.Length, allocator);
renderedObjectInfos = new NativeArray<RenderedObjectInfo>(keyValueArrays.Keys.Length, allocator);
for (var i = 0; i < keyValueArrays.Keys.Length; i++)
{
var instanceId = keyValueArrays.Keys[i];

var classId = m_InstanceIdToLabelEntryIndexLookup[instanceId];
classCounts[classId]++;
var labelIndex = m_InstanceIdToLabelEntryIndexLookup[instanceId];
var labelId = m_LabelingConfiguration.LabelEntries[labelIndex].id;
perLabelEntryObjectCount[labelIndex]++;
var renderedObjectInfo = keyValueArrays.Values[i];
var boundingBox = renderedObjectInfo.boundingBox;
if (boundingBoxOrigin == BoundingBoxOrigin.TopLeft)

}
boundingBoxes[i] = new RenderedObjectInfo
renderedObjectInfos[i] = new RenderedObjectInfo
labelId = classId,
labelId = labelId,
boundingBox = boundingBox,
pixelCount = renderedObjectInfo.pixelCount
};

20
com.unity.perception/Tests/Runtime/GroundTruthTests/BoundingBox2DTests.cs


{
boundingBox = new Rect(0, 0, 2, 2),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 4
}
}, new uint[]

{
boundingBox = new Rect(0, 0, 1, 2),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 2
},
new RenderedObjectInfo()

labelId = 1,
labelId = 2,
pixelCount = 1
}
}, new uint[]

{
boundingBox = new Rect(0, 0, 3, 2),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 4
},
new RenderedObjectInfo()

labelId = 1,
labelId = 2,
pixelCount = 2
}
}, new uint[]

{
boundingBox = new Rect(1, 0, 1, 1),
instanceId = 1,
labelId = 0,
labelId = 1,
pixelCount = 1
},
}, new uint[]

var label2 = "label2";
var labelingConfiguration = ScriptableObject.CreateInstance<LabelingConfiguration>();
labelingConfiguration.LabelingConfigurations = new List<LabelingConfigurationEntry>
labelingConfiguration.LabelEntries = new List<LabelEntry>
new LabelingConfigurationEntry
new LabelEntry
id = 1,
new LabelingConfigurationEntry
new LabelEntry
id = 2,
label = label2,
value = 500
}

9
com.unity.perception/Tests/Runtime/GroundTruthTests/ObjectCountTests.cs


var label = "label";
var labelingConfiguration = ScriptableObject.CreateInstance<LabelingConfiguration>();
labelingConfiguration.LabelingConfigurations = new List<LabelingConfigurationEntry>
labelingConfiguration.LabelEntries = new List<LabelEntry>
new LabelingConfigurationEntry
new LabelEntry
id = 1,
var receivedResults = new List<(uint[] counts, LabelingConfigurationEntry[] labels, int frameCount)>();
var receivedResults = new List<(uint[] counts, LabelEntry[] labels, int frameCount)>();
var cameraObject = SetupCamera(labelingConfiguration, (counts, labels, frameCount) =>
{

}
static GameObject SetupCamera(LabelingConfiguration labelingConfiguration,
Action<NativeSlice<uint>, IReadOnlyList<LabelingConfigurationEntry>, int> onClassCountsReceived)
Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> onClassCountsReceived)
{
var cameraObject = new GameObject();
cameraObject.SetActive(false);

5
com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs


var label = "label";
var labelingConfiguration = ScriptableObject.CreateInstance<LabelingConfiguration>();
labelingConfiguration.LabelingConfigurations = new List<LabelingConfigurationEntry>
labelingConfiguration.LabelEntries = new List<LabelEntry>
new LabelingConfigurationEntry
new LabelEntry
id = 1,
label = label,
value = 500
}

正在加载...
取消
保存