Skip to content

Commit 4a56460

Browse files
author
Chris Elion
authored
[WIP] ISensor interface and use for visual observations (#2731)
* ISensor and SensorBase * camera and rendertex first pass * use isensors for visual obs * Update gridworld with CameraSensors * compressed obs for reals * Remove AgentInfo.visualObservations * better separation of train and inference sensor calls * compressed obs proto - need CI to generate code * int32 * get proto name right * run protoc locally for new fiels * apply generated proto patch (pyi files were weird) * don't repeat bytes * hook up compressedobs * dont send BrainParameters until there's an AgentInfo * python BrainParameters now needs an AgentInfo to create * remove last (I hope) dependency on camerares * remove CameraResolutions and AgentInfo.visual_observations * update mypy-protobuf version * cleanup todos * python cleanup * more unit test fixes * more unit test fix * camera sensors for VisualFood collector, record demo * SensorComponent * timers, rename Sensor fields, GetName interface * sort sensors, add test * remove empty test * revert ProjectVersion.txt change * fix demo recording * remove AgentParameters cameras and textures * PR feedback, use RenderTexture sensor in GridWorld * update scenes to use Camera sensors * Add comments on future of GenerateSensorData * make SensorComponent and subclasses public * #if DEBUG around checks
1 parent a580b31 commit 4a56460

File tree

78 files changed

+1912
-1238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1912
-1238
lines changed

UnitySDK/Assets/ML-Agents/Editor/AgentEditor.cs

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,9 @@ public override void OnInspectorGUI()
2525
"agentParameters.resetOnDone");
2626
var isOdd = serializedAgent.FindProperty(
2727
"agentParameters.onDemandDecision");
28-
var cameras = serializedAgent.FindProperty(
29-
"agentParameters.agentCameras");
30-
var renderTextures = serializedAgent.FindProperty(
31-
"agentParameters.agentRenderTextures");
3228

3329
EditorGUILayout.PropertyField(brain);
3430

35-
if (cameras.arraySize > 0 && renderTextures.arraySize > 0)
36-
{
37-
EditorGUILayout.HelpBox("Brain visual observations created by first getting all cameras then all render textures.", MessageType.Info);
38-
}
39-
40-
EditorGUILayout.LabelField("Agent Cameras");
41-
for (var i = 0; i < cameras.arraySize; i++)
42-
{
43-
EditorGUILayout.PropertyField(
44-
cameras.GetArrayElementAtIndex(i),
45-
new GUIContent("Camera " + (i + 1) + ": "));
46-
}
47-
48-
EditorGUILayout.BeginHorizontal();
49-
if (GUILayout.Button("Add Camera", EditorStyles.miniButton))
50-
{
51-
cameras.arraySize++;
52-
}
53-
54-
if (GUILayout.Button("Remove Camera", EditorStyles.miniButton))
55-
{
56-
cameras.arraySize--;
57-
}
58-
59-
EditorGUILayout.EndHorizontal();
60-
61-
EditorGUILayout.LabelField("Agent RenderTextures");
62-
for (var i = 0; i < renderTextures.arraySize; i++)
63-
{
64-
EditorGUILayout.PropertyField(
65-
renderTextures.GetArrayElementAtIndex(i),
66-
new GUIContent("RenderTexture " + (i + 1) + ": "));
67-
}
68-
69-
EditorGUILayout.BeginHorizontal();
70-
if (GUILayout.Button("Add RenderTextures", EditorStyles.miniButton))
71-
{
72-
renderTextures.arraySize++;
73-
}
74-
75-
if (GUILayout.Button("Remove RenderTextures", EditorStyles.miniButton))
76-
{
77-
renderTextures.arraySize--;
78-
}
79-
80-
EditorGUILayout.EndHorizontal();
81-
8231

8332
EditorGUILayout.PropertyField(
8433
maxSteps,

UnitySDK/Assets/ML-Agents/Editor/BrainParametersDrawer.cs

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,11 @@ public class BrainParametersDrawer : PropertyDrawer
1313
// The height of a line in the Unity Inspectors
1414
private const float k_LineHeight = 17f;
1515
private const int k_VecObsNumLine = 3;
16-
private const string k_CamResPropName = "cameraResolutions";
1716
private const string k_ActionSizePropName = "vectorActionSize";
1817
private const string k_ActionTypePropName = "vectorActionSpaceType";
1918
private const string k_ActionDescriptionPropName = "vectorActionDescriptions";
2019
private const string k_VecObsPropName = "vectorObservationSize";
2120
private const string k_NumVecObsPropName = "numStackedVectorObservations";
22-
private const string k_CamWidthPropName = "width";
23-
private const string k_CamHeightPropName = "height";
24-
private const string k_CamGrayPropName = "blackAndWhite";
25-
private const int k_DefaultCameraWidth = 84;
26-
private const int k_DefaultCameraHeight = 84;
27-
private const bool k_DefaultCameraGray = false;
2821

2922
/// <inheritdoc />
3023
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
@@ -33,7 +26,6 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
3326
{
3427
return k_LineHeight +
3528
GetHeightDrawVectorObservation() +
36-
GetHeightDrawVisualObservation(property) +
3729
GetHeightDrawVectorAction(property) +
3830
GetHeightDrawVectorActionDescriptions(property);
3931
}
@@ -57,10 +49,6 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5749
DrawVectorObservation(position, property);
5850
position.y += GetHeightDrawVectorObservation();
5951

60-
//Visual Observations
61-
DrawVisualObservations(position, property);
62-
position.y += GetHeightDrawVisualObservation(property);
63-
6452
// Vector Action
6553
DrawVectorAction(position, property);
6654
position.y += GetHeightDrawVectorAction(property);
@@ -111,107 +99,6 @@ private static float GetHeightDrawVectorObservation()
11199
return k_VecObsNumLine * k_LineHeight;
112100
}
113101

114-
/// <summary>
115-
/// Draws the Visual Observations parameters for the Brain Parameters
116-
/// </summary>
117-
/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
118-
/// <param name="property">The SerializedProperty of the BrainParameters
119-
/// to make the custom GUI for.</param>
120-
private static void DrawVisualObservations(Rect position, SerializedProperty property)
121-
{
122-
EditorGUI.LabelField(position, "Visual Observations");
123-
position.y += k_LineHeight;
124-
var quarter = position.width / 4;
125-
var resolutions = property.FindPropertyRelative(k_CamResPropName);
126-
DrawVisualObsButtons(position, resolutions);
127-
position.y += k_LineHeight;
128-
129-
// Display the labels for the columns : Index, Width, Height and Gray
130-
var indexRect = new Rect(position.x, position.y, quarter, position.height);
131-
var widthRect = new Rect(position.x + quarter, position.y, quarter, position.height);
132-
var heightRect = new Rect(position.x + 2 * quarter, position.y, quarter, position.height);
133-
var bwRect = new Rect(position.x + 3 * quarter, position.y, quarter, position.height);
134-
EditorGUI.indentLevel++;
135-
if (resolutions.arraySize > 0)
136-
{
137-
EditorGUI.LabelField(indexRect, "Index");
138-
indexRect.y += k_LineHeight;
139-
EditorGUI.LabelField(widthRect, "Width");
140-
widthRect.y += k_LineHeight;
141-
EditorGUI.LabelField(heightRect, "Height");
142-
heightRect.y += k_LineHeight;
143-
EditorGUI.LabelField(bwRect, "Gray");
144-
bwRect.y += k_LineHeight;
145-
}
146-
147-
// Iterate over the resolutions
148-
for (var i = 0; i < resolutions.arraySize; i++)
149-
{
150-
EditorGUI.LabelField(indexRect, "Obs " + i);
151-
indexRect.y += k_LineHeight;
152-
var res = resolutions.GetArrayElementAtIndex(i);
153-
var w = res.FindPropertyRelative("width");
154-
w.intValue = EditorGUI.IntField(widthRect, w.intValue);
155-
widthRect.y += k_LineHeight;
156-
var h = res.FindPropertyRelative("height");
157-
h.intValue = EditorGUI.IntField(heightRect, h.intValue);
158-
heightRect.y += k_LineHeight;
159-
var bw = res.FindPropertyRelative("blackAndWhite");
160-
bw.boolValue = EditorGUI.Toggle(bwRect, bw.boolValue);
161-
bwRect.y += k_LineHeight;
162-
}
163-
EditorGUI.indentLevel--;
164-
}
165-
166-
/// <summary>
167-
/// Draws the buttons to add and remove the visual observations parameters
168-
/// </summary>
169-
/// <param name="position">Rectangle on the screen to use for the property GUI.</param>
170-
/// <param name="resolutions">The SerializedProperty of the resolution array
171-
/// to make the custom GUI for.</param>
172-
private static void DrawVisualObsButtons(Rect position, SerializedProperty resolutions)
173-
{
174-
var widthEighth = position.width / 8;
175-
var addButtonRect = new Rect(position.x + widthEighth, position.y,
176-
3 * widthEighth, position.height);
177-
var removeButtonRect = new Rect(position.x + 4 * widthEighth, position.y,
178-
3 * widthEighth, position.height);
179-
if (resolutions.arraySize == 0)
180-
{
181-
addButtonRect.width *= 2;
182-
}
183-
// Display the buttons
184-
if (GUI.Button(addButtonRect, "Add New", EditorStyles.miniButton))
185-
{
186-
resolutions.arraySize += 1;
187-
var newRes = resolutions.GetArrayElementAtIndex(resolutions.arraySize - 1);
188-
newRes.FindPropertyRelative(k_CamWidthPropName).intValue = k_DefaultCameraWidth;
189-
newRes.FindPropertyRelative(k_CamHeightPropName).intValue = k_DefaultCameraHeight;
190-
newRes.FindPropertyRelative(k_CamGrayPropName).boolValue = k_DefaultCameraGray;
191-
}
192-
if (resolutions.arraySize > 0)
193-
{
194-
if (GUI.Button(removeButtonRect, "Remove Last", EditorStyles.miniButton))
195-
{
196-
resolutions.arraySize -= 1;
197-
}
198-
}
199-
}
200-
201-
/// <summary>
202-
/// The Height required to draw the Visual Observations parameters
203-
/// </summary>
204-
/// <returns>The height of the drawer of the Visual Observations </returns>
205-
private static float GetHeightDrawVisualObservation(SerializedProperty property)
206-
{
207-
var visObsSize = property.FindPropertyRelative(k_CamResPropName).arraySize + 2;
208-
if (property.FindPropertyRelative(k_CamResPropName).arraySize > 0)
209-
{
210-
visObsSize += 1;
211-
}
212-
return k_LineHeight * visObsSize;
213-
}
214-
215102
/// <summary>
216103
/// Draws the Vector Actions parameters for the Brain Parameters
217104
/// </summary>

UnitySDK/Assets/ML-Agents/Editor/DemonstrationDrawer.cs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,6 @@ static string BuildActionArrayLabel(SerializedProperty actionSizeProperty)
5959
return actionLabel.ToString();
6060
}
6161

62-
/// <summary>
63-
/// Constructs complex label for each CameraResolution object.
64-
/// An example of this could be `[ 84 X 84 ]`
65-
/// for a single camera with 84 pixels height and width.
66-
/// </summary>
67-
private static string BuildCameraResolutionLabel(SerializedProperty cameraArray)
68-
{
69-
var numCameras = cameraArray.arraySize;
70-
var cameraLabel = new StringBuilder("[ ");
71-
for (var i = 0; i < numCameras; i++)
72-
{
73-
var camHeightPropName =
74-
cameraArray.GetArrayElementAtIndex(i).FindPropertyRelative("height");
75-
cameraLabel.Append(camHeightPropName.intValue);
76-
cameraLabel.Append(" X ");
77-
var camWidthPropName =
78-
cameraArray.GetArrayElementAtIndex(i).FindPropertyRelative("width");
79-
cameraLabel.Append(camWidthPropName.intValue);
80-
if (i < numCameras - 1)
81-
{
82-
cameraLabel.Append(", ");
83-
}
84-
}
85-
86-
cameraLabel.Append(" ]");
87-
return cameraLabel.ToString();
88-
}
8962

9063
/// <summary>
9164
/// Renders Inspector UI for Brain Parameters of Demonstration.
@@ -95,21 +68,18 @@ void MakeBrainParametersProperty(SerializedProperty property)
9568
var vecObsSizeProp = property.FindPropertyRelative("vectorObservationSize");
9669
var numStackedProp = property.FindPropertyRelative("numStackedVectorObservations");
9770
var actSizeProperty = property.FindPropertyRelative("vectorActionSize");
98-
var camResProp = property.FindPropertyRelative("cameraResolutions");
9971
var actSpaceTypeProp = property.FindPropertyRelative("vectorActionSpaceType");
10072

10173
var vecObsSizeLabel = vecObsSizeProp.displayName + ": " + vecObsSizeProp.intValue;
10274
var numStackedLabel = numStackedProp.displayName + ": " + numStackedProp.intValue;
10375
var vecActSizeLabel =
10476
actSizeProperty.displayName + ": " + BuildActionArrayLabel(actSizeProperty);
105-
var camResLabel = camResProp.displayName + ": " + BuildCameraResolutionLabel(camResProp);
10677
var actSpaceTypeLabel = actSpaceTypeProp.displayName + ": " +
10778
(SpaceType)actSpaceTypeProp.enumValueIndex;
10879

10980
EditorGUILayout.LabelField(vecObsSizeLabel);
11081
EditorGUILayout.LabelField(numStackedLabel);
11182
EditorGUILayout.LabelField(vecActSizeLabel);
112-
EditorGUILayout.LabelField(camResLabel);
11383
EditorGUILayout.LabelField(actSpaceTypeLabel);
11484
}
11585

UnitySDK/Assets/ML-Agents/Editor/Tests/DemonstrationTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public void TestStoreInitalize()
3333
{
3434
vectorObservationSize = 3,
3535
numStackedVectorObservations = 2,
36-
cameraResolutions = new[] {new Resolution()},
3736
vectorActionDescriptions = new[] {"TestActionA", "TestActionB"},
3837
vectorActionSize = new[] {2, 2},
3938
vectorActionSpaceType = SpaceType.Discrete
@@ -47,7 +46,6 @@ public void TestStoreInitalize()
4746
var agentInfo = new AgentInfo
4847
{
4948
reward = 1f,
50-
visualObservations = new List<Texture2D>(),
5149
actionMasks = new[] {false, true},
5250
done = true,
5351
id = 5,

UnitySDK/Assets/ML-Agents/Editor/Tests/MLAgentsEditModeTest.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using UnityEngine;
22
using NUnit.Framework;
33
using System.Reflection;
4+
using MLAgents.Sensor;
5+
using MLAgents.InferenceBrain;
46

57
namespace MLAgents.Tests
68
{
@@ -33,6 +35,13 @@ public class TestAgent : Agent
3335
public override void InitializeAgent()
3436
{
3537
initializeAgentCalls += 1;
38+
39+
// Add in some custom sensors so we can confirm they get sorted as expected.
40+
var sensor1 = new TestSensor("testsensor1");
41+
var sensor2 = new TestSensor("testsensor2");
42+
43+
m_Sensors.Add(sensor2);
44+
m_Sensors.Add(sensor1);
3645
}
3746

3847
public override void CollectObservations()
@@ -57,6 +66,37 @@ public override void AgentOnDone()
5766
}
5867
}
5968

69+
public class TestSensor : ISensor
70+
{
71+
public string sensorName;
72+
73+
public TestSensor(string n)
74+
{
75+
sensorName = n;
76+
}
77+
78+
public int[] GetFloatObservationShape() {
79+
return new[] {1};
80+
}
81+
82+
public void WriteToTensor(TensorProxy tensorProxy, int agentIndex) { }
83+
84+
public byte[] GetCompressedObservation()
85+
{
86+
return null;
87+
}
88+
89+
public CompressionType GetCompressionType()
90+
{
91+
return CompressionType.None;
92+
}
93+
94+
public string GetName()
95+
{
96+
return sensorName;
97+
}
98+
}
99+
60100
// This is an empty class for testing the behavior of agents and academy
61101
// It is left empty because we are not testing any brain behavior
62102
public class TestBrain : Brain
@@ -177,6 +217,10 @@ public void TestAgent()
177217
Assert.AreEqual(1, agent2.initializeAgentCalls);
178218
Assert.AreEqual(0, agent1.agentActionCalls);
179219
Assert.AreEqual(0, agent2.agentActionCalls);
220+
221+
// Make sure the sensors were sorted
222+
Assert.AreEqual(agent1.m_Sensors[0].GetName(), "testsensor1");
223+
Assert.AreEqual(agent1.m_Sensors[1].GetName(), "testsensor2");
180224
}
181225
}
182226

@@ -244,7 +288,6 @@ public void TestAgent()
244288
agent2.agentParameters.onDemandDecision = true;
245289
// agent2 will request decisions only when RequestDecision is called
246290
brain.brainParameters.vectorObservationSize = 0;
247-
brain.brainParameters.cameraResolutions = new Resolution[0];
248291
agent1.GiveBrain(brain);
249292
agent2.GiveBrain(brain);
250293

@@ -370,7 +413,6 @@ public void TestAgent()
370413
agent2.agentParameters.onDemandDecision = true;
371414
// agent2 will request decisions only when RequestDecision is called
372415
brain.brainParameters.vectorObservationSize = 0;
373-
brain.brainParameters.cameraResolutions = new Resolution[0];
374416
agent1.GiveBrain(brain);
375417
agent2.GiveBrain(brain);
376418

@@ -487,7 +529,6 @@ public void TestResetOnDone()
487529
agent1.agentParameters.resetOnDone = false;
488530
agent2.agentParameters.resetOnDone = false;
489531
brain.brainParameters.vectorObservationSize = 0;
490-
brain.brainParameters.cameraResolutions = new Resolution[0];
491532
agent1.GiveBrain(brain);
492533
agent2.GiveBrain(brain);
493534

@@ -567,7 +608,6 @@ public void TestCumulativeReward()
567608
// agent2 will request decisions only when RequestDecision is called
568609
agent1.agentParameters.maxStep = 20;
569610
brain.brainParameters.vectorObservationSize = 0;
570-
brain.brainParameters.cameraResolutions = new Resolution[0];
571611
agent1.GiveBrain(brain);
572612
agent2.GiveBrain(brain);
573613

0 commit comments

Comments
 (0)