浏览代码

Grid sensor bug fixes (#5230)

* use all zeros for empty in one-hot

* fix a bug in calculating cell position

* remove root reference from UI

* removed unused normalized distance in GetObjectData()
/check-for-ModelOverriders
GitHub 4 年前
当前提交
24aa2801
共有 13 个文件被更改,包括 1580 次插入587 次删除
  1. 25
      Project/Assets/ML-Agents/Examples/FoodCollector/Prefabs/FoodCollectorArea.prefab
  2. 995
      Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/GridFoodCollector.onnx
  3. 1
      com.unity.ml-agents.extensions/Editor/GridSensorComponentEditor.cs
  4. 62
      com.unity.ml-agents.extensions/Runtime/Sensors/GridSensor.cs
  5. 14
      com.unity.ml-agents.extensions/Runtime/Sensors/GridSensorComponent.cs
  6. 14
      com.unity.ml-agents.extensions/Tests/Editor/Sensors/ChannelHotPerceiveTests.cs
  7. 8
      com.unity.ml-agents.extensions/Tests/Editor/Sensors/GridObservationPerceiveTests.cs
  8. 5
      com.unity.ml-agents.extensions/Tests/Utils/GridObsTestComponents/SimpleTestGridSensor.cs
  9. 14
      Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/GridFoodCollector.onnx.meta
  10. 1001
      Project/Assets/ML-Agents/Examples/GridFoodCollector.onnx
  11. 14
      Project/Assets/ML-Agents/Examples/GridFoodCollector.onnx.meta
  12. 14
      Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/FoodCollector.onnx.meta
  13. 0
      /Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/GridFoodCollector.onnx

25
Project/Assets/ML-Agents/Examples/FoodCollector/Prefabs/FoodCollectorArea.prefab


VectorActionDescriptions: []
VectorActionSpaceType: 0
hasUpgradedBrainParametersWithActionSpec: 1
m_Model: {fileID: 11400000, guid: 75910f45f20be49b18e2b95879a217b2, type: 3}
m_Model: {fileID: 5022602860645237092, guid: f4b8757bd574543b1aa06d9294a5d707, type: 3}
m_InferenceDevice: 2
m_BehaviorType: 0
m_BehaviorName: GridFoodCollector

m_CellScale: {x: 1, y: 0.01, z: 1}
m_GridSize: {x: 40, y: 1, z: 40}
m_RotateWithAgent: 1
m_ChannelDepths: 06000000
m_ChannelDepths: 05000000
m_DetectableObjects:
- food
- agent

serializedVersion: 2
m_Bits: 307
m_DepthType: 1
m_RootReference: {fileID: 0}
m_MaxColliderBufferSize: 500
m_InitialColliderBufferSize: 4
m_DebugColors:

VectorActionDescriptions: []
VectorActionSpaceType: 0
hasUpgradedBrainParametersWithActionSpec: 1
m_Model: {fileID: 11400000, guid: 75910f45f20be49b18e2b95879a217b2, type: 3}
m_Model: {fileID: 5022602860645237092, guid: f4b8757bd574543b1aa06d9294a5d707, type: 3}
m_InferenceDevice: 2
m_BehaviorType: 0
m_BehaviorName: GridFoodCollector

m_CellScale: {x: 1, y: 0.01, z: 1}
m_GridSize: {x: 40, y: 1, z: 40}
m_RotateWithAgent: 1
m_ChannelDepths: 06000000
m_ChannelDepths: 05000000
m_DetectableObjects:
- food
- agent

serializedVersion: 2
m_Bits: 307
m_DepthType: 1
m_RootReference: {fileID: 0}
m_MaxColliderBufferSize: 500
m_InitialColliderBufferSize: 4
m_DebugColors:

VectorActionDescriptions: []
VectorActionSpaceType: 0
hasUpgradedBrainParametersWithActionSpec: 1
m_Model: {fileID: 11400000, guid: 75910f45f20be49b18e2b95879a217b2, type: 3}
m_Model: {fileID: 5022602860645237092, guid: f4b8757bd574543b1aa06d9294a5d707, type: 3}
m_InferenceDevice: 2
m_BehaviorType: 0
m_BehaviorName: GridFoodCollector

m_CellScale: {x: 1, y: 0.01, z: 1}
m_GridSize: {x: 40, y: 1, z: 40}
m_RotateWithAgent: 1
m_ChannelDepths: 06000000
m_ChannelDepths: 05000000
m_DetectableObjects:
- food
- agent

serializedVersion: 2
m_Bits: 307
m_DepthType: 1
m_RootReference: {fileID: 0}
m_MaxColliderBufferSize: 500
m_InitialColliderBufferSize: 4
m_DebugColors:

VectorActionDescriptions: []
VectorActionSpaceType: 0
hasUpgradedBrainParametersWithActionSpec: 1
m_Model: {fileID: 11400000, guid: 75910f45f20be49b18e2b95879a217b2, type: 3}
m_Model: {fileID: 5022602860645237092, guid: f4b8757bd574543b1aa06d9294a5d707, type: 3}
m_InferenceDevice: 2
m_BehaviorType: 0
m_BehaviorName: GridFoodCollector

m_CellScale: {x: 1, y: 0.01, z: 1}
m_GridSize: {x: 40, y: 1, z: 40}
m_RotateWithAgent: 1
m_ChannelDepths: 06000000
m_ChannelDepths: 05000000
m_DetectableObjects:
- food
- agent

serializedVersion: 2
m_Bits: 307
m_DepthType: 1
m_RootReference: {fileID: 0}
m_MaxColliderBufferSize: 500
m_InitialColliderBufferSize: 4
m_DebugColors:

VectorActionDescriptions: []
VectorActionSpaceType: 0
hasUpgradedBrainParametersWithActionSpec: 1
m_Model: {fileID: 11400000, guid: 75910f45f20be49b18e2b95879a217b2, type: 3}
m_Model: {fileID: 5022602860645237092, guid: f4b8757bd574543b1aa06d9294a5d707, type: 3}
m_InferenceDevice: 2
m_BehaviorType: 0
m_BehaviorName: GridFoodCollector

m_CellScale: {x: 1, y: 0.01, z: 1}
m_GridSize: {x: 40, y: 1, z: 40}
m_RotateWithAgent: 1
m_ChannelDepths: 06000000
m_ChannelDepths: 05000000
m_DetectableObjects:
- food
- agent

serializedVersion: 2
m_Bits: 307
m_DepthType: 1
m_RootReference: {fileID: 0}
m_MaxColliderBufferSize: 500
m_InitialColliderBufferSize: 4
m_DebugColors:

995
Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/GridFoodCollector.onnx
文件差异内容过多而无法显示
查看文件

1
com.unity.ml-agents.extensions/Editor/GridSensorComponentEditor.cs


var gridSize2d = new Vector2Int(gridSize.vector3IntValue.x, gridSize.vector3IntValue.z);
var newGridSize = EditorGUILayout.Vector2IntField("Grid Size", gridSize2d);
gridSize.vector3IntValue = new Vector3Int(newGridSize.x, 1, newGridSize.y);
EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_RootReference)), true);
}
EditorGUI.EndDisabledGroup();
EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_RotateWithAgent)), true);

62
com.unity.ml-agents.extensions/Runtime/Sensors/GridSensor.cs


// Buffers
internal float[] m_PerceptionBuffer;
float[] m_ChannelHotDefaultPerceptionBuffer;
Color[] m_PerceptionColors;
Texture2D m_PerceptionTexture;
Collider[] m_ColliderBuffer;

// Utility Constants Calculated on Init
int m_NumCells;
int m_CellObservationSize;
float m_InverseSphereRadius;
float m_OffsetGridNumSide;
Vector3 m_CellCenterOffset;
public GridSensor(

set { m_ColliderMask = value; }
}
public int[] GizmoColorIndexes
{
get { return m_GizmoColorIndexes; }
}
/// <summary>
/// Initializes the constant parameters used within the perceive method call
/// </summary>

float sphereRadiusX = (m_CellScale.x * m_GridSize.x) / Mathf.Sqrt(2);
float sphereRadiusZ = (m_CellScale.z * m_GridSize.z) / Mathf.Sqrt(2);
m_InverseSphereRadius = 1.0f / Mathf.Max(sphereRadiusX, sphereRadiusZ);
m_OffsetGridNumSide = (m_GridSize.z - 1f) / 2f;
m_CellCenterOffset = new Vector3((m_GridSize.x - 1f) / 2, 0, (m_GridSize.z - 1f) / 2);
}
/// <summary>

{
m_ChannelOffsets[i] = m_ChannelOffsets[i - 1] + m_ChannelDepths[i - 1];
}
m_ChannelHotDefaultPerceptionBuffer = new float[m_CellObservationSize];
for (int i = 0; i < m_ChannelDepths.Length; i++)
{
if (m_ChannelDepths[i] > 1)
{
m_ChannelHotDefaultPerceptionBuffer[m_ChannelOffsets[i]] = 1;
}
}
}
else
{

{
if (m_PerceptionBuffer != null)
{
if (m_GridDepthType == GridDepthType.ChannelHot)
{
// Copy the default value to the array
for (int i = 0; i < m_NumCells; i++)
{
Array.Copy(m_ChannelHotDefaultPerceptionBuffer, 0, m_PerceptionBuffer, i * m_CellObservationSize, m_CellObservationSize);
}
}
else
{
Array.Clear(m_PerceptionBuffer, 0, m_PerceptionBuffer.Length);
}
Array.Clear(m_PerceptionBuffer, 0, m_PerceptionBuffer.Length);
}
else
{

if (!ReferenceEquals(closestColliderGo, null))
{
LoadObjectData(closestColliderGo, cellIndex, (float)Math.Sqrt(minDistanceSquared) * m_InverseSphereRadius);
LoadObjectData(closestColliderGo, cellIndex);
}
Profiler.EndSample();
}

closestColliderPoint = foundColliders[i].ClosestPointOnBounds(cellCenter);
LoadObjectData(currentColliderGo, cellIndex,
Vector3.Distance(closestColliderPoint, m_RootReference.transform.position) * m_InverseSphereRadius);
LoadObjectData(currentColliderGo, cellIndex);
}
Profiler.EndSample();
}

/// }
/// </code>
/// </example>
protected virtual float[] GetObjectData(GameObject currentColliderGo, float typeIndex, float normalizedDistance)
protected virtual float[] GetObjectData(GameObject currentColliderGo, int typeIndex)
{
Array.Clear(m_CellDataBuffer, 0, m_CellDataBuffer.Length);
m_CellDataBuffer[0] = typeIndex;

/// </summary>
/// <param name="currentColliderGo">The game object that was found colliding with a certain cell</param>
/// <param name="cellIndex">The index of the current cell</param>
/// <param name="normalizedDistance">A float between 0 and 1 describing the ratio of
/// the distance currentColliderGo is compared to the edge of the gridsensor</param>
protected virtual void LoadObjectData(GameObject currentColliderGo, int cellIndex, float normalizedDistance)
protected virtual void LoadObjectData(GameObject currentColliderGo, int cellIndex)
if (m_GridDepthType != GridDepthType.Counting)
{
for (var ii = 0; ii < channelHotVals.Count; ii++)
{
m_PerceptionBuffer[channelHotVals.Offset + ii] = 0f;
}
}
// Using i+1 as the type index as "0" represents "empty"
var channelValues = GetObjectData(currentColliderGo, (float)i + 1, normalizedDistance);
var channelValues = GetObjectData(currentColliderGo, i);
ValidateValues(channelValues, currentColliderGo);
switch (m_GridDepthType)

/// <param name="cell">The index of the cell</param>
Vector3 CellToLocalPoint(int cellIndex)
{
float x = (cellIndex % m_GridSize.z - m_OffsetGridNumSide) * m_CellScale.x;
float z = (cellIndex / m_GridSize.z - m_OffsetGridNumSide) * m_CellScale.z - (m_GridSize.z - m_GridSize.x);
float x = (cellIndex / m_GridSize.z - m_CellCenterOffset.x) * m_CellScale.x;
float z = (cellIndex % m_GridSize.z - m_CellCenterOffset.z) * m_CellScale.z;
return new Vector3(x, 0, z);
}

14
com.unity.ml-agents.extensions/Runtime/Sensors/GridSensorComponent.cs


}
[HideInInspector, SerializeField]
internal GameObject m_RootReference;
/// <summary>
/// The reference of the root of the agent. This is used to disambiguate objects with the same tag as the agent. Defaults to current GameObject.
/// Note that changing this after the sensor is created has no effect.
/// </summary>
public GameObject RootReference
{
get { return m_RootReference == null ? gameObject : m_RootReference; }
set { m_RootReference = value; }
}
[HideInInspector, SerializeField]
internal int m_MaxColliderBufferSize = 500;
/// <summary>
/// The absolute max size of the Collider buffer used in the non-allocating Physics calls. In other words

m_DetectableObjects,
m_ColliderMask,
m_DepthType,
RootReference,
gameObject,
m_CompressionType,
m_MaxColliderBufferSize,
m_InitialColliderBufferSize

14
com.unity.ml-agents.extensions/Tests/Editor/Sensors/ChannelHotPerceiveTests.cs


int[] subarrayIndicies = new[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new float[] { 0, 0, 1 }, 4);
float[] expectedDefault = new[] { 1.0f, 0.0f, 0.0f };
float[] expectedDefault = new[] { 0.0f, 0.0f, 0.0f };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new float[] { 0, 0, 1 }, 4);
float[] expectedDefault = new float[] { 1, 0, 0 };
float[] expectedDefault = new float[] { 0, 0, 0 };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { .4f, 0, 1, 0 }, 4);
float[] expectedDefault = new float[] { 0, 1, 0, 0 };
float[] expectedDefault = new float[] { 0, 0, 0, 0 };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

int[] subarrayIndicies = new[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { 0, 1, 0, .4f }, 4);
float[] expectedDefault = new float[] { 1, 0, 0, 0 };
float[] expectedDefault = new float[] { 0, 0, 0, 0 };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new float[] { 0, 1, 0, 0, 0, 1 }, 4);
float[] expectedDefault = new float[] { 1, 0, 0, 1, 0, 0 };
float[] expectedDefault = new float[] { 0, 0, 0, 0, 0, 0 };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { 0, 0, 0, 1, 0, .6f, 0, 0, 1 }, 4);
float[] expectedDefault = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0 };
float[] expectedDefault = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { 0, 0, 0, 1, 0, .6f, 0, 0, 1 }, 4);
float[] expectedDefault = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0 };
float[] expectedDefault = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
Object.DestroyImmediate(boxGo);

8
com.unity.ml-agents.extensions/Tests/Editor/Sensors/GridObservationPerceiveTests.cs


Assert.AreEqual(10 * 10 * 3, gridSensor.m_PerceptionBuffer.Length);
int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new float[] { 0, 0, 1 }, 4);
float[] expectedDefault = new float[] { 1, 0, 0 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new float[] { 0, 1, 0 }, 4);
float[] expectedDefault = new float[] { 0, 0, 0 };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

Assert.AreEqual(10 * 10 * 1, gridSensor.m_PerceptionBuffer.Length);
int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { 2f / 3f }, 4);
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { 1f / 3f }, 4);
float[] expectedDefault = new float[] { 0f };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

Assert.AreEqual(10 * 10 * 1, gridSensor.m_PerceptionBuffer.Length);
int[] subarrayIndicies = new int[] { 77, 78, 87, 88 };
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { 1f / 3f }, 4);
float[][] expectedSubarrays = GridObsTestUtils.DuplicateArray(new[] { 0f }, 4);
float[] expectedDefault = new float[] { 0f };
GridObsTestUtils.AssertSubarraysAtIndex(gridSensor.m_PerceptionBuffer, subarrayIndicies, expectedSubarrays, expectedDefault);
}

5
com.unity.ml-agents.extensions/Tests/Utils/GridObsTestComponents/SimpleTestGridSensor.cs


maxColliderBufferSize,
initialColliderBufferSize)
{ }
protected override float[] GetObjectData(GameObject currentColliderGo,
float type_index, float normalized_distance)
protected override float[] GetObjectData(GameObject currentColliderGo, int type_index)
{
return (float[])currentColliderGo.GetComponent<GridSensorDummyData>().Data.Clone();
}

DetectableObjects,
ColliderMask,
DepthType,
RootReference,
gameObject,
CompressionType,
MaxColliderBufferSize,
InitialColliderBufferSize

14
Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/GridFoodCollector.onnx.meta


fileFormatVersion: 2
guid: f4b8757bd574543b1aa06d9294a5d707
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 683b6cb6d0a474744822c888b46772c9, type: 3}
optimizeModel: 1
forceArbitraryBatchSize: 1
treatErrorsAsWarnings: 0
importMode: 1

1001
Project/Assets/ML-Agents/Examples/GridFoodCollector.onnx
文件差异内容过多而无法显示
查看文件

14
Project/Assets/ML-Agents/Examples/GridFoodCollector.onnx.meta


fileFormatVersion: 2
guid: 49be8866460a44d34b25fe7a67208eaa
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 683b6cb6d0a474744822c888b46772c9, type: 3}
optimizeModel: 1
forceArbitraryBatchSize: 1
treatErrorsAsWarnings: 0
importMode: 1

14
Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/FoodCollector.onnx.meta


fileFormatVersion: 2
guid: 75910f45f20be49b18e2b95879a217b2
ScriptedImporter:
fileIDToRecycleName:
11400000: main obj
11400002: model data
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 683b6cb6d0a474744822c888b46772c9, type: 3}
optimizeModel: 1
forceArbitraryBatchSize: 1
treatErrorsAsWarnings: 0

/Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/FoodCollector.onnx → /Project/Assets/ML-Agents/Examples/FoodCollector/TFModels/GridFoodCollector.onnx

正在加载...
取消
保存