/// <summary> /// Create a worker that will execute `asset` using the best backend that is available for a given `device` type. /// This is just a convenience function that internally calls `ModelLoader.Load` followed by ``WorkerFactory.CreateWorker`. /// `asset` is the associated NNModel asset. /// `additionalOutputs` are the additional outputs to track but not directly specified by the model. /// `trimOutputs` are the outputs not discard even if they are specified by the model. /// `device` is the device type to run worker on. For example `WorkerFactory.Device.GPU` specifies the fast GPU path. /// `verbose` will log scheduling of layers execution to the console (default == false) /// </summary> public static IWorker CreateWorker(this NNModel asset, string[] additionalOutputs, string[] trimOutputs, WorkerFactory.Device device = WorkerFactory.Device.Auto, bool verbose = false) { var model = ModelLoader.Load(asset); return(WorkerFactory.CreateWorker(model, additionalOutputs, trimOutputs, device, verbose)); }
/// <summary> /// Create a worker that will execute `asset` using the best backend that is available for a given `device` type. /// This is just a convenience function that internally calls `ModelLoader.Load` followed by ``WorkerFactory.CreateWorker`. /// `asset` is the associated NNModel asset. /// `device` is the preferred device for execution. For example `WorkerFactory.Device.GPU` specifies the fast GPU path. /// `verbose` will log scheduling of layers execution to the console. /// </summary> public static IWorker CreateWorker(this NNModel asset, WorkerFactory.Device device = WorkerFactory.Device.Auto, bool verbose = false) { var model = ModelLoader.Load(asset); return(WorkerFactory.CreateWorker(model, device, verbose)); }
// Asset importer callback public override void OnImportAsset(AssetImportContext ctx) { var onnxModel = new ModelProto(); using (var readStream = new FileStream(ctx.assetPath, FileMode.Open)) using (var inputStream = new CodedInputStream(readStream)) onnxModel.MergeFrom(inputStream); var model = ConvertOnnxModel(onnxModel); D.Log($"ONNX v{model.IrVersion}. Producer: {model.ProducerName}. Asset path: {ctx.assetPath}"); D.Log($"Barracuda model: {model}"); NNModelData assetData = ScriptableObject.CreateInstance <NNModelData>(); using (var memoryStream = new MemoryStream()) using (var writer = new BinaryWriter(memoryStream)) { ModelWriter.Save(writer, model); assetData.Value = memoryStream.ToArray(); } assetData.name = "Data"; assetData.hideFlags = HideFlags.HideInHierarchy; NNModel asset = ScriptableObject.CreateInstance <NNModel>(); asset.modelData = assetData; ctx.AddObjectToAsset("main obj", asset, LoadIconTexture()); ctx.AddObjectToAsset("model data", assetData); ctx.SetMainObject(asset); }
/// <summary> /// Return an object oriented representation (aka: `Model`) of a neural network from a binary representation of type `NNModel`. /// By default details are logged to the console, set `verbose` to false to load silently. /// </summary> public static Model Load(NNModel model, bool verbose = true) { return(Load(model.Value, verbose)); }
/// <summary> /// Return an object oriented representation (aka: `Model`) of a neural network from a binary representation of type `NNModel`. /// By default details are not logged to the console, set `verbose` to true to see loading details. /// </summary> public static Model Load(NNModel model, bool verbose = false) { return(Load(model.modelData.Value, verbose)); }