Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

"the external ONNX file data" to NNModel Instance

Discussion in 'ML-Agents' started by gichan_unity, Sep 7, 2021.

  1. gichan_unity

    gichan_unity

    Joined:
    Sep 7, 2021
    Posts:
    4
    I would like to read the external ONNX file of ML-Agent and import it during runtime.

    The code I wrote is as follows.

    upload_2021-9-7_12-0-12.png

    save me.

    Please....
     
    Last edited: Sep 10, 2021
  2. gichan_unity

    gichan_unity

    Joined:
    Sep 7, 2021
    Posts:
    4
    upload_2021-9-8_15-48-4.png
    this is error text
     
  3. gft-ai

    gft-ai

    Joined:
    Jan 12, 2021
    Posts:
    44
  4. gichan_unity

    gichan_unity

    Joined:
    Sep 7, 2021
    Posts:
    4
    I'll check. Thank you for your interest.
     
  5. gichan_unity

    gichan_unity

    Joined:
    Sep 7, 2021
    Posts:
    4
  6. gft-ai

    gft-ai

    Joined:
    Jan 12, 2021
    Posts:
    44
    I don’t quite understand what you mean?
    Maybe it might help if you explained what you have done and what you want to achieve?

    Where is this .onnx model from? Did you train a model using mlagents or did you train a model somewhere else and converted into onnx and you want to load it to unity? Need more context! :)
     
  7. unity_72548131E1D9F001A288

    unity_72548131E1D9F001A288

    Joined:
    Oct 30, 2021
    Posts:
    1
    Having the same issue with a build. The model is trained using ML-Agents but it is not in the Projects Asset directory. It's being loaded from a completely different directory. Not sure why but unless the NNModel is in the asset database, which isn't an option for a build, it will throw the error 244.
     
  8. IkigaiMonkey

    IkigaiMonkey

    Joined:
    Sep 22, 2012
    Posts:
    18
    Hey guys, did anyone solve this?

    We need to load models that we download in runtime from a webserver, and we execute it in a build (not in Editor), is it possible?
    All the examples and code I find only work within Editor using the Resources Folder.
     
  9. maxgrafqmul

    maxgrafqmul

    Joined:
    Oct 5, 2020
    Posts:
    3
    Any updates on this?
     
  10. maxgrafqmul

    maxgrafqmul

    Joined:
    Oct 5, 2020
    Posts:
    3
    Edit: figured it out. For future people wanting to dynamically load ONNX model files from disk without relying on Resources or Assets:

    Code (CSharp):
    1. var ONNXPath = "/my/onnx/path/file.onnx";
    2. var converter = new ONNXModelConverter(optimizeModel: true); // requires the Unity.Barracuda.ONNX assembly
    3.  
    4. // Read file at path and convert to byte array
    5. byte[] modelData = File.ReadAllBytes(ONNXPath);
    6. var model = converter.Convert(modelData); // type is Unity.Barracuda.Model
    7. // profit...
     
    jaranov and jdmcgraw like this.
  11. Romaissa_Guetoutou

    Romaissa_Guetoutou

    Joined:
    Feb 11, 2023
    Posts:
    3
    I have an issue when I import onnx model in unity it doesn't load as NNModel. Could you help me please
     
    CloudyVR likes this.
  12. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    718
    How can I load a NNModel model from a .onnx file?
     
  13. sthuacheng

    sthuacheng

    Joined:
    Apr 19, 2018
    Posts:
    2
    you can't load "onnx" directly , and you should use NNModel byte[] value.
    you can use "model" to NNModel .modelData.Value.
    The point is ,asset can not be null,and do not create a new NNmode or modelData , just give the modelData.Value, that's all .
    Code (CSharp):
    1.  
    2.  
    3.             NNModel asset = NNmode;// just drag a  policy brain.
    4.             asset.modelData.Value = null;
    5.  
    6. ONNXModelConverter onnx = new ONNXModelConverter(true, false, true);
    7.  
    8.             Model model = onnx.Convert(Application.streamingAssetsPath + "/SimAgent.onnx");
    9.  
    10.             using (var memoryStream = new MemoryStream())
    11.             using (var writer = new BinaryWriter(memoryStream))
    12.             {
    13.                 ModelWriter.Save(writer, model);
    14.                 byte[] bbb = memoryStream.ToArray();
    15.                 asset.modelData.Value = bbb;
    16.                 BehaviorParameters.Model = asset;
    17. //when it done , policy will change.
    18.             }