【发布时间】:2018-08-06 23:36:39
【问题描述】:
我基于存储库 (C#) 中提供的 Squeeze Net 示例构建了一个 UWP 应用程序,该应用程序使用深度学习模型 (ONNX) 进行图像分类。我在 PyTorch 中构建了深度学习模型,其中图像的像素值已从 [0, 255] 范围缩小到 [0, 1] 范围,然后使用通道明智 (RGB) 标准差和均值进行归一化。因此,此模型需要 [0, 255] 范围以外的像素值。
但在 UWP 应用程序中,我无法在将输入绑定到模型之前执行像素值的缩小。我搜索了 SoftwareBitmap 类,但找不到执行此缩小操作的方法。任何帮助将不胜感激。
我需要在这些代码行之间的某个地方进行此操作。
await LoadModel();
// Trigger file picker to select an image file
var picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
StorageFile file = await picker.PickSingleFileAsync();
outputTextBlock.Text = $"The selected Image: {file.Name}";
SoftwareBitmap softwareBitmap;
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
{
// Create the decoder from the stream
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
PixelDataProvider random = await decoder.GetPixelDataAsync();
// byte[] pD = random.DetachPixelData();
//await FileIO.WriteBytesAsync("path/file.ext", pD);
// System.IO.File.WriteAllBytes("path/file.ext", pD);
// byteData.Text = $"{pD}";
// Get the SoftwareBitmap representation of the file in BGRA8 format
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
}
var streamD = await file.OpenReadAsync();
var imageSource = new BitmapImage();
await imageSource.SetSourceAsync(streamD);
selectedImage.Source = imageSource;
// Display the image
//SoftwareBitmapSource imageSource = new SoftwareBitmapSource();
//await imageSource.SetBitmapAsync(softwareBitmap);
//selectedImage.Source = imageSource;
// Encapsulate the image within a VideoFrame to be bound and evaluated
VideoFrame inputWoodImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
await EvaluateVideoFrameAsync(inputWoodImage);
【问题讨论】:
-
将控件添加到
Canvas。绑定那个控件的Height和Width就可以调整大小了。 -
我对你的问题感到困惑,你想要什么? “降级”是什么意思?您只是想降低图像质量吗?
-
嘿,当我想在 UWP 应用程序中使用我在 Pytorch 中构建的深度学习模型时。最初在 [0, 255] 范围内的图像像素值首先转换为范围为 [0, 1] 的张量。这是通过将每个像素值除以 255 以将其降低到范围 [0, 1] 来完成的。然后用每个通道 (RGB) 的均值和标准差对该张量进行归一化。我能够将像素值从 [0, 255] 范围缩小到 [0, 1] 但无法使用均值和标准差进一步规范化这些字节数据类型值。任何帮助将不胜感激。