Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Dynamically loading Onnx file and setting as the model for an Agent

Discussion in 'ML-Agents' started by frieldhelm446, Nov 13, 2022.

  1. frieldhelm446

    frieldhelm446

    Joined:
    Sep 27, 2020
    Posts:
    26
    I have a .onnx file from a trained model that's stored on the application persistentpath. I want to load it at runtime and set it as the model for an Agent. Now, the Agent expects a NNModel in its SetModel method. This is what I've tried

    Code (CSharp):
    1.  
    2. var modelPath = NNHelper.GetOnnxPath(Species, selectedBrain.Brain);
    3. var modelData = File.ReadAllBytes(modelPath);
    4. NNModel model = ScriptableObject.CreateInstance<NNModel>();
    5. model.modelData = ScriptableObject.CreateInstance<NNModelData>();
    6. model.modelData.Value = modelData;
    7. GetComponent<FishAgent>().SetModel("Fishy", model);
    This results in the NotSupportedException: Format version not supported: 242 error since Barracuda doesn't support Onnx files apparently. Online I've read some people have used the ONNXConverter, however, that converts the byte[] to a Model, where a byte[] is required for the modelData.Value.

    Any suggestions?
     
  2. frieldhelm446

    frieldhelm446

    Joined:
    Sep 27, 2020
    Posts:
    26
    Fixed it. If anyone wants to know, take a look at the ONNXModelImporter.