"the external ONNX file data" to NNModel Instance

I would like to read the external ONNX file of ML-Agent and import it during runtime.

The code I wrote is as follows.

save me.

Please…


this is error text

https://docs.unity3d.com/Packages/com.unity.barracuda@1.0/manual/Loading.html

Does this help?

1 Like

I’ll check. Thank you for your interest.

Is there a way to change the model to NN Model?

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! :slight_smile:

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.

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.

Any updates on this?

Edit: figured it out. For future people wanting to dynamically load ONNX model files from disk without relying on Resources or Assets:

var ONNXPath = "/my/onnx/path/file.onnx";
var converter = new ONNXModelConverter(optimizeModel: true); // requires the Unity.Barracuda.ONNX assembly

// Read file at path and convert to byte array
byte[] modelData = File.ReadAllBytes(ONNXPath);
var model = converter.Convert(modelData); // type is Unity.Barracuda.Model
// profit...
2 Likes

I have an issue when I import onnx model in unity it doesn’t load as NNModel. Could you help me please

1 Like

How can I load a NNModel model from a .onnx file?

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 .

            NNModel asset = NNmode;// just drag a  policy brain.
            asset.modelData.Value = null;

ONNXModelConverter onnx = new ONNXModelConverter(true, false, true);

            Model model = onnx.Convert(Application.streamingAssetsPath + "/SimAgent.onnx");

            using (var memoryStream = new MemoryStream())
            using (var writer = new BinaryWriter(memoryStream))
            {
                ModelWriter.Save(writer, model);
                byte[] bbb = memoryStream.ToArray();
                asset.modelData.Value = bbb;
                BehaviorParameters.Model = asset;
//when it done , policy will change.
            }