unable to extract model type swift

时间:2018-03-25 18:55:11

标签: ios swift iostream

I'm having a slight issue with CoreML using Swift Playgrounds while attempting to make a gender classifier model. When I add the Gender.mlmodel and Gender.swift to their respective folders within the Playground Page (Resources/ and Sources/), I get a rather peculiar error, shown below:

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=com.apple.CoreML Code=0 "Unable to extract model type from stream in compiled model: Error opening file stream: /var/folders/kt/11b_lk412hv327qjhvgnhvzr0000gn/T/com.apple.dt.Xcode.pg/auxiliarymodules/7C17E9A3-54C7-4828-96BD-00AE07EC7F3E/Gender_Classifier_PageSources.framework/Resources/Gender.mlmodel/coremldata.bin: unspecified iostream_category error" UserInfo={NSLocalizedDescription=Unable to extract model type from stream in compiled model: Error opening file stream: /var/folders/kt/11b_lk412hv327qjhvgnhvzr0000gn/T/com.apple.dt.Xcode.pg/auxiliarymodules/7C17E9A3-54C7-4828-96BD-00AE07EC7F3E/Gender_Classifier_PageSources.framework/Resources/Gender.mlmodel/coremldata.bin: unspecified iostream_category error}: file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.74.1/src/swift/stdlib/public/core/ErrorType.swift, line 181

I've thought that it had an issue accessing the file itself, so I tried using different locations and the absolute path as a String (which returned a value when force-unwrapped), but to no avail. In addition, I tried other models that I know are able to be loaded (SqueezeNet.mlmodel), and that did not work as well, returning a comparable error.

I appreciate any help that anyone would be able to offer, I'm completely perplexed by this issue. Thank you!

2 个答案:

答案 0 :(得分:8)

我找到了解决方法:显然Swift Playgrounds .mlmodel编译为.mlmodelc,从而导致无法读取未编译的模型。对于其他想知道的人,只需运行MLModel.compile(at: URL?)即可将其转换为已编译的格式。

答案 1 :(得分:1)

在Swift 5.2.3中,以下是正确的。

let url = try MLModel.compileModel(at: URL(fileURLWithPath: "<#/path/to/read/model.mlmodel#>"))
let mode = try MLModel(contentsOf: url)