Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Saving Behavior.nn file after training with Python

Discussion in 'ML-Agents' started by OleguerCanal, Feb 2, 2020.

  1. OleguerCanal

    OleguerCanal

    Joined:
    Jul 21, 2015
    Posts:
    7
    I'm trying to compare different RL algorithms performance using https://github.com/hill-a/stable-baselines (which uses tensorflow) training in Python, but I am not sure how to generate the .nn file I can pass to unity once I have the model trained. Any ideas?

    Thanks a lot.
     
  2. celion_unity

    celion_unity

    Joined:
    Jun 12, 2019
    Posts:
    289
    Hi,
    When training stops (either you reached max steps or interrupted the trainer) you should see something like
    Code (csharp):
    1. Converting ./models/ppo/3DBall/frozen_graph_def.pb to ./models/ppo/3DBall.nn
    2. ...
    3. DONE: wrote ./models/ppo/3DBall.nn file.
    4. INFO:mlagents.trainers:Exported ./models/ppo/3DBall.nn file
    in the logs. The actual path and file name will depend on your run ID and behavior name.

    Just copy that .nn file somewhere that Unity can read it.
     
  3. OleguerCanal

    OleguerCanal

    Joined:
    Jul 21, 2015
    Posts:
    7
    Thanks for the answer!
    Not sure I explained myself correctly though: I meant when training importing the environment to Python with custom RL algorithms given for example by the stable-baselines repo. Not when running the provided PPO or other implemented RL algorithms within ML-Agents library.
    Once I have a trained model from stable baselines in python, how can I convert the weights into a .nn file?

    Thank you for your time.
     
  4. Procuste

    Procuste

    Joined:
    Feb 10, 2020
    Posts:
    12
    Take a look to the tensorflow_to_barracuda.py script located in the mlagents/trainers folder. It takes as input a .pb file (a frozen tensorflow graph that you can easily generate) and outputs a .nn file, a Model that you can directly drag and drop in Unity.
     
    OleguerCanal likes this.
  5. OleguerCanal

    OleguerCanal

    Joined:
    Jul 21, 2015
    Posts:
    7
    Thank you a lot! I'll try that
     
  6. dani_kal

    dani_kal

    Joined:
    Mar 25, 2020
    Posts:
    52
    Do you have found the solution? I try to drag and drop the .nn file in Unity editor but an error occurs.

    NullReferenceException: Object reference not set to an instance of an object
    MLAgents.InferenceBrain.BarracudaModelParamLoader.GetIntScalar (System.String name) (at Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs:133)
    MLAgents.InferenceBrain.BarracudaModelParamLoader.GenerateChecks () (at Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs:160)
    MLAgents.InferenceBrain.BarracudaModelParamLoader.GetLoaderAndCheck (Barracuda.IWorker engine, Barracuda.Model model, MLAgents.BrainParameters brainParameters) (at Assets/ML-Agents/Scripts/InferenceBrain/BarracudaModelParamLoader.cs:44)
    MLAgents.LearningBrain.ReloadModel (System.Int32 seed) (at Assets/ML-Agents/Scripts/LearningBrain.cs:106)
    MLAgents.LearningBrainEditor.OnInspectorGUI () (at Assets/ML-Agents/Editor/LearningBrainEditor.cs:60)
    UnityEditor.InspectorWindow.DoOnInspectorGUI (System.Boolean rebuildOptimizedGUIBlock, UnityEditor.Editor editor, System.Boolean wasVisible, UnityEngine.Rect& contentRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1647)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)