private async Task LoadModelAsync() { if (this.model != null) { return; } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"Loading { MODEL_FILENAME } ... patience "); try { // Load Model var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{ MODEL_FILENAME }")); this.model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); // Retrieve model input and output variable descriptions (we already know the model takes an image in and outputs a tensor) var inputFeatures = this.model.Description.InputFeatures.ToList(); var outputFeatures = this.model.Description.OutputFeatures.ToList(); this.inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; this.outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; } catch (Exception ex) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"error: {ex.Message}"); model = null; } }
public async Task LoadModelAsync() { try { // Load Model var modelFile = await StorageFile.GetFileFromApplicationUriAsync( new Uri($"ms-appx:///Assets/FERPlus.onnx")); model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); // Retrieve model input and output variable descriptions (we already know // the model takes an image in and outputs a tensor) var inputFeatures = model.Description.InputFeatures.ToList(); var outputFeatures = model.Description.OutputFeatures.ToList(); inputImageDescription = inputFeatures.FirstOrDefault( feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; outputTensorDescription = outputFeatures.FirstOrDefault( feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; } catch (Exception) { model = null; throw; } }
public async void InitializeModelAsync() { //若模型已加载,则可略过此步 if (this.model != null) { return; } //ms-appx是指当前正运行应用的程序包。应该是Assets/StreamingAssets下的文件部署至Hololens后会在Data文件夹中 StorageFile tinyYoloModelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Data/StreamingAssets/TinyYOLO.onnx")); this.model = await LearningModelPreview.LoadModelFromStorageFileAsync(tinyYoloModelFile); //评估后回收内存 this.model.InferencingOptions.ReclaimMemoryAfterEvaluation = true; this.model.InferencingOptions.PreferredDeviceKind = shouldUseGpu ? LearningModelDeviceKindPreview.LearningDeviceGpu : LearningModelDeviceKindPreview.LearningDeviceCpu; //模型的输入描述是Image,输出是Tensor var inputFeatures = this.model.Description.InputFeatures.ToArray(); var outputFeatures = this.model.Description.OutputFeatures.ToArray(); this.inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; this.outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; if (this.model != null) { DisplayText("Tiny YOLO Model Loaded. Please Walk Around to Scan the Environment... "); Debug.Log("Initialize Model Async Succeed! "); } }
/// <summary> /// /// </summary> /// <returns></returns> private async Task LoadModelAsync() { if (model != null) { return; } try { //Outputを受け取るためには予め領域の確保が必要。 prob = new Dictionary <string, float>(); for (var i = 0; i < 1000; i++) { prob.Add(i.ToString(), float.NaN); } //Load Model var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{ModelFileName}")); model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); // Retrieve model input and output variable descriptions (we already know the model takes an image in and outputs a tensor) List <ILearningModelVariableDescriptorPreview> inputFeatures = model.Description.InputFeatures.ToList(); List <ILearningModelVariableDescriptorPreview> outputFeatures = model.Description.OutputFeatures.ToList(); inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; outputMapDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Map) as MapVariableDescriptorPreview; outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; //オプション設定 model.InferencingOptions.ReclaimMemoryAfterEvaluation = true; //GPUを設定すると処理速度が上がるけれど、現時点では途中でエラーになってモデルが停止する。 //model.InferencingOptions.PreferredDeviceKind = LearningModelDeviceKindPreview.LearningDeviceGpu; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => statusTBlock.Text = $"Loaded MobileNet.onnx"); } catch (Exception ex) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => statusTBlock.Text = $"error: {ex.Message}"); model = null; } }
/// <summary> /// Load the label and model files /// </summary> /// <returns></returns> private async Task LoadModelAsync() { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"Loading {_kModelFileName} ... patience "); try { // Parse labels from label file var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_kLabelsFileName}")); using (var inputStream = await file.OpenReadAsync()) using (var classicStream = inputStream.AsStreamForRead()) using (var streamReader = new StreamReader(classicStream)) { 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]); } } } // Load Model var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_kModelFileName}")); _model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); // Retrieve model input and output variable descriptions (we already know the model takes an image in and outputs a tensor) List <ILearningModelVariableDescriptorPreview> inputFeatures = _model.Description.InputFeatures.ToList(); List <ILearningModelVariableDescriptorPreview> outputFeatures = _model.Description.OutputFeatures.ToList(); _inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; _outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; } catch (Exception ex) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"error: {ex.Message}"); _model = null; } }
/// <summary> /// /// </summary> /// <returns></returns> private async Task LoadModelAsysnc() { if (model != null) { return; } try { //モデルの読み込み var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{ModelFileName}")); model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); List <ILearningModelVariableDescriptorPreview> inputFeatures = model.Description.InputFeatures.ToList(); List <ILearningModelVariableDescriptorPreview> outputFeatures = model.Description.OutputFeatures.ToList(); //入力データの形式を設定 inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; //出力データの形式を設定 outputMapDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Map) as MapVariableDescriptorPreview; outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; //オプション model.InferencingOptions.ReclaimMemoryAfterEvaluation = true; //GPUが利用できる環境ではコメントを外すと処理速度がアップする //model.InferencingOptions.PreferredDeviceKind = LearningModelDeviceKindPreview.LearningDeviceGpu; //Outputを受け取るためにあらかじめ記録領域を用意する必要がある。 //CustomVisionから出力したモデルの場合はTagの数だけ用意しておく。 loss = new Dictionary <string, float>(); for (var i = 0; i < lossNum; i++) { loss.Add(i.ToString(), float.NaN); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
private async Task LoadModelAsync() { try { // ラベルデータの読み込み var file = await ApplicationData.Current.LocalFolder.GetFileAsync("Labels.json"); using (var inputStream = await file.OpenReadAsync()) using (var classicStream = inputStream.AsStreamForRead()) using (var streamReader = new StreamReader(classicStream)) { 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]); } } } // 学習モデルの読み込み var modelFile = await ApplicationData.Current.LocalFolder.GetFileAsync("SqueezeNet.onnx"); _model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); List <ILearningModelVariableDescriptorPreview> inputFeatures = _model.Description.InputFeatures.ToList(); List <ILearningModelVariableDescriptorPreview> outputFeatures = _model.Description.OutputFeatures.ToList(); _inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; _outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; } catch (Exception ex) { _model = null; UnityEngine.WSA.Application.InvokeOnAppThread(() => { text.text = ex.ToString(); }, true); } }
private async Task LoadModelAsync(bool isGpu = true) { if (this.model != null) { return; } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"Loading { MODEL_FILENAME } ... patience"); try { // Load Model Debug.Write("1"); var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{ MODEL_FILENAME }")); this.model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); this.model.InferencingOptions.ReclaimMemoryAfterEvaluation = true; this.model.InferencingOptions.PreferredDeviceKind = isGpu == true ? LearningModelDeviceKindPreview.LearningDeviceGpu : LearningModelDeviceKindPreview.LearningDeviceCpu; Debug.Write("2"); //入力、出力を受ける変数。入力には画像、出力にテンソルが入る前提 var inputFeatures = this.model.Description.InputFeatures.ToList(); var outputFeatures = this.model.Description.OutputFeatures.ToList(); this.inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) // 配列の先頭の要素を返すか、空なら規定値を返す as ImageVariableDescriptorPreview; this.outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"Loaded { MODEL_FILENAME }. Press the camera button to start the webcam..."); } catch (Exception ex) { Debug.Write("kuso"); Debug.Write(ex.Message); await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"error: {ex.Message}"); model = null; } }
private async Task LoadModelAsync(bool isGpu = true) { if (this.model != null) { return; } await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"Loading { MODEL_FILENAME } ... patience "); try { // Load Model var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{ MODEL_FILENAME }")); this.model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); this.model.InferencingOptions.ReclaimMemoryAfterEvaluation = true; this.model.InferencingOptions.PreferredDeviceKind = isGpu == true ? LearningModelDeviceKindPreview.LearningDeviceGpu : LearningModelDeviceKindPreview.LearningDeviceCpu; // Retrieve model input and output variable descriptions (we already know the model takes an image in and outputs a tensor) var inputFeatures = this.model.Description.InputFeatures.ToList(); var outputFeatures = this.model.Description.OutputFeatures.ToList(); this.inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; this.outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"Loaded { MODEL_FILENAME }. Press the camera button to start the webcam..."); } catch (Exception ex) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => StatusBlock.Text = $"error: {ex.Message}"); model = null; } }
// Loads the ML model file and sets private async Task LoadModelAsync() { if (model != null) { return; } Debug.WriteLine($"Loading Model"); try { // Load Model var modelFile = await StorageFile.GetFileFromApplicationUriAsync(ModelUri); Debug.WriteLine($"Model file discovered at: {modelFile.Path}"); model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); Debug.WriteLine($"LearningModelPReview object instantiated: {modelFile.Path}"); options = model.InferencingOptions; options.PreferredDeviceKind = LearningModelDeviceKindPreview.LearningDeviceGpu; model.InferencingOptions = options; // Retrieve model input and output variable descriptions (we already know the model takes an image in and outputs a tensor) var inputFeatures = model.Description.InputFeatures.ToList(); Debug.WriteLine($"{inputFeatures.Count} Input Features"); var outputFeatures = model.Description.OutputFeatures.ToList(); Debug.WriteLine($"{inputFeatures.Count} Output Features"); inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; binding = new LearningModelBindingPreview(model); // Create bindings for the input and output buffer outputArray = new List <float>(); // R4 WinML does needs the output pre-allocated for multi-dimensional tensors outputArray.AddRange(new float[21125]); // Total size of TinyYOLO output if (inputImageDescription != null && outputTensorDescription != null) { binding.Bind(inputImageDescription.Name, evaluatableVideoFrame); binding.Bind(outputTensorDescription.Name, outputArray); modelBindingComplete = true; } else { modelBindingComplete = false; } } catch (Exception ex) { Debug.WriteLine($"Error loading model: {ex.Message}"); model = null; modelBindingComplete = false; } }
public async Task LoadModelAsync(bool shouldUseGpu = false) { try { // Parse labels from label file 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 Model via Unity // The WinML LearningModelPreview.LoadModelFromStreamAsync is 'not implemented' so we are using the trick to load via Unity Resource as txt file... // Thanks Mike for the hint! https://mtaulty.com/2018/03/29/third-experiment-with-image-classification-on-windows-ml-from-uwp-on-hololens-in-unity/ IStorageFile modelFile = null; var fileName = "model.bytes"; try { modelFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(fileName); } catch (FileNotFoundException) { } if (modelFile == null) { var modelResource = Resources.Load(ModelFileName) as TextAsset; modelFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(fileName); await FileIO.WriteBytesAsync(modelFile, modelResource.bytes); } // Initialize model _model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); _model.InferencingOptions.ReclaimMemoryAfterEvaluation = true; _model.InferencingOptions.PreferredDeviceKind = shouldUseGpu ? LearningModelDeviceKindPreview.LearningDeviceGpu : LearningModelDeviceKindPreview.LearningDeviceCpu; // Get model input and output descriptions List <ILearningModelVariableDescriptorPreview> inputFeatures = _model.Description.InputFeatures.ToList(); List <ILearningModelVariableDescriptorPreview> outputFeatures = _model.Description.OutputFeatures.ToList(); _inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; _outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; #endif } catch { #if ENABLE_WINMD_SUPPORT _model = null; #endif throw; } }
/// <summary> /// Load the label and model files /// </summary> /// <returns></returns> private async Task LoadModelAsync() { // just load the model one time. if (_model != null) { return; } StatusBlock.Text = $"Loading {_kModelFileName} ... patience "; try { // Parse labels from label json file. We know the file's // entries are already sorted in order. var fileString = File.ReadAllText($"Assets/{_kLabelsFileName}"); var fileDict = JsonConvert.DeserializeObject <Dictionary <string, string> >(fileString); foreach (var kvp in fileDict) { _labels.Add(kvp.Value); } // legacy code for comparison /* * var labels = new List<string>(); * var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_kLabelsFileName}")); * using (var inputStream = await file.OpenReadAsync()) * using (var classicStream = inputStream.AsStreamForRead()) * using (var streamReader = new StreamReader(classicStream)) * { * 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]); * } * } * } */ // Load Model var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_kModelFileName}")); _model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); // Retrieve model input and output variable descriptions (we already know the model takes an image in and outputs a tensor) List <ILearningModelVariableDescriptorPreview> inputFeatures = _model.Description.InputFeatures.ToList(); List <ILearningModelVariableDescriptorPreview> outputFeatures = _model.Description.OutputFeatures.ToList(); _inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; _outputTensorDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Tensor) as TensorVariableDescriptorPreview; } catch (Exception ex) { StatusBlock.Text = $"error: {ex.Message}"; _model = null; } }