unable to extract model type swift - ios

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!

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.

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)

Related

SwiftUI preview crash after modify core data entity's attribute

Prior to this issue, the preview is running fine. However, after I have changed the type of the attribute (e.g. from String to Float) inside a .xcdatamodeld file and modify the manual definition file accordingly, which looks similar to this
public class test01: NSManagedObject, Identifiable {
#NSManaged public var var01: String
...
}
The simulation crashes on first few tries but works again after I removed the application inside the simulation iPhone. But then when I tried to use the preview, it always show an error tab saying that it always crashed in updating the view, even the simplest starter file, "Hello World", given when creating the SwiftUI preview file.
Application Specific Information:
Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model." UserInfo={destinationModel=() isEditable 0
At this point I could not find the solution, any idea on this bug/problem?
Try deleting the preview canvas simulators as well. They exist in:
~/Library/Developer/Xcode/UserData/Previews/Simulator Devices/
The preview canvas probably has the old version of the data model.
You may need to do this command at command line:
killall -9 com.apple.CoreSimulator.CoreSimulatorService
And restart XCode as well.

"help to fix ' error: unable to read property list from file: info.plist

I added Admob Google extension in to my Game Maker 2 project, when I try to test it on xcode for iOS, I am getting the error from xcode : unable to read property list from file: ...
Tried to fiddle around with the build settings in Xcode for plist to make it XML/Binary and same as input but nothing helped.
Made sure that the extensions in game maker have IOS enabled
Tried to clean build folder
Changed Locations: of Derived data to Relative
Looked for similar problems on google and stack overflow, but nothing exactly with the same issue
Below is the code given by Gamemaker
if os_type == os_ios
{
ads_app_id = "ca-app-pub-4724502015965127~5126911475";
banner_id = "ca-app-pub-4337965814269841/5893054134";
interstitial_id = "ca-app-pub-4724502015965127/8852681295";
rewarded_id = "ca-app-pub-4724502015965127/3717109960";
}
else
{
ads_app_id = "ca-app-pub-4337965814269841~8766033395";
banner_id = "ca-app-pub-4337965814269841/5893055122";
interstitial_id = "ca-app-pub-4337965814269841/5893055258";
rewarded_id = "ca-app-pub-4337965814269841/5893055399";
}
GoogleMobileAds_Init(interstitial_id, ads_app_id);
GoogleMobileAds_LoadInterstitial();
interstitial_loaded = false;
error:
unable to read property list from file:
/Users/faisalnaamani/GameMakerStudio2/iOS/GMS2IOS/Pixel_Bricks_iOS/Pixel_Bricks_iOS/Pixel_Bricks_iOS/Supporting
Files/Pixel_Bricks_iOS-Info.plist: The operation couldn’t be
completed. (XCBUtil.PropertyListConversionError error 1.)
The error is happening at Build time
I opened the plist file with ATOM, the one generated by GameMaker Studio when I noticed xcode could not read it. I noticed a bit of code that was causing an error and when I deleted it xcode was able to build. The code is below:
<key>NSAppTransportSecurity</key>\n\r<dict>\n\r
<key>NSAllowsArbitraryLoads</key>\n\r <true/>\n\r
<key>NSAllowsArbitraryLoadsForMedia</key>\n\r <true/>\n\r
<key>NSAllowsArbitraryLoadsInWebContent</key>\n\r <true/>\n\r</dict>
But I am not sure how to fix it to make it look correct. I am afraid if I delete it somehow the code will not be correct. Does anyone have a solution? And how would I fix it from GameMaker.
As Faisal suggested, the file cannot be read if it's malformed.
Trying to open the file with Xcode helped me find the problem: a syntax error. Xcode was very explicit about where the error might be: at line 32.
After fixing the error in another IDE I was able to open the file with Xcode and build/run my app.

Crash when initializing CoreML model: Error Domain=com.apple.CoreML Code=0 "Error in declaring network."

I have an app on the App Store and I'm getting its error logs from Crashlytics. One of the most frequent errors the users get (and the one I failed miserably to reproduce) occurs when initializing a CoreML model in my project. Here is how I initialize the model:
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.")
}
...
}
...
}
The error, as seen on Crashlytics, reads as follows:
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
And the stack trace shows that the error is thrown when executing the guard block. Actually, it goes deeper and shows that the error was thrown inside the static initialization at the top, when calling the initializer. The initializer, together with the whole MobileNet.swift class is automatically generated and looks like this:
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!)
}
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.
One possibility is that the compiled .mlmodelc file is not copied to the bundle somehow, and when trying to initialize the MobileNet object with that URL, we get an uncaught error. Is that even possible?
Any ideas or pointers on this issue is greatly appreciated.
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.
FYI, you can copy this generated file into a new file and use it instead to initialize the model (just rename the classes inside the new file). Then, try changing this line in your new file:
let bundle = Bundle(for: MobileNet.self)
to:
let bundle = Bundle.main
I'm not sure if this will fix your particular problem, but it did for me when I moved the generated file into a Cocoapod
Better to read the errors at the top of the output panel in Xcode. There should be an error something like this which shows the actual error: "A Core ML custom neural network layer requires an implementation named 'scaling' which was not found in the global namespace.
In my case, I had an unsupported layer in the model I used so I need to write MLCustomLayer.
https://developer.apple.com/documentation/coreml/core_ml_api/creating_a_custom_layer

NSUrlErrorDomain can't find the code and how to deal with it anywhere

This is the error code I'm getting when I try to pull data from a server, while debugging on my device through Xcode.
error NSError? domain: "NSURLErrorDomain" - code: 4294966274 0x15d9ae90
What does it mean? How I solve it?
I found this site with a list of enums
https://developer.apple.com/reference/foundation/foundation_constants/1508628-url_loading_system_error_codes
But it doesn't give numbers only the enum strings.

Getting unknown type name on using NSPopUpButton

When I try to use NSPopUpButton, I get a "Unknown type name" while building the application. I imported the Foundation.h and even after that am getting the same error.
Can anyone let me know what might be the issue ?

Resources