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;
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...