mbaske / grid-sensor

Grid Sensor Components for Unity ML-Agents
MIT License
134 stars 28 forks source link

Sensor shapes must match. [20, 20, 1] != [23, 23, 8] . A problem with sensor initialization? #10

Open MarkTension opened 2 years ago

MarkTension commented 2 years ago

Hello, my gridsensor somehow doesn't initialize corerctly.

Sensor shapes must match. [20, 20, 1] != [23, 23, 8]
UnityEngine.Debug:AssertFormat (bool,string,object[])
Unity.MLAgents.Sensors.SensorShapeValidator:ValidateSensors (System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Sensors/SensorShapeValidator.cs:43)
Unity.MLAgents.Inference.ModelRunner:PutObservations (Unity.MLAgents.AgentInfo,System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Inference/ModelRunner.cs:160)
Unity.MLAgents.Policies.BarracudaPolicy:RequestDecision (Unity.MLAgents.AgentInfo,System.Collections.Generic.List`1<Unity.MLAgents.Sensors.ISensor>) (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Policies/BarracudaPolicy.cs:96)
Unity.MLAgents.Agent:SendInfoToBrain () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Agent.cs:1096)
Unity.MLAgents.Agent:SendInfo () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Agent.cs:1323)
Unity.MLAgents.Academy:EnvironmentStep () (at Library/PackageCache/com.unity.ml-agents@2.1.0-exp.1/Runtime/Academy.cs:573)
Environment:FixedUpdate () (at Assets/Assets_rl 1/scripts/Environment/Environment.cs:148

It says the sensor shapes don't match, but I do specify a colorgridbuffer with a shape (23, 23, 8) like so:

customGridSensor.cs

m_SensorBuffer = new ColorGridBuffer(8, 23, 23);
var sensorComp = GetComponent<MBaske.Sensors.Grid.GridSensorComponent>();
sensorComp.GridBuffer = m_SensorBuffer;

I'm not sure where 20,20,1 comes from. When I hard-code the GridSensorComponentBase script, where the (20,20,1) comes from, then it sometimes works. But other times it still gives the same error

GridSensorComponentBase .cs

[SerializeField, HideInInspector]
private GridBuffer.Shape m_GridShape = new GridBuffer.Shape(8, 23, 23);

When it works, all looks good in the debugging visualization. Is there a way to fix this, or did I forget anything? Thanks!

mbaske commented 2 years ago

Hi - This could be a timing issue in combination with the serialized GridBuffer.Shape. I did serialize a default value for it in order to prevent a null reference in the CreateSensors() method. It is invoked by the ml-agents code when the associated agent is enabled - regardless of whether you're in edit or runtime mode. My guess is your sensor is created before you are overriding the sensor buffer at runtime, and therefore it keeps using the serialized value. As a workaround, you can try keeping the agent disabled until you've set the new value. I'm aware this is less than ideal though...

MarkTension commented 2 years ago

Thank you. Keeping the agent disabled didn't work for me. But I hacked an extra line in GridSensorComponent class when (GridBuffer == null), I hardcode it. Not very pretty, but it works

    GridShape = new GridBuffer.Shape(8, 23, 23);