gpt4 book ai didi

ios - 初始化 CoreML 模型时崩溃 : Error Domain=com. apple.CoreML Code=0 "Error in declaring network."

转载 作者:IT王子 更新时间:2023-10-29 05:37:53 31 4
gpt4 key购买 nike

我在 App Store 上有一个应用程序,我从 Crashlytics 获取了它的错误日志。在我的项目中初始化 CoreML 模型时,用户遇到的最常见错误之一(也是我重现失败的错误)发生了。以下是我如何初始化模型:

class VisionManager: NSObject {
/// Prediction model
private static let model = MobileNet()

...

override init() {
super.init()

guard let visionModel = try? VNCoreMLModel(for: VisionManager.model.model) else {
// this case should never happen as we know for sure that the model we are using is an image classification model
fatalError("The CoreML model being used is not compatible with the Vision framework.")
}

...
}

...
}

在 Crashlytics 上看到的错误内容如下:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=com.apple.CoreML Code=0 "Error in declaring network." UserInfo={NSLocalizedDescription=Error in declaring network.}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.65.2/src/swift/stdlib/public/core/ErrorType.swift, line 181

并且堆栈跟踪显示错误是在执行 guard block 时抛出的。实际上,它更深入地表明错误是在调用初始化程序时在顶部的静态初始化内部抛出的。初始化器连同整个 MobileNet.swift 类是自动生成的,如下所示:

init(contentsOf url: URL) throws {
self.model = try MLModel(contentsOf: url)
}

/// Construct a model that automatically loads the model from the app's bundle
convenience init() {
let bundle = Bundle(for: MobileNet.self)
let assetPath = bundle.url(forResource: "MobileNet", withExtension:"mlmodelc")
try! self.init(contentsOf: assetPath!)
}

调用 init(contentsOf url: URL) 方法时抛出错误似乎很明显。但是,由于这是一个生成的文件,我相信我无法解决这个错误。

一种可能性是编译后的 .mlmodelc 文件没有以某种方式复制到包中,并且在尝试使用该 URL MobileNet 对象时,我们得到一个 Uncaught Error 。这可能吗?

非常感谢关于此问题的任何想法或指示。

最佳答案

It seems obvious that the error is thrown calling the init(contentsOf url: URL) method. However, since this is a generated file, I believe there isn't much I can do to tackle this error.

仅供引用,您可以将此生成的文件复制到一个新文件中,并使用它来初始化模型(只需重命名新文件中的类)。然后,尝试更改新文件中的这一行:

let bundle = Bundle(for: MobileNet.self)

到:

let bundle = Bundle.main

我不确定这是否会解决您的特定问题,但当我将生成的文件移动到 Cocoapod 中时它对我有用

关于ios - 初始化 CoreML 模型时崩溃 : Error Domain=com. apple.CoreML Code=0 "Error in declaring network.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47339390/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com