private async Task EvaluteImageAsync(VideoFrame videoFrame) { try { var startTime = DateTime.Now; if (model == null) { var modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Model/Inceptionv3.onnx")); if (modelFile != null) { model = new Inceptionv3Model(); await MLHelper.CreateModelAsync(modelFile, model); } } var input = new Inceptionv3Input() { image = ImageFeatureValue.CreateFromVideoFrame(videoFrame) }; var res = await model.EvaluateAsync(input) as Inceptionv3Output; if (res != null) { var results = new List <LabelResult>(); if (res.classLabelProbs != null) { var dict = res.classLabelProbs.FirstOrDefault(); foreach (var kv in dict) { results.Add(new LabelResult { Label = kv.Key, Result = (float)Math.Round(kv.Value * 100, 2) }); } results.Sort((p1, p2) => { return(p2.Result.CompareTo(p1.Result)); }); } await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () => { previewControl.EvalutionTime = (DateTime.Now - startTime).TotalSeconds.ToString(); outputText.Text = res.classLabel.GetAsVectorView().FirstOrDefault(); resultList.ItemsSource = results; }); } } catch (Exception ex) { await AlertHelper.ShowMessageAsync(ex.ToString()); } }
/// <summary> /// init a ML model /// </summary> /// <param name="file"></param> /// <param name="model"></param> /// <returns></returns> public async static Task CreateModelAsync(StorageFile file, IMachineLearningModel learningModel, bool _useCPU = false) { LearningModelDevice device = null; if (_useCPU) { device = new LearningModelDevice(LearningModelDeviceKind.Default); } else { device = new LearningModelDevice(LearningModelDeviceKind.DirectXHighPerformance); } learningModel.LearningModel = await LearningModel.LoadFromStreamAsync(file); learningModel.Session = new LearningModelSession(learningModel.LearningModel, device); learningModel.Binding = new LearningModelBinding(learningModel.Session); }
/// <summary> /// evaluate a model /// </summary> /// <param name="input"></param> /// <param name="learningModel"></param> /// <returns></returns> public async static Task <(IMachineLearningOutput Result, double EvalutionTime)> EvaluateWithTimeReturnAsync(IMachineLearningInput input, IMachineLearningModel learningModel) { var startTime = DateTime.Now; var output = await EvaluateAsync(input, learningModel); var costTime = (DateTime.Now - startTime).TotalSeconds; return(output, costTime); }
/// <summary> /// evaluate a model /// </summary> /// <param name="input"></param> /// <param name="learningModel"></param> /// <returns></returns> public async static Task <IMachineLearningOutput> EvaluateAsync(IMachineLearningInput input, IMachineLearningModel learningModel) { var output = await learningModel.EvaluateAsync(input); return(output); }
/// <summary> /// init a ML model /// </summary> /// <param name="file"></param> /// <param name="model"></param> /// <returns></returns> public async static Task CreateModelAsync(StorageFile file, IMachineLearningModel model) { var learningModel = await LearningModelPreview.LoadModelFromStorageFileAsync(file); model.LearningModel = learningModel; }