public static async Task <u2netModel> CreateFromStreamAsync(IRandomAccessStreamReference stream) { u2netModel learningModel = new u2netModel(); learningModel.model = await LearningModel.LoadFromStreamAsync(stream); learningModel.session = new LearningModelSession(learningModel.model); learningModel.binding = new LearningModelBinding(learningModel.session); return(learningModel); }
private async void Button_Click(object sender, RoutedEventArgs e) { // Use Picket to get file var file = await GetImageFile(); SoftwareBitmap softwareBitmap; byte[] bytes; // Load image & scale to tensor input dimensions using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { bytes = await GetImageAsByteArrayAsync(stream, 320, 320, BitmapPixelFormat.Rgba8); softwareBitmap = await GetImageAsSoftwareBitmapAsync(stream, 320, 320, BitmapPixelFormat.Bgra8); } // Display source image var source = new SoftwareBitmapSource(); await source.SetBitmapAsync(softwareBitmap); sourceImage.Source = source; // Convert rgba-rgba-...-rgba to bb...b-rr...r-gg...g as colour weighted tensor (0..1) TensorFloat input = TensorFloat.CreateFromIterable(new long[] { 1, 3, 320, 320 }, TensorBrg(bytes)); // Load model & perform inference StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/u2net.onnx")); u2netModel model = await u2netModel.CreateFromStreamAsync(modelFile); Stopwatch sw = new Stopwatch(); sw.Start(); u2netOutput output = await model.EvaluateAsync(new u2netInput { input = input }); sw.Stop(); await ToImage(output.o6, o6); await ToImage(output.o5, o5); await ToImage(output.o4, o4); await ToImage(output.o3, o3); await ToImage(output.o2, o2); await ToImage(output.o1, o1); await ToBlendedImage(bytes, output.o0, targetImage); }