【问题标题】:Espresso exception: "Invalid argument":general shape kernel while loading mlmodelEspresso 异常:“无效参数”:加载 mlmodel 时的一般形状内核
【发布时间】:2021-04-26 14:58:27
【问题描述】:

我从 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:.

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

【问题讨论】:

    标签: swift machine-learning tf.keras coreml handwriting-recognition


    【解决方案1】:

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

    在您的情况下,有一个层输出形状为 (64, 12, 1, 1, 1) 的张量,而有一个重塑层期望可以重塑为 (768, 50, -1, 1, 1)。

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

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

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

    【讨论】:

    • 谢谢!该错误似乎与缩放有关。通过在转换模型时添加适当的缩放来修复它
    猜你喜欢
    • 2010-10-12
    • 2021-03-25
    • 1970-01-01
    • 2018-01-15
    • 2021-07-21
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多