Espresso 异常:“无效参数”:加载 mlmodel 时的一般形状内核

问题描述

我从 tf.keras 转换了我的 mlmodel。目标是从图像中识别手写文本 当我使用此代码运行它时:

func performCoreMLImageRecognition(_ image: UIImage) {
     
        let model = try! HTRModel()
        // process input image
        let scale = image.scaledImage(200)
        let sized = scale?.resize(size: CGSize(width: 200,height: 50))
        let gray = sized?.rgb2GrayScale()
        
        guard let pixelBuffer = sized?.pixelBufferGray(width: 200,height: 50) else { fatalError("Cannot convert image to pixelBufferGray")}
        
        UIImageWritetoSavedPhotosAlbum(gray!,self,#selector(self.didFinishSavingImage(_:didFinishSavingWithError:contextInfo:)),nil)
        
        let mlArray = try! MLMultiArray(shape: [1,1],dataType: MLMultiArrayDataType.float32)
        let htrinput = HTRInput(image: pixelBuffer,label: mlArray)
        if let prediction = try? model.prediction(input: htrinput) {
            print(prediction)
        }
    }

我收到以下错误

[espresso] [Espresso::handle_ex_plan] exception=Espresso exception: "Invalid argument": generic_reshape_kernel: Invalid bottom shape (64 12 1 1 1) for reshape to (768 50 -1 1 1) status=-6
2021-01-21 20:23:50.712585+0900 Guided Camera[7575:1794819] [coreml] Error computing NN outputs -6
2021-01-21 20:23:50.712611+0900 Guided Camera[7575:1794819] 
[coreml] Failure in -executePlan:error:.

这是模型配置

enter image description here

该模型运行得非常好。我哪里出错了。我不精通 swift 需要帮助。 这个错误是什么意思,我该如何解决这个错误

解决方法

有时在从 Keras(或其他)到 Core ML 的转换过程中,转换器不了解如何处理某些操作,从而导致模型不起作用。

在你的例子中,有一个层输出一个形状为 (64,12,1,1) 的张量,而有一个 reshape 层,它期望可以被重新整形为 (768,50,-1,1).

您需要找出哪个层进行了此重塑,然后检查 Core ML 模型为什么它会获得大小不正确的输入张量。仅仅因为它在 Keras 中运行良好并不意味着向 Core ML 的转换是完美的。

您可以使用开源模型查看器 Netron 检查 Core ML 模型。

(请注意,64x12 = 768,因此问题似乎出在该张量中的 50。)