Skip to content

Commit 56898ab

Browse files
author
Chris Elion
authored
Add enum for sensor implementations, send in analytics (#4871)
1 parent 0e573a1 commit 56898ab

File tree

17 files changed

+152
-14
lines changed

17 files changed

+152
-14
lines changed

com.unity.ml-agents.extensions/Runtime/Match3/Match3Sensor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public enum Match3ObservationType
3535
/// or uncompressed visual observations. Uses AbstractBoard.GetCellType()
3636
/// and AbstractBoard.GetSpecialType() to determine the observation values.
3737
/// </summary>
38-
public class Match3Sensor : ISparseChannelSensor
38+
public class Match3Sensor : ISparseChannelSensor, IBuiltInSensor
3939
{
4040
private Match3ObservationType m_ObservationType;
4141
private AbstractBoard m_Board;
@@ -234,6 +234,12 @@ public int[] GetCompressedChannelMapping()
234234
return m_SparseChannelMapping;
235235
}
236236

237+
/// <inheritdoc/>
238+
public BuiltInSensorType GetBuiltInSensorType()
239+
{
240+
return BuiltInSensorType.Match3Sensor;
241+
}
242+
237243
static void DestroyTexture(Texture2D texture)
238244
{
239245
if (Application.isEditor)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Unity.MLAgents.Extensions.Sensors
99
/// <summary>
1010
/// Grid-based sensor.
1111
/// </summary>
12-
public class GridSensor : SensorComponent, ISensor
12+
public class GridSensor : SensorComponent, ISensor, IBuiltInSensor
1313
{
1414
/// <summary>
1515
/// Name of this grid sensor.
@@ -474,6 +474,13 @@ public virtual SensorCompressionType GetCompressionType()
474474
return CompressionType;
475475
}
476476

477+
/// <inheritdoc/>
478+
public BuiltInSensorType GetBuiltInSensorType()
479+
{
480+
return BuiltInSensorType.GridSensor;
481+
}
482+
483+
477484
/// <summary>
478485
/// GetCompressedObservation - Calls Perceive then puts the data stored on the perception buffer
479486
/// onto the m_perceptionTexture2D to be converted to a byte array and returned

com.unity.ml-agents.extensions/Runtime/Sensors/PhysicsBodySensor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Unity.MLAgents.Extensions.Sensors
99
/// <summary>
1010
/// ISensor implementation that generates observations for a group of Rigidbodies or ArticulationBodies.
1111
/// </summary>
12-
public class PhysicsBodySensor : ISensor
12+
public class PhysicsBodySensor : ISensor, IBuiltInSensor
1313
{
1414
int[] m_Shape;
1515
string m_SensorName;
@@ -120,5 +120,12 @@ public string GetName()
120120
{
121121
return m_SensorName;
122122
}
123+
124+
/// <inheritdoc/>
125+
public BuiltInSensorType GetBuiltInSensorType()
126+
{
127+
return BuiltInSensorType.PhysicsBodySensor;
128+
}
129+
123130
}
124131
}

com.unity.ml-agents/Runtime/Analytics/Events.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ internal struct EventObservationSpec
6666
{
6767
public string SensorName;
6868
public string CompressionType;
69+
public int BuiltInSensorType;
6970
public EventObservationDimensionInfo[] DimensionInfos;
7071

7172
public static EventObservationSpec FromSensor(ISensor sensor)
@@ -78,10 +79,14 @@ public static EventObservationSpec FromSensor(ISensor sensor)
7879
// TODO copy flags when we have them
7980
}
8081

82+
var builtInSensorType =
83+
(sensor as IBuiltInSensor)?.GetBuiltInSensorType() ?? Sensors.BuiltInSensorType.Unknown;
84+
8185
return new EventObservationSpec
8286
{
8387
SensorName = sensor.GetName(),
8488
CompressionType = sensor.GetCompressionType().ToString(),
89+
BuiltInSensorType = (int)builtInSensorType,
8590
DimensionInfos = dimInfos,
8691
};
8792
}

com.unity.ml-agents/Runtime/Analytics/InferenceAnalytics.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal class InferenceAnalytics
2020
{
2121
const string k_VendorKey = "unity.ml-agents";
2222
const string k_EventName = "ml_agents_inferencemodelset";
23+
const int k_EventVersion = 1;
2324

2425
/// <summary>
2526
/// Whether or not we've registered this particular event yet
@@ -36,6 +37,7 @@ internal class InferenceAnalytics
3637
/// </summary>
3738
const int k_MaxNumberOfElements = 1000;
3839

40+
3941
/// <summary>
4042
/// Models that we've already sent events for.
4143
/// </summary>
@@ -49,7 +51,7 @@ static bool EnableAnalytics()
4951
}
5052

5153
#if UNITY_EDITOR
52-
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
54+
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey, k_EventVersion);
5355
#else
5456
AnalyticsResult result = AnalyticsResult.UnsupportedPlatform;
5557
#endif
@@ -112,9 +114,9 @@ ActionSpec actionSpec
112114

113115
var data = GetEventForModel(nnModel, behaviorName, inferenceDevice, sensors, actionSpec);
114116
// Note - to debug, use JsonUtility.ToJson on the event.
115-
// Debug.Log(JsonUtility.ToJson(data, true));
117+
//Debug.Log(JsonUtility.ToJson(data, true));
116118
#if UNITY_EDITOR
117-
EditorAnalytics.SendEventWithLimit(k_EventName, data);
119+
EditorAnalytics.SendEventWithLimit(k_EventName, data, k_EventVersion);
118120
#else
119121
return;
120122
#endif

com.unity.ml-agents/Runtime/Sensors/BufferSensor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Unity.MLAgents.Sensors
44
{
5-
internal class BufferSensor : ISensor, IDimensionPropertiesSensor
5+
internal class BufferSensor : ISensor, IDimensionPropertiesSensor, IBuiltInSensor
66
{
77
private int m_MaxNumObs;
88
private int m_ObsSize;
@@ -90,6 +90,12 @@ public string GetName()
9090
return "BufferSensor";
9191
}
9292

93+
/// <inheritdoc/>
94+
public BuiltInSensorType GetBuiltInSensorType()
95+
{
96+
return BuiltInSensorType.BufferSensor;
97+
}
98+
9399
}
94100

95101
}

com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Unity.MLAgents.Sensors
66
/// <summary>
77
/// A sensor that wraps a Camera object to generate visual observations for an agent.
88
/// </summary>
9-
public class CameraSensor : ISensor
9+
public class CameraSensor : ISensor, IBuiltInSensor
1010
{
1111
Camera m_Camera;
1212
int m_Width;
@@ -188,5 +188,12 @@ static void DestroyTexture(Texture2D texture)
188188
Object.Destroy(texture);
189189
}
190190
}
191+
192+
/// <inheritdoc/>
193+
public BuiltInSensorType GetBuiltInSensorType()
194+
{
195+
return BuiltInSensorType.CameraSensor;
196+
}
197+
191198
}
192199
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace Unity.MLAgents.Sensors
2+
{
3+
/// <summary>
4+
/// Identifiers for "built in" sensor types.
5+
/// These are only used for analytics, and should not be used for any runtime decisions.
6+
///
7+
/// NOTE: Do not renumber these, since the values are used for analytics. Renaming is allowed though.
8+
/// </summary>
9+
public enum BuiltInSensorType
10+
{
11+
Unknown = 0,
12+
VectorSensor = 1,
13+
// Note that StackingSensor actually returns the wrapped sensor's type
14+
StackingSensor = 2,
15+
RayPerceptionSensor = 3,
16+
ReflectionSensor = 4,
17+
CameraSensor = 5,
18+
RenderTextureSensor = 6,
19+
BufferSensor = 7,
20+
PhysicsBodySensor = 8,
21+
Match3Sensor = 9,
22+
GridSensor = 10
23+
}
24+
25+
/// <summary>
26+
/// Interface for sensors that are provided as part of ML-Agents.
27+
/// User-implemented sensors don't need to use this interface.
28+
/// </summary>
29+
public interface IBuiltInSensor
30+
{
31+
/// <summary>
32+
/// Return the corresponding BuiltInSensorType for the sensor.
33+
/// </summary>
34+
/// <returns>A BuiltInSensorType corresponding to the sensor.</returns>
35+
BuiltInSensorType GetBuiltInSensorType();
36+
}
37+
38+
39+
}

com.unity.ml-agents/Runtime/Sensors/IBuiltInSensor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public int age
234234
/// <summary>
235235
/// A sensor implementation that supports ray cast-based observations.
236236
/// </summary>
237-
public class RayPerceptionSensor : ISensor
237+
public class RayPerceptionSensor : ISensor, IBuiltInSensor
238238
{
239239
float[] m_Observations;
240240
int[] m_Shape;
@@ -366,6 +366,12 @@ public virtual SensorCompressionType GetCompressionType()
366366
return SensorCompressionType.None;
367367
}
368368

369+
/// <inheritdoc/>
370+
public BuiltInSensorType GetBuiltInSensorType()
371+
{
372+
return BuiltInSensorType.RayPerceptionSensor;
373+
}
374+
369375
/// <summary>
370376
/// Evaluates the raycasts to be used as part of an observation of an agent.
371377
/// </summary>

0 commit comments

Comments
 (0)