Search Unity

Bug Instantiate Agent with GridSensor

Discussion in 'ML-Agents' started by LexVolkov, Apr 30, 2021.

  1. LexVolkov

    LexVolkov

    Joined:
    Sep 14, 2014
    Posts:
    62
    Hi. I'm trying to runtime add (instantiate) agent (prefabe) to scene. But I get the error:
    NullReferenceException: Object reference not set to an instance of an object
    Unity.MLAgents.Extensions.Sensors.GridSensor.ChannelsToTexture (System.Int32 channelIndex, System.Int32 numChannelsToAdd) (at C:/Users/User/ml-agents/com.unity.ml-agents.extensions/Runtime/Sensors/GridSensor.cs:548)
    Unity.MLAgents.Extensions.Sensors.GridSensor.GetCompressedObservation () (at C:/Users/User/ml-agents/com.unity.ml-agents.extensions/Runtime/Sensors/GridSensor.cs:523)
    Unity.MLAgents.GrpcExtensions.GetObservationProto (Unity.MLAgents.Sensors.ISensor sensor, Unity.MLAgents.Sensors.ObservationWriter observationWriter) (at C:/Users/User/ml-agents/com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs:406)
    Unity.MLAgents.RpcCommunicator.PutObservations (System.String behaviorName, Unity.MLAgents.AgentInfo info, System.Collections.Generic.List`1[T] sensors) (at C:/Users/User/ml-agents/com.unity.ml-agents/Runtime/Communicator/RpcCommunicator.cs:330)
    Unity.MLAgents.Policies.RemotePolicy.RequestDecision (Unity.MLAgents.AgentInfo info, System.Collections.Generic.List`1[T] sensors) (at C:/Users/User/ml-agents/com.unity.ml-agents/Runtime/Policies/RemotePolicy.cs:55)
    Unity.MLAgents.Agent.SendInfoToBrain () (at C:/Users/User/ml-agents/com.unity.ml-agents/Runtime/Agent.cs:1126)
    Unity.MLAgents.Agent.SendInfo () (at C:/Users/User/ml-agents/com.unity.ml-agents/Runtime/Agent.cs:1387)
    Unity.MLAgents.Academy.EnvironmentStep () (at C:/Users/User/ml-agents/com.unity.ml-agents/Runtime/Academy.cs:573)
    Unity.MLAgents.AcademyFixedUpdateStepper.FixedUpdate () (at C:/Users/User/ml-agents/com.unity.ml-agents/Runtime/Academy.cs:43)

    it happens everytime only once for the entire learning game, regardless of the number of agents I'm trying to create. But everything works. But it's still strange. and I don't understand why this is happening.

    CMD: Version information:
    ml-agents: 0.25.0,
    ml-agents-envs: 0.25.0,
    Communicator API: 1.5.0,
    PyTorch: 1.7.1+cu110

    Ver:
    ML Agents 1.9.0-preview
    Unity 2021.1.1f1
     
  2. ruoping_unity

    ruoping_unity

    Unity Technologies

    Joined:
    Jul 10, 2020
    Posts:
    134
    Hi, this looks like something on our end. Some questions to help me track down the issue:
    1. When in the training does this happen? Is it when the training starts/ends/randomly sometime during the training?
    2. Can you share your grid sensor settings?

    FYI, we just release ML-Agents 2.0 in which we moved grid sensor into the main package with some improvements and UI simplification. If you're interested in upgrading you're welcome to try that out.
     
  3. LexVolkov

    LexVolkov

    Joined:
    Sep 14, 2014
    Posts:
    62
    In new version.
    When I set the "Observation Stacks" more than 1, the error appears:
    InvalidCastException: Specified cast is not valid.
    Unity.MLAgents.Sensors.GridSensorComponent.CreateSensors () (at D:/Unity/ml-agents-release_17_branch/com.unity.ml-agents/Runtime/Sensors/GridSensorComponent.cs:237)
    Unity.MLAgents.Agent.InitializeSensors () (at D:/Unity/ml-agents-release_17_branch/com.unity.ml-agents/Runtime/Agent.cs:977)
    Unity.MLAgents.Editor.BehaviorParametersEditor.DisplayFailedModelChecks () (at D:/Unity/ml-agents-release_17_branch/com.unity.ml-agents/Editor/BehaviorParametersEditor.cs:110)
    Unity.MLAgents.Editor.BehaviorParametersEditor.OnInspectorGUI () (at D:/Unity/ml-agents-release_17_branch/com.unity.ml-agents/Editor/BehaviorParametersEditor.cs:81)
    UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <a122028f8c314654ad750c3d9eb1f725>:0)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr, Boolean&)
     
  4. LexVolkov

    LexVolkov

    Joined:
    Sep 14, 2014
    Posts:
    62
    New version it work fine in my project (by create runtime agents)!
     
    Last edited: May 1, 2021
  5. LexVolkov

    LexVolkov

    Joined:
    Sep 14, 2014
    Posts:
    62
    But strange. earlier warning "Couldn't connect to trainer on port 5004 using ..." appeared before start scene, but in new ver, after.
     
    Last edited: May 1, 2021
  6. ruoping_unity

    ruoping_unity

    Unity Technologies

    Joined:
    Jul 10, 2020
    Posts:
    134
    Glad to hear the new version works for you.

    To help us maintain the older version, can you still share the original grid sensor settings for my two question above? That will help us make a patch for users using older versions.

    The error with stack > 1 is on us. I'm working on fixing that.

    The "Couldn't connect to trainer on port 5004 using ..." should always appear after you press the play button. This behavior shouldn't change between version1.x and 2.x. If there's anything weird happening please let us know.
     
  7. LexVolkov

    LexVolkov

    Joined:
    Sep 14, 2014
    Posts:
    62
    is simple.
    take a clean Release 15 ML-Agents.
    Go Example -> FoodCollector -> FoodCollectorSettings -> EnvironmentReset
    and write bellow in the end:
    Code (CSharp):
    1. foreach (var a in agents)
    2.         {
    3.             Instantiate(a,a.transform.parent);
    4.         }
    and look error NullReferenceException.
    it comes out only once at the beginning and does not affect anything. everything works correctly.
     
  8. LexVolkov

    LexVolkov

    Joined:
    Sep 14, 2014
    Posts:
    62
    Sorry. This message appears when there is at least one agent in the scene. And since the new version can easily create an agent from the prefab, I dont need to leave them in scene, and the initialization occurs later. Everything is correct. Thank you.
     
  9. ruoping_unity

    ruoping_unity

    Unity Technologies

    Joined:
    Jul 10, 2020
    Posts:
    134
    Hi,

    The error with more than one stack is now fixed on the main branch. It will be released in the next patch release. Thanks for pointing this out.

    I've also identified the cause of the NullReferenceException with the old GridSensor in extension package. While we're now in favor of the new GridSensor in ML-Agents 2.0, I'll try to fix that when we do a patch release for ML-Agents 1.9.