简体   繁体   English

MLModel 与 MultiArray output 一起使用,但无法成功将 output 更改为图像

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

I have converted a Keras model to a MLModel using coremltools 4.0 with limited success.我已使用 coremltools 4.0 将 Keras model 转换为 MLModel,但成功有限。

It works but only if I use an MLMultiArray for the output and covert to an image.它有效,但前提是我为 output 使用 MLMultiArray 并转换为图像。 Converting to an image takes magnitudes longer than inferencing;转换为图像比推理需要更长的时间; making it unusable.使其无法使用。

If I try to change the MLModel spec to use images for output I get this error running prediction:如果我尝试更改 MLModel 规范以使用 output 的图像,我会收到此错误运行预测:

Failed to convert output Identity to image:无法将 output 身份转换为图像:

NSUnderlyingError=0x2809bad00 {Error Domain=com.apple.CoreML Code=0 "Invalid array shape ( 2048, 2048, 3 ) for converting to gray image" NSUnderlyingError=0x2809bad00 {Error Domain=com.apple.CoreML Code=0 "无效的数组形状 (2048, 2048, 3) 用于转换为灰度图像"

Even though I have specified RGB for the output color:即使我为 output 颜色指定了RGB

output { name: "Identity" type { imageType { width: 2048 height: 2048 colorSpace: RGB } } } output { 名称:“身份”类型 { imageType { 宽度:2048 高度:2048 颜色空间: RGB } } }

If I use a MultiArray (that works) Xcode reports:如果我使用 MultiArray (有效) Xcode 报告:

output: Float32 1 x 2048 x 2048 x 3 array output:Float32 1 x 2048 x 2048 x 3 阵列

I suspect the issue is the first dimension, which is the batch number but no dimensions are shown, so I can't delete the batch dimension:我怀疑问题出在第一个维度,即批号,但没有显示任何维度,所以我无法删除批次维度:

output { name: "Identity" type { multiArrayType { dataType: FLOAT32 } } } output { 名称:“身份”类型 { multiArrayType { dataType: FLOAT32 } } }

I don't think I can just add an output shape to the Keras Conv2D output layer because it has multiple inbound nodes with different shapes.我不认为我可以将 output 形状添加到 Keras Conv2D output 层,因为它有多个不同形状的入站节点。 Here are the output shapes:以下是 output 形状:

>>> 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)

I think coremltools is confusing the batch for the channels that is why it is attempting to create a grayscale image even if I specify RGB.我认为 coremltools 混淆了通道的批次,这就是为什么即使我指定 RGB 也尝试创建灰度图像的原因。

Any idea how to fix it?知道如何解决吗?

I have the original Keras model, but I don't see how specify shapes without a batch dimension.我有原始的 Keras model,但我看不到如何在没有批次尺寸的情况下指定形状。 Here is the beginning and ending of the Keras model layer description这里是Keras model层描述的开始和结束

__________________________________________________________________________________________________
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]               
==================================================================================================

In Core ML the order of the dimensions is (channels, height, width) so it expects to see a 3 x 2048 x 2048 output instead of 2048 x 2048 x 3.在 Core ML 中,尺寸的顺序是(通道、高度、宽度),因此它期望看到 3 x 2048 x 2048 output 而不是 2048 x 2048 x 3。

Note that you also need to make sure the output pixels are in the range [0, 255] instead of [0, 1] which is probably what your Keras model gives you.请注意,您还需要确保 output 像素在 [0, 255] 而不是 [0, 1] 范围内,这可能是您的 Keras Z20F35E630DAF44DBFA4C3F68F5399DC8 给您的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM