// Create a model from an ONNX 1.2 file public static async Task <CustomVisionModel> CreateONNXModel(StorageFile file) { LearningModel learningModel = null; try { learningModel = await LearningModel.LoadFromStorageFileAsync(file); } catch (Exception ex) { throw ex; } var inputFeatures = learningModel.InputFeatures; ImageFeatureDescriptor inputImageDescription = inputFeatures?.FirstOrDefault(feature => feature.Kind == LearningModelFeatureKind.Image) as ImageFeatureDescriptor; uint inputImageWidth = 0, inputImageHeight = 0; if (inputImageDescription != null) { inputImageHeight = inputImageDescription.Height; inputImageWidth = inputImageDescription.Width; } return(new CustomVisionModel() { _learningModel = learningModel, _session = new LearningModelSession(learningModel), InputImageWidth = (int)inputImageWidth, InputImageHeight = (int)inputImageHeight }); }
public void debugModelIO() { string _inName, _outName; foreach (var inputF in _learningModel.InputFeatures) { Debug.WriteLine($"input | kind:{inputF.Kind}, name:{inputF.Name}, type:{inputF.GetType()}"); int i = 0; ImageFeatureDescriptor imgDesc = inputF as ImageFeatureDescriptor; TensorFeatureDescriptor tfDesc = inputF as TensorFeatureDescriptor; _inWidth = (uint)(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width); _inHeight = (uint)(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height); _inName = inputF.Name; Debug.WriteLine($"N: {(imgDesc == null ? tfDesc.Shape[0] : 1)}, " + $"Channel: {(imgDesc == null ? tfDesc.Shape[1].ToString() : imgDesc.BitmapPixelFormat.ToString())}, " + $"Height:{(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height)}, " + $"Width: {(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width)}"); } foreach (var outputF in _learningModel.OutputFeatures) { Debug.WriteLine($"output | kind:{outputF.Kind}, name:{outputF.Name}, type:{outputF.GetType()}"); int i = 0; ImageFeatureDescriptor imgDesc = outputF as ImageFeatureDescriptor; TensorFeatureDescriptor tfDesc = outputF as TensorFeatureDescriptor; _outWidth = (uint)(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width); _outHeight = (uint)(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height); _outName = outputF.Name; Debug.WriteLine($"N: {(imgDesc == null ? tfDesc.Shape[0] : 1)}, " + $"Channel: {(imgDesc == null ? tfDesc.Shape[1].ToString() : imgDesc.BitmapPixelFormat.ToString())}, " + $"Height:{(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height)}, " + $"Width: {(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width)}"); } }
/// <summary> /// Load the labels and model and initialize WinML /// </summary> /// <returns></returns> private async Task LoadModelAsync(string modelFileName) { Debug.WriteLine("LoadModelAsync"); _evaluationLock.Wait(); { m_binding = null; m_model = null; m_session = null; _isReadyForEval = false; try { // Start stopwatch _perfStopwatch.Restart(); // Load Model StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/{modelFileName}.onnx")); m_model = await LearningModel.LoadFromStorageFileAsync(modelFile); // Stop stopwatch _perfStopwatch.Stop(); // Setting preferred inference device given user's intent m_inferenceDeviceSelected = _useGPU ? LearningModelDeviceKind.DirectXHighPerformance : LearningModelDeviceKind.Cpu; m_session = new LearningModelSession(m_model, new LearningModelDevice(m_inferenceDeviceSelected)); // Debugging logic to see the input and output of ther model and retrieve dimensions of input/output variables // ### DEBUG ### foreach (var inputF in m_model.InputFeatures) { Debug.WriteLine($"input | kind:{inputF.Kind}, name:{inputF.Name}, type:{inputF.GetType()}"); int i = 0; ImageFeatureDescriptor imgDesc = inputF as ImageFeatureDescriptor; TensorFeatureDescriptor tfDesc = inputF as TensorFeatureDescriptor; m_inWidth = (uint)(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width); m_inHeight = (uint)(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height); m_inName = inputF.Name; Debug.WriteLine($"N: {(imgDesc == null ? tfDesc.Shape[0] : 1)}, " + $"Channel: {(imgDesc == null ? tfDesc.Shape[1].ToString() : imgDesc.BitmapPixelFormat.ToString())}, " + $"Height:{(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height)}, " + $"Width: {(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width)}"); } foreach (var outputF in m_model.OutputFeatures) { Debug.WriteLine($"output | kind:{outputF.Kind}, name:{outputF.Name}, type:{outputF.GetType()}"); int i = 0; ImageFeatureDescriptor imgDesc = outputF as ImageFeatureDescriptor; TensorFeatureDescriptor tfDesc = outputF as TensorFeatureDescriptor; m_outWidth = (uint)(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width); m_outHeight = (uint)(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height); m_outName = outputF.Name; Debug.WriteLine($"N: {(imgDesc == null ? tfDesc.Shape[0] : 1)}, " + $"Channel: {(imgDesc == null ? tfDesc.Shape[1].ToString() : imgDesc.BitmapPixelFormat.ToString())}, " + $"Height:{(imgDesc == null ? tfDesc.Shape[2] : imgDesc.Height)}, " + $"Width: {(imgDesc == null ? tfDesc.Shape[3] : imgDesc.Width)}"); } // ### END OF DEBUG ### // Create output frame _outputFrame?.Dispose(); _outputFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)m_outWidth, (int)m_outHeight); Debug.WriteLine($"Elapsed time: {_perfStopwatch.ElapsedMilliseconds} ms"); _isReadyForEval = true; } catch (Exception ex) { NotifyUser($"error: {ex.Message}", NotifyType.ErrorMessage); Debug.WriteLine($"error: {ex.Message}"); } } _evaluationLock.Release(); }
/// <summary> /// Crop image given a imageVariableDescription /// </summary> /// <param name="inputVideoFrame"></param> /// <returns></returns> public static IAsyncOperation <VideoFrame> CenterCropImageAsync(VideoFrame inputVideoFrame, ImageFeatureDescriptor imageVariableDescription) { return(CenterCropImageAsync(inputVideoFrame, imageVariableDescription.Width, imageVariableDescription.Height)); }