Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to extract model type swift

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!

like image 354
Bradley H Avatar asked Mar 25 '18 18:03

Bradley H


2 Answers

I found the solution to this: apparently Swift Playgrounds does not compile .mlmodel to .mlmodelc, thus causing the failure to read a non-compiled model. For anyone else wondering, just run MLModel.compile(at: URL?) to convert it to the compiled format.

like image 143
Bradley H Avatar answered Oct 17 '22 23:10

Bradley H


In Swift 5.2.3, the following is correct.

let url = try MLModel.compileModel(at: URL(fileURLWithPath: "<#/path/to/read/model.mlmodel#>"))
let mode = try MLModel(contentsOf: url) 
like image 42
Harutaka Matsumoto Avatar answered Oct 17 '22 22:10

Harutaka Matsumoto