浏览代码

Merge pull request #159 from Unity-Technologies/isolate-sampler-random-state

Isolate sampler random state from scenarios
/main
GitHub 4 年前
当前提交
7ba383fe
共有 12 个文件被更改,包括 52 次插入30 次删除
  1. 5
      com.unity.perception/CHANGELOG.md
  2. 2
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/BackgroundObjectPlacementRandomizer.cs
  3. 4
      com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ForegroundObjectPlacementRandomizer.cs
  4. 2
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/AnimationCurveSampler.cs
  5. 2
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs
  6. 2
      com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs
  7. 1
      com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs
  8. 18
      com.unity.perception/Runtime/Randomization/Scenarios/ScenarioBase.cs
  9. 6
      com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs
  10. 5
      com.unity.perception/Tests/Runtime/Randomization/ScenarioTests.cs
  11. 32
      com.unity.perception/Runtime/Randomization/Samplers/SamplerState.cs
  12. 3
      com.unity.perception/Runtime/Randomization/Samplers/SamplerState.cs.meta

5
com.unity.perception/CHANGELOG.md


Unique seeds per Sampler have been replaced with one global random seed configured via the ScenarioConstants of a Scenario
Replaced ScenarioBase.GenerateRandomSeed() with ScenarioBase.NextRandomState()
Samplers now derive their random state from the static SamplerState class instead of individual scenarios to allow parameters and samplers to be used outside of the context of a scenario
Replaced ScenarioBase.GenerateRandomSeed() with SamplerState.NextRandomState() and SamplerState.CreateGenerator()
### Deprecated

2
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/BackgroundObjectPlacementRandomizer.cs


{
for (var i = 0; i < layerCount; i++)
{
var seed = scenario.NextRandomState();
var seed = SamplerState.NextRandomState();
var placementSamples = PoissonDiskSampling.GenerateSamples(
placementArea.x, placementArea.y, separationDistance, seed);
var offset = new Vector3(placementArea.x, placementArea.y, 0f) * -0.5f;

4
com.unity.perception/Runtime/Randomization/Randomizers/RandomizerExamples/Randomizers/ForegroundObjectPlacementRandomizer.cs


using System;
using System.Collections.Generic;
using UnityEngine.Experimental.Perception.Randomization.Samplers;
namespace UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers
{

/// </summary>
protected override void OnIterationStart()
{
var seed = scenario.NextRandomState();
var seed = SamplerState.NextRandomState();
var placementSamples = PoissonDiskSampling.GenerateSamples(
placementArea.x, placementArea.y, separationDistance, seed);
var offset = new Vector3(placementArea.x, placementArea.y, 0f) * -0.5f;

2
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/AnimationCurveSampler.cs


public float Sample()
{
Initialize();
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
var rng = SamplerState.CreateGenerator();
return SamplerUtility.AnimationCurveSample(
m_IntegratedCurve, rng.NextFloat(), m_Interval, m_StartTime, m_EndTime);
}

2
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs


/// <returns>The generated sample</returns>
public float Sample()
{
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
var rng = SamplerState.CreateGenerator();
return SamplerUtility.TruncatedNormalSample(
rng.NextFloat(), range.minimum, range.maximum, mean, standardDeviation);
}

2
com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs


/// <returns>The generated sample</returns>
public float Sample()
{
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState());
var rng = SamplerState.CreateGenerator();
return rng.NextFloat(range.minimum, range.maximum);
}

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


using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine.Experimental.Perception.Randomization.Parameters;

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


{
static ScenarioBase s_ActiveScenario;
uint m_RandomState = SamplerUtility.largePrime;
bool m_SkipFrame = true;
bool m_FirstScenarioFrame = true;
bool m_WaitingForFinalUploads;

/// If true, this scenario will quit the Unity application when it's finished executing
/// </summary>
[HideInInspector] public bool quitOnComplete = true;
/// <summary>
/// The random state of the scenario
/// </summary>
public uint randomState => m_RandomState;
/// <summary>
/// The name of the Json file this scenario's constants are serialized to/from.

if (currentIterationFrame == 0)
{
DatasetCapture.StartNewSequence();
m_RandomState = SamplerUtility.IterateSeed((uint)currentIteration, genericConstants.randomSeed);
SamplerState.randomState = SamplerUtility.IterateSeed((uint)currentIteration, genericConstants.randomSeed);
foreach (var randomizer in activeRandomizers)
randomizer.IterationStart();
}

var randomizer = m_Randomizers[currentIndex];
m_Randomizers.RemoveAt(currentIndex);
m_Randomizers.Insert(nextIndex, randomizer);
}
/// <summary>
/// Generates a new random state and overwrites the old random state with the newly generated value
/// </summary>
/// <returns>The newly generated random state</returns>
public uint NextRandomState()
{
m_RandomState = SamplerUtility.Hash32NonZero(m_RandomState);
return m_RandomState;
}
void ValidateParameters()

6
com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs


[Test]
public void ConsecutiveSamplesChangesState()
{
var state0 = activeScenario.randomState;
var state0 = SamplerState.randomState;
var state1 = activeScenario.randomState;
var state1 = SamplerState.randomState;
var state2 = activeScenario.randomState;;
var state2 = SamplerState.randomState;;
Assert.AreNotEqual(state0, state1);
Assert.AreNotEqual(state1, state2);

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


using NUnit.Framework;
using UnityEngine;
using UnityEngine.Experimental.Perception.Randomization.Randomizers.SampleRandomizers;
using UnityEngine.Experimental.Perception.Randomization.Samplers;
using UnityEngine.Perception.GroundTruth;
using UnityEngine.Experimental.Perception.Randomization.Scenarios;
using UnityEngine.TestTools;

yield return CreateNewScenario(3, 1);
var seeds = new uint[3];
for (var i = 0; i < 3; i++)
seeds[i] = m_Scenario.NextRandomState();
seeds[i] = SamplerState.NextRandomState();
Assert.AreNotEqual(seeds[i], m_Scenario.NextRandomState());
Assert.AreNotEqual(seeds[i], SamplerState.NextRandomState());
}
PerceptionCamera SetupPerceptionCamera()

32
com.unity.perception/Runtime/Randomization/Samplers/SamplerState.cs


namespace UnityEngine.Experimental.Perception.Randomization.Samplers
{
/// <summary>
/// Encapsulates the random state that all samplers mutate when generating random values
/// </summary>
public static class SamplerState
{
/// <summary>
/// The central random state that all samplers mutate when generating random numbers
/// </summary>
public static uint randomState = SamplerUtility.largePrime;
/// <summary>
/// Creates a random number generator seeded with a unique random state
/// </summary>
/// <returns>The seeded random number generator</returns>
public static Unity.Mathematics.Random CreateGenerator()
{
return new Unity.Mathematics.Random { state = NextRandomState() };
}
/// <summary>
/// Generates a new random state and overwrites the old random state with the newly generated value
/// </summary>
/// <returns>The newly generated random state</returns>
public static uint NextRandomState()
{
randomState = SamplerUtility.Hash32NonZero(randomState);
return randomState;
}
}
}

3
com.unity.perception/Runtime/Randomization/Samplers/SamplerState.cs.meta


fileFormatVersion: 2
guid: dab4e2f4d003402193a82cfe926b91d0
timeCreated: 1611261496
正在加载...
取消
保存