浏览代码

Old perception format working, and all tests passing

/solo_support
Steve Borkman 3 年前
当前提交
4cea1504
共有 7 个文件被更改,包括 210 次插入208 次删除
  1. 74
      com.unity.perception/Runtime/GroundTruth/Consumers/OldPerceptionConsumer.cs
  2. 303
      com.unity.perception/Runtime/GroundTruth/Consumers/OldPerceptionJsonFactory.cs
  3. 22
      com.unity.perception/Runtime/Randomization/Scenarios/PerceptionScenario.cs
  4. 6
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  5. 1
      com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs
  6. 2
      com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenarioConstants.cs
  7. 10
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests/ScenarioTests.cs

74
com.unity.perception/Runtime/GroundTruth/Consumers/OldPerceptionConsumer.cs


using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;

using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Perception.GroundTruth.DataModel;
using Formatting = Newtonsoft.Json.Formatting;
namespace UnityEngine.Perception.GroundTruth.Consumers
{

{
static readonly string version = "0.1.1";
public string baseDirectory = "D:/PerceptionOutput/KickinItOldSchool";
public int capturesPerFile = 150;
public int metricsPerFile = 150;
const string k_BaseDirectory = "D:/PerceptionOutput/KickinItOldSchool";
const int k_CapturesPerFile = 150;
const int k_MetricsPerFile = 150;
JsonSerializer m_JsonSerializer = new JsonSerializer();
string m_LogsPath;
// ReSharper disable NotAccessedField.Local
[Serializable]
struct SensorInfo
{

}
// ReSharper enable NotAccessedField.Local
Dictionary<string, SensorInfo> m_SensorMap = new Dictionary<string, SensorInfo>();
Dictionary<string, AnnotationDefinition> m_RegisteredAnnotations = new Dictionary<string, AnnotationDefinition>();

// Create a directory guid...
var path = Guid.NewGuid().ToString();
m_CurrentPath = Path.Combine(baseDirectory, path);
m_CurrentPath = Path.Combine(k_BaseDirectory, path);
m_LogsPath = VerifyDirectoryWithGuidExists("Logs", false);
}
public override void OnFrameGenerated(Frame frame)

foreach (var annotation in sensor.annotations)
{
string defId = null;
if (!m_RegisteredAnnotations.TryGetValue(annotation.Id, out var def))
{
defId = null;
}
defId = def.id;
m_RegisteredAnnotations.TryGetValue(annotation.Id, out var def);
var defId = def?.id ?? string.Empty;
var json = OldPerceptionJsonFactory.Convert(this, frame, annotation.Id, defId, annotation);
if (json != null) annotations.Add(json);
}

AddMetricToReport(metric);
}
var capture = new PerceptionCapture
// Scenarios report frames before captures, so skip it if a capture is not ready
if (rgbSensor != null)
id = $"frame_{frame.frame}",
filename = RemoveDatasetPathPrefix(path),
format = "PNG",
sequence_id = seqId,
step = frame.step,
timestamp = frame.timestamp,
sensor = PerceptionRgbSensor.Convert(this, rgbSensor, path),
annotations = annotations
};
var capture = new PerceptionCapture
{
id = $"frame_{frame.frame}",
filename = RemoveDatasetPathPrefix(path),
format = "PNG",
sequence_id = seqId,
step = frame.step,
timestamp = frame.timestamp,
sensor = PerceptionRgbSensor.Convert(rgbSensor),
annotations = annotations
};
m_CurrentCaptures.Add(capture);
m_CurrentCaptures.Add(capture);
}
WriteCaptures();
}

if (flush || m_MetricsReady.Count > metricsPerFile)
if (flush || m_MetricsReady.Count > k_MetricsPerFile)
{
WriteMetricsFile(m_MetricOutCount++, m_MetricsReady);
m_MetricsReady.Clear();

void WriteCaptures(bool flush = false)
{
if (flush || m_CurrentCaptures.Count >= capturesPerFile)
if (flush || m_CurrentCaptures.Count >= k_CapturesPerFile)
{
WriteCaptureFile(m_CurrentCaptureIndex++, m_CurrentCaptures);
m_CurrentCaptures.Clear();

WriteMetrics(true);
}
int m_CurrentCaptureIndex = 0;
int m_CurrentCaptureIndex;
string WriteOutImageFile(int frame, RgbSensor rgb)
{

WriteJTokenToFile(path, top);
}
JToken ToJtoken(Metric metric)
JToken ToJToken(Metric metric)
{
string sensorId = null;
string annotationId = null;

WriteJTokenToFile(path, top);
}
int m_MetricOutCount = 0;
int m_MetricOutCount;
m_MetricsReady.Add(ToJtoken(metric));
m_MetricsReady.Add(ToJToken(metric));
WriteMetrics();
}

WriteJTokenToFile(path, top);
}
// ReSharper disable NotAccessedField.Local
// ReSharper disable InconsistentNaming
// ReSharper disable once MemberHidesStaticFromOuterClass
public string version;
public IEnumerable<PerceptionCapture> captures;
}

{
// ReSharper disable once MemberHidesStaticFromOuterClass
[SuppressMessage("ReSharper", "InconsistentNaming")]
[Serializable]
struct PerceptionCapture
{

public JArray annotations;
}
public static float[][] ToFloatArray(float3x3 inF3)
static float[][] ToFloatArray(float3x3 inF3)
{
return new[]
{

};
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
[Serializable]
struct PerceptionRgbSensor
{

public float[][] camera_intrinsic;
public string projection;
public static PerceptionRgbSensor Convert(OldPerceptionConsumer consumer, RgbSensor inRgb, string path)
public static PerceptionRgbSensor Convert(RgbSensor inRgb)
{
return new PerceptionRgbSensor
{

};
}
}
// ReSharper enable NotAccessedField.Local
// ReSharper enable InconsistentNaming
}
}

303
com.unity.perception/Runtime/GroundTruth/Consumers/OldPerceptionJsonFactory.cs


using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;

// ReSharper disable InconsistentNaming
// ReSharper disable NotAccessedField.Local
public static JToken Convert(OldPerceptionConsumer consumer, string id, AnnotationDefinition def)
public static JToken Convert(OldPerceptionConsumer consumer, string id, AnnotationDefinition annotationDefinition)
switch (def)
switch (annotationDefinition)
case BoundingBox2DLabeler.BoundingBoxAnnotationDefinition b:
return JToken.FromObject(LabelConfigurationAnnotationDefinition.Convert(b, "json", b.spec));
case BoundingBox3DLabeler.BoundingBox3DAnnotationDefinition d:
return JToken.FromObject(LabelConfigurationAnnotationDefinition.Convert(d, "json", d.spec));
case InstanceSegmentationLabeler.InstanceSegmentationDefinition d:
return JToken.FromObject(LabelConfigurationAnnotationDefinition.Convert(d, "PNG", d.spec));
case SemanticSegmentationLabeler.SemanticSegmentationDefinition d:
return JToken.FromObject(PerceptionSemanticSegmentationAnnotationDefinition.Convert(consumer, d));
case BoundingBox2DLabeler.BoundingBoxAnnotationDefinition def:
return JToken.FromObject(LabelConfigurationAnnotationDefinition.Convert(def, "json", def.spec));
case BoundingBox3DLabeler.BoundingBox3DAnnotationDefinition def:
return JToken.FromObject(LabelConfigurationAnnotationDefinition.Convert(def, "json", def.spec));
case InstanceSegmentationLabeler.InstanceSegmentationDefinition def:
return JToken.FromObject(LabelConfigurationAnnotationDefinition.Convert(def, "PNG", def.spec));
case SemanticSegmentationLabeler.SemanticSegmentationDefinition def:
return JToken.FromObject(PerceptionSemanticSegmentationAnnotationDefinition.Convert(def, "PNG"));
case KeypointLabeler.Definition kp:
return JToken.FromObject(PerceptionKeypointAnnotationDefinition.Convert(consumer, kp));
}

return null;
}
}
[Serializable]
struct LabelConfigurationAnnotationDefinition
[Serializable]
struct LabelConfigMetricDefinition
{
LabelConfigMetricDefinition(string id, string name, string description, IdLabelConfig.LabelEntrySpec[] spec)
public string id;
public string name;
public string description;
public string format;
public IdLabelConfig.LabelEntrySpec[] spec;
this.id = id;
this.name = name;
this.description = description;
this.spec = spec;
}
public static LabelConfigurationAnnotationDefinition Convert(AnnotationDefinition def, string format, IdLabelConfig.LabelEntrySpec[] spec)
{
return new LabelConfigurationAnnotationDefinition
{
id = def.id,
name = def.id,
description = def.description,
format = format,
spec = spec
};
}
public string id;
public string name;
public string description;
public IdLabelConfig.LabelEntrySpec[] spec;
public JToken ToJToken()
{
return JToken.FromObject(this);
[Serializable]
struct GenericMetricDefinition
public static LabelConfigMetricDefinition Convert(string id, MetricDefinition def, IdLabelConfig.LabelEntrySpec[] spec)
public string id;
public string name;
public string description;
return new LabelConfigMetricDefinition(id, def.id, def.description, spec);
}
}
public static GenericMetricDefinition Convert(string id, MetricDefinition def)
{
return new GenericMetricDefinition
{
id = id,
name = def.id,
description = def.description
};
}
[Serializable]
struct LabelConfigurationAnnotationDefinition
{
public string id;
public string name;
public string description;
public string format;
public IdLabelConfig.LabelEntrySpec[] spec;
LabelConfigurationAnnotationDefinition(AnnotationDefinition def, string format, IdLabelConfig.LabelEntrySpec[] spec)
{
id = def.id;
name = def.id;
description = def.description;
this.format = format;
this.spec = spec;
struct LabelConfigMetricDefinition
public static LabelConfigurationAnnotationDefinition Convert(AnnotationDefinition def, string format, IdLabelConfig.LabelEntrySpec[] spec)
public string id;
public string name;
public string description;
public IdLabelConfig.LabelEntrySpec[] spec;
return new LabelConfigurationAnnotationDefinition(def, format, spec);
}
}
public static LabelConfigMetricDefinition Convert(string id, MetricDefinition def, IdLabelConfig.LabelEntrySpec[] spec)
{
return new LabelConfigMetricDefinition
{
id = id,
name = def.id,
description = def.description,
spec = spec
};
}
[Serializable]
struct GenericMetricDefinition
{
public string id;
public string name;
public string description;
public GenericMetricDefinition(string id, MetricDefinition def)
{
this.id = id;
name = def.id;
description = def.description;
}
public static GenericMetricDefinition Convert(string id, MetricDefinition def)
{
return new GenericMetricDefinition(id, def);
public struct PerceptionKeyPointValue
struct PerceptionKeyPointValue
{
public string id;
public string annotation_definition;

public struct Keypoint
internal struct Keypoint
Keypoint(KeypointLabeler.Keypoint kp)
{
index = kp.index;
location = kp.location;
state = kp.state;
}
return new Keypoint
{
index = kp.index,
location = kp.location,
state = kp.state
};
return new Keypoint(kp);
public struct Entry
internal struct Entry
{
public int label_id;
public uint instance_id;

Entry(KeypointLabeler.Annotation.Entry entry)
{
label_id = entry.labelId;
instance_id = entry.instanceId;
template_guid = entry.templateGuid;
pose = entry.pose;
keypoints = entry.keypoints.Select(Keypoint.Convert).ToArray();
}
return new Entry
{
label_id = entry.labelId,
instance_id = entry.instanceId,
template_guid = entry.templateGuid,
pose = entry.pose,
keypoints = entry.keypoints.Select(Keypoint.Convert).ToArray()
};
return new Entry(entry);
PerceptionKeyPointValue(KeypointLabeler.Annotation kp)
{
id = kp.Id;
annotation_definition = kp.description;
values = kp.entries.Select(Entry.Convert).ToList();
}
return new PerceptionKeyPointValue
{
id = kp.Id,
annotation_definition = kp.description,
values = kp.entries.Select(Entry.Convert).ToList()
};
return new PerceptionKeyPointValue(kp);
}
}

}
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
[Serializable]
Entry(InstanceSegmentationLabeler.InstanceSegmentation.Entry entry)
{
instance_id = entry.instanceId;
color = entry.rgba;
}
return new Entry
{
instance_id = entry.instanceId,
color = entry.rgba
};
return new Entry(entry);
}
}

public List<Entry> values;
PerceptionInstanceSegmentationValue(OldPerceptionConsumer consumer, int frame, InstanceSegmentationLabeler.InstanceSegmentation annotation)
{
id = annotation.Id;
annotation_definition = annotation.description;
filename = consumer.RemoveDatasetPathPrefix(CreateFile(consumer, frame, annotation));
values = annotation.instances.Select(Entry.Convert).ToList();
}
static string CreateFile(OldPerceptionConsumer consumer, int frame, InstanceSegmentationLabeler.InstanceSegmentation annotation)
{
var path = consumer.VerifyDirectoryWithGuidExists("InstanceSegmentation");

public static PerceptionInstanceSegmentationValue Convert(OldPerceptionConsumer consumer, int frame, InstanceSegmentationLabeler.InstanceSegmentation annotation)
{
return new PerceptionInstanceSegmentationValue
{
id = Guid.NewGuid().ToString(),
annotation_definition = Guid.NewGuid().ToString(),
filename = consumer.RemoveDatasetPathPrefix(CreateFile(consumer, frame, annotation)),
values = annotation.instances.Select(Entry.Convert).ToList()
};
return new PerceptionInstanceSegmentationValue(consumer, frame, annotation);
struct LabelDefinitionEntry
{
public int label_id;
public string label_name;
}
struct PerceptionBounding3dBoxAnnotationValue
{
[Serializable]

public Vector3 velocity;
public Vector3 acceleration;
Entry(BoundingBox3DLabeler.BoundingBoxAnnotation.Entry entry)
{
label_id = entry.labelId;
label_name = entry.labelName;
instance_id = entry.instanceId;
translation = entry.translation;
size = entry.size;
rotation = entry.rotation;
velocity = entry.velocity;
acceleration = entry.acceleration;
}
return new Entry
{
label_id = entry.labelId,
label_name = entry.labelName,
instance_id = (uint)entry.instanceId,
translation = entry.translation,
size = entry.size,
rotation = entry.rotation,
velocity = entry.velocity,
acceleration = entry.acceleration
};
return new Entry(entry);
}
}

PerceptionBounding3dBoxAnnotationValue(string labelerId, string defId, BoundingBox3DLabeler.BoundingBoxAnnotation annotation)
{
id = labelerId;
annotation_definition = defId;
values = annotation.boxes.Select(Entry.Convert).ToList();
}
return new PerceptionBounding3dBoxAnnotationValue
{
id = labelerId,
annotation_definition = defId,
values = annotation.boxes.Select(Entry.Convert).ToList()
};
return new PerceptionBounding3dBoxAnnotationValue(labelerId, defId, annotation);
}
}

[Serializable]
Entry(SemanticSegmentationLabeler.SemanticSegmentationDefinition.DefinitionEntry e)
{
label_name = e.labelName;
pixel_value = e.pixelValue;
}
return new Entry
{
label_name = e.labelName,
pixel_value = e.pixelValue
};
return new Entry(e);
}
}

public string format;
public List<Entry> spec;
public static PerceptionSemanticSegmentationAnnotationDefinition Convert(OldPerceptionConsumer consumer, SemanticSegmentationLabeler.SemanticSegmentationDefinition def)
PerceptionSemanticSegmentationAnnotationDefinition(SemanticSegmentationLabeler.SemanticSegmentationDefinition def, string format)
{
id = def.id;
name = def.id;
description = def.description;
spec = def.spec.Select(Entry.Convert).ToList();
this.format = format;
}
public static PerceptionSemanticSegmentationAnnotationDefinition Convert(SemanticSegmentationLabeler.SemanticSegmentationDefinition def, string format)
return new PerceptionSemanticSegmentationAnnotationDefinition
{
id = def.id,
name = def.id,
description = def.description,
format = "PNG",
spec = def.spec.Select(Entry.Convert).ToList()
};
return new PerceptionSemanticSegmentationAnnotationDefinition(def, format);
}
}

[Serializable]
public struct JointJson
internal struct JointJson
{
public string label;
public int index;

}
[Serializable]
public struct SkeletonJson
internal struct SkeletonJson
internal static SkeletonJson Convert(KeypointLabeler.Definition.SkeletonDefinition skel)
internal static SkeletonJson Convert(KeypointLabeler.Definition.SkeletonDefinition skeleton)
joint1 = skel.joint1,
joint2 = skel.joint2,
color = skel.color
joint1 = skeleton.joint1,
joint2 = skeleton.joint2,
color = skeleton.color
public struct KeypointJson
internal struct KeypointJson
{
public string template_id;
public string template_name;

}
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
[Serializable]
struct PerceptionBoundingBoxAnnotationValue
{

22
com.unity.perception/Runtime/Randomization/Scenarios/PerceptionScenario.cs


/// <inheritdoc/>
protected override void OnStart()
{
// Manager.Instance.ShutdownCondition = new ShutdownCondition();
// DatasetCapture.Instance.ResetSimulation();
#if true
m_IterationMetricDefinition = new MetricDefinition("scenario_iteration", "Iteration information for dataset sequences");
DatasetCapture.Instance.RegisterMetric(m_IterationMetricDefinition);

DatasetCapture.Instance.ReportMetric(randomSeedMetricDefinition, new object[] { genericConstants.randomSeed });
#endif
}
/// <inheritdoc/>

#if true
#endif
}
static IEnumerator WaitUntilWritesAreComplete()

}
/// <inheritdoc/>
#if false
protected override IEnumerator OnComplete()
{
yield return StartCoroutine(DatasetCapture.Instance.ResetSimulation());
#else
//Manager.Instance.ShutdownAfterFrames(105);
//Manager.Instance.Shutdown();
//DatasetCapture.Instance.ResetSimulation();
#endif
#if true
/// <summary>
/// Used to report a scenario iteration as a perception metric
/// </summary>

public int iteration;
}
#endif
}
}

6
com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs


{
foreach (var randomizer in activeRandomizers)
randomizer.ScenarioComplete();
#if false
StartCoroutine(OnComplete());
#else
#endif
state = State.Idle;
OnIdle();
return;

1
com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenario.cs


{
var filePath = new Uri(Configuration.Instance.SimulationConfig.app_param_uri).LocalPath;
LoadConfigurationFromFile(filePath);
// PlayerPrefs.SetString(SimulationState.outputFormatMode, constants.outputFormat);
}
else
{

2
com.unity.perception/Runtime/Randomization/Scenarios/UnitySimulationScenarioConstants.cs


/// </summary>
[Tooltip("The Unity Simulation instance index of the currently executing worker.")]
public int instanceIndex;
public string outputFormat;
}
}

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


using System;
using System.Collections;
using GroundTruthTests;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Perception.Randomization.Randomizers.SampleRandomizers;

[UnityTest]
public IEnumerator StartNewDatasetSequenceEveryIteration()
{
var collector = new CollectEndpoint();
DatasetCapture.SetEndpoint(collector);
// Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, 0);
Assert.AreEqual(DatasetCapture.Instance.currentSimulation.SequenceTime, 0);
// Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, perceptionCamera.simulationDeltaTime);
Assert.AreEqual(DatasetCapture.Instance.currentSimulation.SequenceTime, perceptionCamera.simulationDeltaTime);
// Assert.AreEqual(DatasetCapture.SimulationState.SequenceTime, 0);
Assert.AreEqual(DatasetCapture.Instance.currentSimulation.SequenceTime, 0);
}
[UnityTest]

正在加载...
取消
保存