微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WinML onnx示例产生错误结果

如何解决WinML onnx示例产生错误结果

我正在遵循此WinML example来创建回归模型并对其进行推断。 我尝试使用WinML / WinRT运行onnx模型,但结果出了错误。我强制将数据读取为RGB而不是BGR,但是其中包含一个alpha分量,例如RGBA,我怀疑错误的结果是由于alpha导致的,而我的原始模型中没有该结果。我该如何解决这个问题? Code snippet Console output

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(),ExifOrientationMode::RespectExifOrientation,colorManagementMode
        ).get();
        
        printf("Image format: %d\n",softwareBitmap.BitmapPixelFormat());
    }

当我从解码器读取像素时,我得到RGBA,其中A(alpha)设置为255。我尝试将其替换为1,但看来解码器是不可变的。 如果我可以确保输入模型的像素正确,那么将产生正确的结果。

解决方法

请按照 example 创建 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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。