public void SetProperties(IPropertySet configuration) { this.configuration = configuration; var modelFile = StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///segm_video_effect_uwp/Assets/{_kModelFileName}")).GetAwaiter().GetResult(); Debug.WriteLine(modelFile); _learningModel = LearningModelPreview.LoadModelFromStorageFileAsync(modelFile).GetAwaiter().GetResult(); InferencingOptionsPreview options = _learningModel.InferencingOptions; options.PreferredDeviceKind = LearningModelDeviceKindPreview.LearningDeviceGpu; //options.IsTracingEnabled = true; _learningModel.InferencingOptions = options; fpsTimer = new Timer(1000); fpsTimer.Elapsed += this.OnTimedEvent; fpsTimer.AutoReset = true; fpsTimer.Start(); fpsLabelOffset = new Vector2(10, 10); fpsLabelColor = Color.FromArgb(255, 255, 0, 0); }
/// <summary> /// Load the labels and model and initialize WinML /// </summary> /// <returns></returns> private async Task LoadModelAsync() { _evaluationLock.Wait(); { _binding = null; _model = null; _isReadyForEval = false; try { // Load Model StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{_kModelFileName}.onnx")); _model = await LearningModelPreview.LoadModelFromStorageFileAsync(modelFile); // Hardcoding to use GPU InferencingOptionsPreview options = _model.InferencingOptions; options.PreferredDeviceKind = _useGPU ? LearningModelDeviceKindPreview.LearningDeviceGpu : LearningModelDeviceKindPreview.LearningDeviceCpu; _model.InferencingOptions = options; // Debugging logic to see the input and output of ther model List <ILearningModelVariableDescriptorPreview> inputFeatures = _model.Description.InputFeatures.ToList(); List <ILearningModelVariableDescriptorPreview> outputFeatures = _model.Description.OutputFeatures.ToList(); var metadata = _model.Description.Metadata; foreach (var md in metadata) { Debug.WriteLine($"{md.Key} | {md.Value}"); } _inputImageDescription = inputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; _outputImageDescription = outputFeatures.FirstOrDefault(feature => feature.ModelFeatureKind == LearningModelFeatureKindPreview.Image) as ImageVariableDescriptorPreview; _isReadyForEval = true; } catch (Exception ex) { NotifyUser($"error: {ex.Message}", NotifyType.ErrorMessage); Debug.WriteLine($"error: {ex.Message}"); } } _evaluationLock.Release(); }
// 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; } }