浏览代码

Do not generate any empty folders if the PerceptionCamera is never enabled (#62)

* Do not generate any dataset if the PerceptionCamera is never enabled and no other dataset generation APIs are called.

* Fixing style and performance issues
/main
GitHub 4 年前
当前提交
f607d8e7
共有 4 个文件被更改,包括 75 次插入14 次删除
  1. 5
      com.unity.perception/Runtime/GroundTruth/DatasetCapture.cs
  2. 29
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  3. 20
      com.unity.perception/Runtime/GroundTruth/SimulationState.cs
  4. 35
      com.unity.perception/Tests/Runtime/GroundTruthTests/PerceptionCameraIntegrationTests.cs

5
com.unity.perception/Runtime/GroundTruth/DatasetCapture.cs


using System;
using System.IO;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using Unity.Simulation;

static readonly Guid k_DatasetGuid = Guid.NewGuid();
internal static SimulationState SimulationState { get; private set; } = CreateSimulationData();
internal static string OutputDirectory => SimulationState.OutputDirectory;
internal static string OutputDirectory => SimulationState.GetOutputDirectoryNoCreate();
/// <summary>
/// The json metadata schema version the DatasetCapture's output conforms to.

static SimulationState CreateSimulationData()
{
//TODO: Remove the Guid path when we have proper dataset merging in USim/Thea
return new SimulationState(Manager.Instance.GetDirectoryFor($"Dataset{k_DatasetGuid}"));
return new SimulationState($"Dataset{k_DatasetGuid}");
}
[RuntimeInitializeOnLoadMethod]

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


Dictionary<string, object> m_PersistentSensorData = new Dictionary<string, object>();
int m_LastFrameCaptured = -1;
Ego m_EgoMarker;
#pragma warning disable 414
//only used to confirm that GroundTruthRendererFeature is present in URP

/// <summary>
/// The <see cref="SensorHandle"/> associated with this camera. Use this to report additional annotations and metrics at runtime.
/// </summary>
public SensorHandle SensorHandle { get; private set; }
public SensorHandle SensorHandle
{
get
{
EnsureSensorRegistered();
return m_SensorHandle;
}
private set => m_SensorHandle = value;
}
SensorHandle m_SensorHandle;
Ego m_EgoMarker;
static ProfilerMarker s_WriteFrame = new ProfilerMarker("Write Frame (PerceptionCamera)");
static ProfilerMarker s_EncodeAndSave = new ProfilerMarker("Encode and save (PerceptionCamera)");

// Start is called before the first frame update
void Awake()
{
m_EgoMarker = this.GetComponentInParent<Ego>();
var ego = m_EgoMarker == null ? DatasetCapture.RegisterEgo("") : m_EgoMarker.EgoHandle;
SensorHandle = DatasetCapture.RegisterSensor(ego, "camera", description, period, startTime);
AsyncRequest.maxJobSystemParallelism = 0; // Jobs are not chained to one another in any way, maximizing parallelism
AsyncRequest.maxAsyncRequestFrameAge = 4; // Ensure that readbacks happen before Allocator.TempJob allocations get stale

#endif
DatasetCapture.SimulationEnding += OnSimulationEnding;
}
void EnsureSensorRegistered()
{
if (m_SensorHandle.IsNil)
{
m_EgoMarker = GetComponentInParent<Ego>();
var ego = m_EgoMarker == null ? DatasetCapture.RegisterEgo("") : m_EgoMarker.EgoHandle;
SensorHandle = DatasetCapture.RegisterSensor(ego, "camera", description, period, startTime);
}
}
void OnEnable()

// Update is called once per frame
void Update()
{
EnsureSensorRegistered();
if (!SensorHandle.IsValid)
return;

20
com.unity.perception/Runtime/GroundTruth/SimulationState.cs


using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using Unity.Simulation;
using UnityEngine;
using UnityEngine.Profiling;

{
partial class SimulationState
{
public string OutputDirectory { get; }
HashSet<SensorHandle> m_ActiveSensors = new HashSet<SensorHandle>();
Dictionary<SensorHandle, SensorData> m_Sensors = new Dictionary<SensorHandle, SensorData>();
HashSet<EgoHandle> m_Egos = new HashSet<EgoHandle>();

CustomSampler m_SerializeMetricsAsyncSampler = CustomSampler.Create("SerializeMetricsAsync");
CustomSampler m_GetOrCreatePendingCaptureForThisFrameSampler = CustomSampler.Create("GetOrCreatePendingCaptureForThisFrame");
float m_LastTimeScale;
readonly string m_OutputDirectoryName;
string m_OutputDirectoryPath;
public string OutputDirectory
{
get
{
if (m_OutputDirectoryPath == null)
m_OutputDirectoryPath = Manager.Instance.GetDirectoryFor(m_OutputDirectoryName);
return m_OutputDirectoryPath;
}
}
//A sensor will be triggered if sequenceTime is within includeThreshold seconds of the next trigger
const float k_IncludeInFrameThreshold = .01f;
const int k_MinPendingCapturesBeforeWrite = 150;

public SimulationState(string outputDirectory)
{
OutputDirectory = outputDirectory;
m_OutputDirectoryName = outputDirectory;
IsRunning = true;
}

return m_UnscaledSequenceTimeDoNotUse;
}
}
public string GetOutputDirectoryNoCreate() => Path.Combine(Configuration.Instance.GetStoragePath(), m_OutputDirectoryName);
void EnsureSequenceTimingsUpdated()
{

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


}
[UnityTest]
public IEnumerator EnableSemanticSegmentation_GeneratesCorrectDataset()
public IEnumerator EnableSemanticSegmentation_GeneratesCorrectDataset([Values(true, false)] bool enabled)
{
SetupCamera(pc =>
{
pc.AddLabeler(new SemanticSegmentationLabeler(CreateSemanticSegmentationLabelConfig()));
}, enabled);
string expectedImageFilename = $"segmentation_{Time.frameCount}.png";
this.AddTestObjectForCleanup(TestHelper.CreateLabeledPlane());
yield return null;
DatasetCapture.ResetSimulation();
if (enabled)
{
var capturesPath = Path.Combine(DatasetCapture.OutputDirectory, "captures_000.json");
var capturesJson = File.ReadAllText(capturesPath);
var imagePath = $"SemanticSegmentation/{expectedImageFilename}";
StringAssert.Contains(imagePath, capturesJson);
}
else
{
DirectoryAssert.DoesNotExist(DatasetCapture.OutputDirectory);
}
}
[UnityTest]
public IEnumerator Disabled_GeneratesCorrectDataset()
{
SetupCamera(pc =>
{

return labelingConfiguration;
}
void SetupCamera(Action<PerceptionCamera> initPerceptionCamera)
void SetupCamera(Action<PerceptionCamera> initPerceptionCamera, bool activate = true)
{
var cameraObject = new GameObject();
cameraObject.SetActive(false);

perceptionCamera.captureRgbImages = false;
initPerceptionCamera?.Invoke(perceptionCamera);
cameraObject.SetActive(true);
if (activate)
cameraObject.SetActive(true);
AddTestObjectForCleanup(cameraObject);
}
}
正在加载...
取消
保存