public static async Task <CustomNetworkModel> CreateFromStreamAsync(IRandomAccessStreamReference stream) { // Run on the GPU //var device = new LearningModelDevice(LearningModelDeviceKind.DirectX); CustomNetworkModel learningModel = new CustomNetworkModel(); learningModel.model = await LearningModel.LoadFromStreamAsync(stream); //learningModel.session = new LearningModelSession(learningModel.model, device); learningModel.session = new LearningModelSession(learningModel.model); learningModel.binding = new LearningModelBinding(learningModel.session); return(learningModel); }
/// <summary> /// Asyncrhonously load the onnx model from Visual Studio assets folder /// </summary> /// <returns></returns> public async Task LoadModelAsync() { try { // Parse imagenet labels from label json file // https://github.com/reneschulte/WinMLExperiments/ var labelsTextAsset = Resources.Load(LabelsFileName) as TextAsset; using (var streamReader = new StringReader(labelsTextAsset.text)) { string line = ""; char[] charToTrim = { '\"', ' ' }; while (streamReader.Peek() >= 0) { line = streamReader.ReadLine(); line.Trim(charToTrim); var indexAndLabel = line.Split(':'); if (indexAndLabel.Count() == 2) { _labels.Add(indexAndLabel[1]); } } } #if ENABLE_WINMD_SUPPORT // Load onnx model from Visual studio assets folder, build VS project in Unity // then add the onnx model to the Assets folder in visual studio solution StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/model.onnx")); _customNetworkModel = await CustomNetworkModel.CreateFromStreamAsync(modelFile as IRandomAccessStreamReference); UnityEngine.Debug.Log("LoadModelAsync: Onnx model loaded successfully."); #endif } catch { #if ENABLE_WINMD_SUPPORT _customNetworkModel = null; UnityEngine.Debug.Log("LoadModelAsync: Onnx model failed to load."); #endif throw; } }