Saving Behavior.nn file after training with Python

I’m trying to compare different RL algorithms performance using GitHub - hill-a/stable-baselines: A fork of OpenAI Baselines, implementations of reinforcement learning algorithms (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.

Hi,
When training stops (either you reached max steps or interrupted the trainer) you should see something like

Converting ./models/ppo/3DBall/frozen_graph_def.pb to ./models/ppo/3DBall.nn
...
DONE: wrote ./models/ppo/3DBall.nn file.
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.

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.

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.

1 Like

Thank you a lot! I’ll try that

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)