How to Get The .Mlmodel to Be Used in Swift Playground

How do I get the .mlmodel to be used in Swift Playground?

Playground doesn't recognize the .mlmodel file by default, so we need an iOS Project to help us find the compiled mlmodelc folder, and copy the mlmodelc folder to playground's resource folder, and the auto generated helper class to playground's source folder and change it to public. Then it works!

Try this project:
https://github.com/DocRace/AnimalClassifier-Swift-Playground

How To Get CoreML In Pure Playground Files

The compiler raises this error, because it cannot find a declaration of the class Inceptionv3, that you try to instantiate an object of.

This class is automatically created for you as long as you have a regular Xcode project. If you want to work with it inside a Swift playground, you will need to add this file manually:

First, create a regular Xcode project (an iOS app for example) and integrate your mlmodel there. Xcode will then generate an interface for that model automatically. These are exactly the files that are missing in your project and contain the definition of the Inceptionv3 class.

The same problem has been asked and answered here. There you can also find an image showing how to find the automatically generated classes.

How To Get Bounding Box Data For Created mlmodel With Playground

It can not be done using CreateML.
I did not do it yet but it is said a model with bounding data could be created with Turi Create.

Is there a way to make CoreML model available for iOS11+ at source level

Upd. 07/25/17: Apple just have introduced new API for compiling models on device. This means, that you can avoid steps 1-4 now.

  1. (optional) Switch to Xcode beta sudo xcode-select --switch /Applications/Xcode-beta.app/Contents/Developer.
  2. Compile your model: xcrun coremlcompiler compile /path/to/MyModel.mlmodel /path/to/output/folder.
  3. Put compiled model folder MyModel.mlmodelc into your app bundle.
  4. Add auto-generated swift model class (MyModel.swift) to your project manually and annotate it with @available(iOS 11.0, *).
    How to find model class
  5. Load and initialize your model:

    let path = Bundle.main.path(forResource: "MyModel", ofType: "mlmodelc")

    let url = URL(fileURLWithPath: path!)

    let model = try! MyModel(contentsOf: url)

Warning: I haven't tried to upload such app to the AppStore.
I’ve tried it on my test device, and it works, I’m just not sure if it continues working after releasing to the Appstore.

How to get the confidence of a prediction from a MLModel (Machine Learning Model) in Swift 5

When you write,

try DogClassiferModel().prediction(text: "value")

what is returned is a DogClassiferModelOutput object. If your model's output is named confidence, you can get it by writing:

if let output = try DogClassiferModel().prediction(text: "value") {
print(output.confidence)
}

However, classifier models in Core ML are usually treated in a special way. They can return the label of the highest scoring class, or a dictionary containing the probabilities for all the labels.

The best way to find out how this works for your model, is to look at the mlmodel file in Xcode, click the arrow to go the automatically generated source code file, and look for the "Output" class. This will have one or more properties that you can access (like shown in the example above).

CoreML MLModel prediction progress

AFAIK there is no API for this.

If your model takes so long that users have to wait for it, it can be argued the model is too large.

Assuming that the model doesn't take 10s of seconds, putting up an indeterminate waiting animation is the best you can do.



Related Topics



Leave a reply



Submit