MLModel 适用于 MultiArray 输出,但无法成功将输出更改为图像

MLModel works with MultiArray output but cannot successfully change the output to an image

我已经使用 coremltools 4.0 将 Keras 模型转换为 MLModel,但取得了有限的成功。

它有效,但前提是我将 MLMultiArray 用于输出并转换为图像。转换为图像比推理需要更长的时间;使其无法使用。

如果我尝试更改 MLModel 规范以使用图像进行输出,我会收到此错误 运行 预测:

Failed to convert output Identity to image:

NSUnderlyingError=0x2809bad00 {Error Domain=com.apple.CoreML Code=0 "Invalid array shape ( 2048, 2048, 3 ) for converting to gray image"

尽管我已经指定了 RGB 作为输出颜色:

output { name: "Identity" type { imageType { width: 2048 height: 2048 colorSpace: RGB } } }

如果我使用 MultiArray(有效)Xcode 报告:

output: Float32 1 x 2048 x 2048 x 3 array

我怀疑问题出在第一个维度,即批号但没有显示维度,所以无法删除批次维度:

output { name: "Identity" type { multiArrayType { dataType: FLOAT32 } } }

我认为我不能只向 Keras Conv2D 输出层添加一个输出形状,因为它有多个不同形状的入站节点。以下是输出形状:

>>> print(outputLayer.get_output_shape_at(0))
(None, None, None, 3)
>>> print(outputLayer.get_output_shape_at(1))
(1, 512, 512, 3)
>>> print(outputLayer.get_output_shape_at(2))
(1, 2048, 2048, 3)

>>> print(outputLayer.output)
Tensor("SR/Identity:0", shape=(None, None, None, 3), dtype=float32)

我认为 coremltools 混淆了通道的批次,这就是为什么即使我指定了 RGB,它也会尝试创建灰度图像。

知道如何解决吗?

我有原始的 Keras 模型,但我看不出如何在没有批量维度的情况下指定形状。 这里是Keras模型层描述的开始和结束

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
LR_input (InputLayer)           [(None, None, None,  0                                            
__________________________________________________________________________________________________
Pre_blocks_conv (Conv2D)        multiple             896         LR_input[0][0]                   
__________________________________________________________________________________________________
F_1_1_1 (Conv2D)                multiple             9248        Pre_blocks_conv[0][0]            

...                             multiple
...                             multiple

SR (Conv2D)                     multiple             84          PixelShuffle[0][0]               
==================================================================================================

在 Core ML 中,维度的顺序是(通道、高度、宽度),因此它期望看到 3 x 2048 x 2048 的输出而不是 2048 x 2048 x 3。

请注意,您还需要确保输出像素在 [0, 255] 范围内,而不是 [0, 1],这可能是您的 Keras 模型为您提供的范围。