windows-runtime - WinML onnx 示例产生错误的结果
问题描述
我正在按照这个 WinML示例创建一个回归模型并对其进行推理。我尝试使用 WinML/WinRT 运行 onnx 模型,但结果错误。我强制将数据读取为 RGB 而不是 BGR,但其中包含一个 alpha 组件,例如 RGBA,我怀疑错误的结果是由于 alpha 而我的原始模型中没有它。我该如何解决这个问题? 代码片段 控制台输出
VideoFrame LoadImageFile(hstring filePath, ColorManagementMode colorManagementMode)
{
BitmapDecoder decoder = NULL;
try
{
// open the file
StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
// get a stream on it
auto stream = file.OpenAsync(FileAccessMode::Read).get();
// Create the decoder from the stream
decoder = BitmapDecoder::CreateAsync(stream).get();
decoder.GetPixelDataAsync().get().DetachPixelData()[3] = 1;
decoder.GetPixelDataAsync().get().DetachPixelData()[7] = 1;
auto pix = decoder.GetPixelDataAsync(BitmapPixelFormat::Rgba8, BitmapAlphaMode::Ignore, BitmapTransform(), ExifOrientationMode::IgnoreExifOrientation, ColorManagementMode::DoNotColorManage);
for (auto b : pix.get().DetachPixelData())printf("%d\n", b);
// printf("my pixels: %d", pix.get().DetachPixelData();
}
catch (...)
{
printf(" Failed to load the image file, make sure you are using fully qualified paths\r\n");
exit(EXIT_FAILURE);
}
SoftwareBitmap softwareBitmap = NULL;
try
{
softwareBitmap = decoder.GetSoftwareBitmapAsync(
//decoder.BitmapPixelFormat(),
BitmapPixelFormat::Rgba8,
//decoder.BitmapAlphaMode(),
BitmapAlphaMode::Ignore,
BitmapTransform(),
ExifOrientationMode::RespectExifOrientation,
colorManagementMode
).get();
printf("Image format: %d\n", softwareBitmap.BitmapPixelFormat());
}
当我从解码器读取像素时,我得到 RGBA,其中 A (alpha) 设置为 255。我尝试将其替换为 1,但似乎该解码器是不可变的。如果我可以确保输入到模型的像素是正确的,那么这将产生正确的结果。
解决方案
请按照示例创建 VideoFrame,创建 videoFrame 时无需担心 alpha 通道,因为 WinML 无论如何都会忽略此通道。如果需要,调用SoftwareBitmap.Convert()在不同的像素格式之间进行转换
VideoFrame LoadImageFile(hstring filePath)
{
printf("Loading the image...\n");
DWORD ticks = GetTickCount();
VideoFrame inputImage = nullptr;
try
{
// open the file
StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
// get a stream on it
auto stream = file.OpenAsync(FileAccessMode::Read).get();
// Create the decoder from the stream
BitmapDecoder decoder = BitmapDecoder::CreateAsync(stream).get();
// get the bitmap
SoftwareBitmap softwareBitmap = decoder.GetSoftwareBitmapAsync().get();
// load a videoframe from it
inputImage = VideoFrame::CreateWithSoftwareBitmap(softwareBitmap);
}
catch (...)
{
printf("failed to load the image file, make sure you are using fully qualified paths\r\n");
exit(EXIT_FAILURE);
}
ticks = GetTickCount() - ticks;
printf("image file loaded in %d ticks\n", ticks);
// all done
return inputImage;
}
推荐阅读
- html - 当内容大小溢出父容器时包装内容
- wordpress - 新用户注册时如何将 wp_users -> 用户登录名插入 wp_terms?
- sql - 获取记录需要很长时间
- google-api - 谷歌云视觉无法检测到一个字符
- selenium - 如何使用 selenium webdriver 处理出现在加载页面中间的证书弹出窗口
- regex - vim regex removing all words that contain a certain character that will be at the end of the word
- c# - 当我们切换缩放比例时,AutoScale Factor 没有正确更新
- node.js - 尽管 MongoDB 中存在集合,但 Mongoose 显示空对象
- javascript - 谷歌分析是否有任何字段可用于 eventID 和 eventURL
- android - how to fetch data from parse database and use it in different function