Skip to content

Add enum for sensor implementations, send in analytics #4871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public enum Match3ObservationType
/// or uncompressed visual observations. Uses AbstractBoard.GetCellType()
/// and AbstractBoard.GetSpecialType() to determine the observation values.
/// </summary>
public class Match3Sensor : ISparseChannelSensor
public class Match3Sensor : ISparseChannelSensor, IBuiltInSensor
{
private Match3ObservationType m_ObservationType;
private AbstractBoard m_Board;
Expand Down Expand Up @@ -234,6 +234,12 @@ public int[] GetCompressedChannelMapping()
return m_SparseChannelMapping;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.Match3Sensor;
}

static void DestroyTexture(Texture2D texture)
{
if (Application.isEditor)
Expand Down
9 changes: 8 additions & 1 deletion com.unity.ml-agents.extensions/Runtime/Sensors/GridSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Unity.MLAgents.Extensions.Sensors
/// <summary>
/// Grid-based sensor.
/// </summary>
public class GridSensor : SensorComponent, ISensor
public class GridSensor : SensorComponent, ISensor, IBuiltInSensor
{
/// <summary>
/// Name of this grid sensor.
Expand Down Expand Up @@ -474,6 +474,13 @@ public virtual SensorCompressionType GetCompressionType()
return CompressionType;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.GridSensor;
}


/// <summary>
/// GetCompressedObservation - Calls Perceive then puts the data stored on the perception buffer
/// onto the m_perceptionTexture2D to be converted to a byte array and returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Unity.MLAgents.Extensions.Sensors
/// <summary>
/// ISensor implementation that generates observations for a group of Rigidbodies or ArticulationBodies.
/// </summary>
public class PhysicsBodySensor : ISensor
public class PhysicsBodySensor : ISensor, IBuiltInSensor
{
int[] m_Shape;
string m_SensorName;
Expand Down Expand Up @@ -120,5 +120,12 @@ public string GetName()
{
return m_SensorName;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.PhysicsBodySensor;
}

}
}
5 changes: 5 additions & 0 deletions com.unity.ml-agents/Runtime/Analytics/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal struct EventObservationSpec
{
public string SensorName;
public string CompressionType;
public int BuiltInSensorType;
public EventObservationDimensionInfo[] DimensionInfos;

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

var builtInSensorType =
(sensor as IBuiltInSensor)?.GetBuiltInSensorType() ?? Sensors.BuiltInSensorType.Unknown;

return new EventObservationSpec
{
SensorName = sensor.GetName(),
CompressionType = sensor.GetCompressionType().ToString(),
BuiltInSensorType = (int)builtInSensorType,
DimensionInfos = dimInfos,
};
}
Expand Down
8 changes: 5 additions & 3 deletions com.unity.ml-agents/Runtime/Analytics/InferenceAnalytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal class InferenceAnalytics
{
const string k_VendorKey = "unity.ml-agents";
const string k_EventName = "ml_agents_inferencemodelset";
const int k_EventVersion = 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need to use version 2 here, waiting to hear back from analytics team.


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


/// <summary>
/// Models that we've already sent events for.
/// </summary>
Expand All @@ -49,7 +51,7 @@ static bool EnableAnalytics()
}

#if UNITY_EDITOR
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey);
AnalyticsResult result = EditorAnalytics.RegisterEventWithLimit(k_EventName, k_MaxEventsPerHour, k_MaxNumberOfElements, k_VendorKey, k_EventVersion);
#else
AnalyticsResult result = AnalyticsResult.UnsupportedPlatform;
#endif
Expand Down Expand Up @@ -112,9 +114,9 @@ ActionSpec actionSpec

var data = GetEventForModel(nnModel, behaviorName, inferenceDevice, sensors, actionSpec);
// Note - to debug, use JsonUtility.ToJson on the event.
// Debug.Log(JsonUtility.ToJson(data, true));
//Debug.Log(JsonUtility.ToJson(data, true));
#if UNITY_EDITOR
EditorAnalytics.SendEventWithLimit(k_EventName, data);
EditorAnalytics.SendEventWithLimit(k_EventName, data, k_EventVersion);
#else
return;
#endif
Expand Down
8 changes: 7 additions & 1 deletion com.unity.ml-agents/Runtime/Sensors/BufferSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Unity.MLAgents.Sensors
{
internal class BufferSensor : ISensor, IDimensionPropertiesSensor
internal class BufferSensor : ISensor, IDimensionPropertiesSensor, IBuiltInSensor
{
private int m_MaxNumObs;
private int m_ObsSize;
Expand Down Expand Up @@ -90,6 +90,12 @@ public string GetName()
return "BufferSensor";
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.BufferSensor;
}

}

}
9 changes: 8 additions & 1 deletion com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Unity.MLAgents.Sensors
/// <summary>
/// A sensor that wraps a Camera object to generate visual observations for an agent.
/// </summary>
public class CameraSensor : ISensor
public class CameraSensor : ISensor, IBuiltInSensor
{
Camera m_Camera;
int m_Width;
Expand Down Expand Up @@ -188,5 +188,12 @@ static void DestroyTexture(Texture2D texture)
Object.Destroy(texture);
}
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.CameraSensor;
}

}
}
39 changes: 39 additions & 0 deletions com.unity.ml-agents/Runtime/Sensors/IBuiltInSensor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace Unity.MLAgents.Sensors
{
/// <summary>
/// Identifiers for "built in" sensor types.
/// These are only used for analytics, and should not be used for any runtime decisions.
///
/// NOTE: Do not renumber these, since the values are used for analytics. Renaming is allowed though.
/// </summary>
public enum BuiltInSensorType
{
Unknown = 0,
VectorSensor = 1,
// Note that StackingSensor actually returns the wrapped sensor's type
StackingSensor = 2,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can omit this if you don't like it here (since it's not actually used).

RayPerceptionSensor = 3,
ReflectionSensor = 4,
CameraSensor = 5,
RenderTextureSensor = 6,
BufferSensor = 7,
PhysicsBodySensor = 8,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordering doesn't actually matter here, but I could make the extensions implementations start at some offset, say 1000.

Match3Sensor = 9,
GridSensor = 10
}

/// <summary>
/// Interface for sensors that are provided as part of ML-Agents.
/// User-implemented sensors don't need to use this interface.
/// </summary>
public interface IBuiltInSensor
{
/// <summary>
/// Return the corresponding BuiltInSensorType for the sensor.
/// </summary>
/// <returns>A BuiltInSensorType corresponding to the sensor.</returns>
BuiltInSensorType GetBuiltInSensorType();
}


}
3 changes: 3 additions & 0 deletions com.unity.ml-agents/Runtime/Sensors/IBuiltInSensor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion com.unity.ml-agents/Runtime/Sensors/RayPerceptionSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public int age
/// <summary>
/// A sensor implementation that supports ray cast-based observations.
/// </summary>
public class RayPerceptionSensor : ISensor
public class RayPerceptionSensor : ISensor, IBuiltInSensor
{
float[] m_Observations;
int[] m_Shape;
Expand Down Expand Up @@ -366,6 +366,12 @@ public virtual SensorCompressionType GetCompressionType()
return SensorCompressionType.None;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.RayPerceptionSensor;
}

/// <summary>
/// Evaluates the raycasts to be used as part of an observation of an agent.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Type GetMemberType()
/// <summary>
/// Abstract base class for reflection-based sensors.
/// </summary>
internal abstract class ReflectionSensorBase : ISensor
internal abstract class ReflectionSensorBase : ISensor, IBuiltInSensor
{
protected object m_Object;

Expand Down Expand Up @@ -99,5 +99,12 @@ public string GetName()
{
return m_SensorName;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.ReflectionSensor;
}

}
}
9 changes: 8 additions & 1 deletion com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Unity.MLAgents.Sensors
/// <summary>
/// Sensor class that wraps a [RenderTexture](https://docs.unity3d.com/ScriptReference/RenderTexture.html) instance.
/// </summary>
public class RenderTextureSensor : ISensor
public class RenderTextureSensor : ISensor, IBuiltInSensor
{
RenderTexture m_RenderTexture;
bool m_Grayscale;
Expand Down Expand Up @@ -93,6 +93,13 @@ public SensorCompressionType GetCompressionType()
return m_CompressionType;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.RenderTextureSensor;
}


/// <summary>
/// Converts a RenderTexture to a 2D texture.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion com.unity.ml-agents/Runtime/Sensors/StackingSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Unity.MLAgents.Sensors
/// Internally, a circular buffer of arrays is used. The m_CurrentIndex represents the most recent observation.
/// Currently, observations are stacked on the last dimension.
/// </summary>
public class StackingSensor : ISparseChannelSensor
public class StackingSensor : ISparseChannelSensor, IBuiltInSensor
{
/// <summary>
/// The wrapped sensor.
Expand Down Expand Up @@ -282,5 +282,12 @@ internal int[] ConstructStackedCompressedChannelMapping(ISensor wrappedSenesor)
}
return compressionMapping;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
IBuiltInSensor wrappedBuiltInSensor = m_WrappedSensor as IBuiltInSensor;
return wrappedBuiltInSensor?.GetBuiltInSensorType() ?? BuiltInSensorType.Unknown;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO add unit test that checks this explicitly.

}
}
}
8 changes: 7 additions & 1 deletion com.unity.ml-agents/Runtime/Sensors/VectorSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Unity.MLAgents.Sensors
/// <summary>
/// A sensor implementation for vector observations.
/// </summary>
public class VectorSensor : ISensor
public class VectorSensor : ISensor, IBuiltInSensor
{
// TODO use float[] instead
// TODO allow setting float[]
Expand Down Expand Up @@ -106,6 +106,12 @@ public virtual SensorCompressionType GetCompressionType()
return SensorCompressionType.None;
}

/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.VectorSensor;
}

void Clear()
{
m_Observations.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void TestModelEvent()
Assert.AreEqual(3, continuousEvent.ObservationSpecs[0].DimensionInfos.Length);
Assert.AreEqual(20, continuousEvent.ObservationSpecs[0].DimensionInfos[0].Size);
Assert.AreEqual("None", continuousEvent.ObservationSpecs[0].CompressionType);
Assert.AreEqual(Test3DSensor.k_BuiltInSensorType, continuousEvent.ObservationSpecs[0].BuiltInSensorType);
Assert.AreNotEqual(null, continuousEvent.ModelHash);

// Make sure nested fields get serialized
Expand Down
9 changes: 8 additions & 1 deletion com.unity.ml-agents/Tests/Editor/ParameterLoaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public override int[] GetObservationShape()
return Sensor.GetObservationShape();
}
}
public class Test3DSensor : ISensor
public class Test3DSensor : ISensor, IBuiltInSensor
{
int m_Width;
int m_Height;
int m_Channels;
string m_Name;
// Dummy value for the IBuiltInSensor interface
public const int k_BuiltInSensorType = -42;

public Test3DSensor(string name, int width, int height, int channels)
{
Expand Down Expand Up @@ -70,6 +72,11 @@ public string GetName()
{
return m_Name;
}

public BuiltInSensorType GetBuiltInSensorType()
{
return (BuiltInSensorType)k_BuiltInSensorType;
}
}

[TestFixture]
Expand Down
15 changes: 15 additions & 0 deletions com.unity.ml-agents/Tests/Editor/Sensor/StackingSensorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public int[] GetCompressedChannelMapping()
{
return Mapping;
}

}

[Test]
Expand Down Expand Up @@ -212,5 +213,19 @@ public void TestStackedGetCompressedObservation()
expected4 = expected4.Concat(Array.ConvertAll(new[] { 10f, 11f, 12f }, (z) => (byte)z)).ToArray();
Assert.AreEqual(sensor.GetCompressedObservation(), expected4);
}

[Test]
public void TestStackingSensorBuiltInSensorType()
{
var dummySensor = new Dummy3DSensor();
dummySensor.Shape = new[] { 2, 2, 4 };
dummySensor.Mapping = new[] { 0, 1, 2, 3 };
var stackedDummySensor = new StackingSensor(dummySensor, 2);
Assert.AreEqual(stackedDummySensor.GetBuiltInSensorType(), BuiltInSensorType.Unknown);

var vectorSensor = new VectorSensor(4);
var stackedVectorSensor = new StackingSensor(vectorSensor, 4);
Assert.AreEqual(stackedVectorSensor.GetBuiltInSensorType(), BuiltInSensorType.VectorSensor);
}
}
}