浏览代码

Adding custom label ids and label id auto-assignment to LabelingConfiguration. Also renaming LabelingConfigurationEntry to LabelEntry

/main
Jon Hogins 5 年前
当前提交
44f1a9f3
共有 5 个文件被更改,包括 141 次插入35 次删除
  1. 110
      com.unity.perception/Editor/GroundTruth/LabelingConfigurationEditor.cs
  2. 40
      com.unity.perception/Runtime/GroundTruth/Labeling/LabelingConfiguration.cs
  3. 14
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  4. 6
      com.unity.perception/Runtime/GroundTruth/RenderedObjectInfoGenerator.cs
  5. 6
      com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs

110
com.unity.perception/Editor/GroundTruth/LabelingConfigurationEditor.cs


using System;
using Unity.Mathematics;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Perception.GroundTruth;

class LabelingConfigurationEditor : Editor
{
ReorderableList m_LabelsList;
const float k_Margin = 5f;
m_LabelsList = new ReorderableList(this.serializedObject, this.serializedObject.FindProperty(nameof(LabelingConfiguration.LabelingConfigurations)), true, false, true, true);
m_LabelsList.elementHeight = EditorGUIUtility.singleLineHeight * 2;
m_LabelsList = new ReorderableList(this.serializedObject, this.serializedObject.FindProperty(nameof(LabelingConfiguration.LabelEntries)), true, false, true, true);
m_LabelsList.elementHeight = EditorGUIUtility.singleLineHeight * 3 + k_Margin;
m_LabelsList.onReorderCallbackWithDetails += OnReorder;
}
void OnReorder(ReorderableList list, int oldIndex, int newIndex)
{
if (!autoAssign)
return;
var newFirstElement = list.serializedProperty.GetArrayElementAtIndex(0);
if (newIndex == 0 && list.serializedProperty.arraySize > 1)
{
var oldFirstId = list.serializedProperty.GetArrayElementAtIndex(1).FindPropertyRelative(nameof(LabelEntry.id)).intValue;
newFirstElement.FindPropertyRelative(nameof(LabelEntry.id)).intValue = oldFirstId;
}
if (oldIndex == 0)
{
var oldFirstId = list.serializedProperty.GetArrayElementAtIndex(newIndex).FindPropertyRelative(nameof(LabelEntry.id)).intValue;
newFirstElement.FindPropertyRelative(nameof(LabelEntry.id)).intValue = oldFirstId;
}
AutoAssignIds();
}
void OnRemove(ReorderableList list)

if (autoAssign)
AutoAssignIds();
this.serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target);
}

int maxLabel = Int32.MinValue;
if (list.serializedProperty.arraySize == 0)
maxLabel = -1;
for (int i = 0; i < list.serializedProperty.arraySize; i++)
{
var item = list.serializedProperty.GetArrayElementAtIndex(i);
maxLabel = math.max(maxLabel, item.FindPropertyRelative(nameof(LabelEntry.id)).intValue);
}
var labelProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.label));
var idProperty = element.FindPropertyRelative(nameof(LabelEntry.id));
idProperty.intValue = maxLabel + 1;
var labelProperty = element.FindPropertyRelative(nameof(LabelEntry.label));
var valueProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.value));
var valueProperty = element.FindPropertyRelative(nameof(LabelEntry.value));
if (autoAssign)
AutoAssignIds();
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target);
}

var element = m_LabelsList.serializedProperty.GetArrayElementAtIndex(index);
var labelProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.label));
var valueProperty = element.FindPropertyRelative(nameof(LabelingConfigurationEntry.value));
var idProperty = element.FindPropertyRelative(nameof(LabelEntry.id));
var labelProperty = element.FindPropertyRelative(nameof(LabelEntry.label));
var valueProperty = element.FindPropertyRelative(nameof(LabelEntry.value));
var newLabel = EditorGUI.TextField(contentRect, nameof(LabelingConfigurationEntry.label), labelProperty.stringValue);
using (new EditorGUI.DisabledScope(autoAssign && index != 0))
{
var newLabel = EditorGUI.IntField(contentRect, nameof(LabelEntry.id), idProperty.intValue);
if (change.changed)
{
idProperty.intValue = newLabel;
if (autoAssign)
AutoAssignIds();
}
}
}
using (var change = new EditorGUI.ChangeCheckScope())
{
var contentRect = new Rect(rect.position + new Vector2(0, EditorGUIUtility.singleLineHeight), new Vector2(rect.width, EditorGUIUtility.singleLineHeight));
var newLabel = EditorGUI.TextField(contentRect, nameof(LabelEntry.label), labelProperty.stringValue);
{
}
var contentRect = new Rect(rect.position + new Vector2(0, EditorGUIUtility.singleLineHeight), new Vector2(rect.width, EditorGUIUtility.singleLineHeight));
var newValue = EditorGUI.IntField(contentRect, nameof(LabelingConfigurationEntry.value), valueProperty.intValue);
var contentRect = new Rect(rect.position + new Vector2(0, EditorGUIUtility.singleLineHeight * 2), new Vector2(rect.width, EditorGUIUtility.singleLineHeight));
var newValue = EditorGUI.IntField(contentRect, nameof(LabelEntry.value), valueProperty.intValue);
bool autoAssign => serializedObject.FindProperty(nameof(LabelingConfiguration.AutoAssignIds)).boolValue;
serializedObject.Update();
using (var change = new EditorGUI.ChangeCheckScope())
{
var autoAssignIdsProperty = serializedObject.FindProperty(nameof(LabelingConfiguration.AutoAssignIds));
EditorGUILayout.PropertyField(autoAssignIdsProperty);
if (change.changed && autoAssignIdsProperty.boolValue)
{
var ok = EditorUtility.DisplayDialog("Enable auto-assigned labels", "Existing label ids will be overwritten. Enable label auto-assignment?", "Yes", "Cancel");
if (ok)
{
AutoAssignIds();
}
else
autoAssignIdsProperty.boolValue = false;
}
}
}
void AutoAssignIds()
{
var serializedProperty = serializedObject.FindProperty(nameof(LabelingConfiguration.LabelEntries));
var size = serializedProperty.arraySize;
if (size == 0)
return;
var nextId = serializedProperty.GetArrayElementAtIndex(0).FindPropertyRelative(nameof(LabelEntry.id)).intValue + 1;
for (int i = 1; i < size; i++)
{
serializedProperty.GetArrayElementAtIndex(i).FindPropertyRelative(nameof(LabelEntry.id)).intValue = nextId;
nextId++;
}
}
}
}

40
com.unity.perception/Runtime/GroundTruth/Labeling/LabelingConfiguration.cs


using System;
using System.Collections.Generic;
using UnityEngine.Serialization;
namespace UnityEngine.Perception.GroundTruth
{

public class LabelingConfiguration : ScriptableObject
{
/// <summary>
/// A sequence of <see cref="LabelingConfigurationEntry"/> which defines the labels relevant for this configuration and their values.
/// Whether the inspector will auto-assign ids based on the id of the first element
/// </summary>
public bool AutoAssignIds = true;
/// <summary>
/// A sequence of <see cref="LabelEntry"/> which defines the labels relevant for this configuration and their values.
[FormerlySerializedAs("LabelingConfigurations")]
public List<LabelingConfigurationEntry> LabelingConfigurations = new List<LabelingConfigurationEntry>();
public List<LabelEntry> LabelEntries = new List<LabelEntry>();
/// Attempts to find the matching index in <see cref="LabelingConfigurations"/> for the given <see cref="Labeling"/>.
/// Attempts to find the matching index in <see cref="LabelEntries"/> for the given <see cref="Labeling"/>.
/// The matching index is the first class name in the given Labeling which matches an entry in <see cref="LabelingConfigurations"/>.
/// The matching index is the first class name in the given Labeling which matches an entry in <see cref="LabelEntries"/>.
/// <param name="index">When this method returns, contains the index of the matching <see cref="LabelingConfigurationEntry"/>, or -1 if no match was found.</param>
/// <param name="labelEntry">When this method returns, contains the matching <see cref="LabelEntry"/>, or <code>default</code> if no match was found.</param>
public bool TryGetMatchingConfigurationIndex(Labeling labeling, out int index)
public bool TryGetMatchingConfigurationEntry(Labeling labeling, out LabelEntry labelEntry)
for (var i = 0; i < LabelingConfigurations.Count; i++)
for (var i = 0; i < LabelEntries.Count; i++)
var configuration = LabelingConfigurations[i];
if (string.Equals(configuration.label, labelingClass))
var entry = LabelEntries[i];
if (string.Equals(entry.label, labelingClass))
index = i;
labelEntry = entry;
index = -1;
labelEntry = default;
return false;
}
}

/// </summary>
[Serializable]
public struct LabelingConfigurationEntry
public struct LabelEntry
/// <summary>
/// The id associated with the label. Used to associate objects with labels in various forms of ground truth.
/// </summary>
public int id;
/// <summary>
/// The label string
/// </summary>

/// </summary>
public int value;
/// <param name="id">The id associated with the label. Used to associate objects with labels in various forms of ground truth.</param>
public LabelingConfigurationEntry(string label, int value)
public LabelEntry(int id, string label, int value)
this.id = id;
this.label = label;
this.value = value;
}

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


internal event Action<int, NativeArray<uint>> segmentationImageReceived;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelingConfigurationEntry>, int> classCountsReceived;
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> classCountsReceived;
[NonSerialized]
internal RenderTexture labelingTexture;

if (produceSegmentationImages)
{
var specs = LabelingConfiguration.LabelingConfigurations.Select((l, index) => new SemanticSegmentationSpec()
var specs = LabelingConfiguration.LabelEntries.Select((l, index) => new SemanticSegmentationSpec()
{
label_id = index,
label_name = l.label,

if (produceObjectCountAnnotations || produceBoundingBoxAnnotations || produceRenderedObjectInfoMetric)
{
var labelingMetricSpec = LabelingConfiguration.LabelingConfigurations.Select((l, index) => new ObjectCountSpec()
var labelingMetricSpec = LabelingConfiguration.LabelEntries.Select((l, index) => new ObjectCountSpec()
{
label_id = index,
label_name = l.label,

renderedObjectInfosCalculated?.Invoke(frameCount, renderedObjectInfos);
if (produceObjectCountAnnotations)
OnObjectCountsReceived(classCounts, LabelingConfiguration.LabelingConfigurations, frameCount);
OnObjectCountsReceived(classCounts, LabelingConfiguration.LabelEntries, frameCount);
ProduceBoundingBoxesAnnotation(renderedObjectInfos, LabelingConfiguration.LabelingConfigurations, frameCount);
ProduceBoundingBoxesAnnotation(renderedObjectInfos, LabelingConfiguration.LabelEntries, frameCount);
if (produceRenderedObjectInfoMetric)
ProduceRenderedObjectInfoMetric(renderedObjectInfos, frameCount);

#endif
void ProduceBoundingBoxesAnnotation(NativeArray<RenderedObjectInfo> renderedObjectInfos, List<LabelingConfigurationEntry> labelingConfigurations, int frameCount)
void ProduceBoundingBoxesAnnotation(NativeArray<RenderedObjectInfo> renderedObjectInfos, List<LabelEntry> labelingConfigurations, int frameCount)
{
using (s_BoundingBoxCallback.Auto())
{

return m_RenderedObjectInfoGenerator.TryGetLabelIdFromInstanceId(instanceId, out labelId);
}
void OnObjectCountsReceived(NativeSlice<uint> counts, IReadOnlyList<LabelingConfigurationEntry> entries, int frameCount)
void OnObjectCountsReceived(NativeSlice<uint> counts, IReadOnlyList<LabelEntry> entries, int frameCount)
{
using (s_ClassCountCallback.Auto())
{

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


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

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

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

6
com.unity.perception/Runtime/GroundTruth/SemanticSegmentationCrossPipelinePass.cs


using System;
using System;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;

public override void SetupMaterialProperties(MaterialPropertyBlock mpb, MeshRenderer meshRenderer, Labeling labeling, uint instanceId)
{
var entry = new LabelingConfigurationEntry();
foreach (var l in m_LabelingConfiguration.LabelingConfigurations)
var entry = new LabelEntry();
foreach (var l in m_LabelingConfiguration.LabelEntries)
{
if (labeling.labels.Contains(l.label))
{

正在加载...
取消
保存