Core ML

Here’s a quick setup-guide for using custom Core ML models into your iOS project.

Note

If you haven’t set up the SDK yet, make sure to go through those directions first. You’ll need to add the Core library to the app before using the specific feature or custom model libraries.

For the purpose of this guide, let’s assume you have the following:

  1. A Core ML model file called Digits.mlmodel that recognizes the numbers 0-9 in an image.
  2. You have dragged this model into Xcode and can access the model let model = Digits() in your code.
  3. Your Fritz API key is api-key-12345
  4. Your Fritz model id for the Digits model is model-id-abcde

1. Conform Your Model

In order to expose Fritz functionality to your Xcode generated Digits class, you must conform that class to a Fritz protocol that tells the SDK the about your API key and model id for that model.

Create a new file called Digits+Fritz.swift and conform your class like so:

import Fritz

extension Digits: Fritz.SwiftIdentifiedModel {

    static let modelIdentifier = "model-id-abcde"

    static let packagedModelVersion = 1
}

Create a new extension called Digits+Fritz and conform your class like so:

// Digits+Fritz.h
@import Fritz;

@interface Digits(Fritz) <FritzObjcIdentifiedModel>
@end
// Digits+Fritz.m
#import "Digits+Fritz.h"

@implementation Digits(Fritz)
+ (NSString * _Nonnull)fritzModelIdentifier {
    return @"model-id-abcde";
}

+ (NSInteger)fritzPackagedModelVersion {
    return 1;
}
@end

2. Update Model Usage

By conforming the Digits class to the Fritz protocol we have exposed a .fritz() function which injects an instrumented MLModel into the instance of that class.

Find any calls that look like this:

let model = Digits()

And change them to:

let model = Digits().fritz()

Find any calls that look like this:

Digits *model = [[Digits alloc] init];

And change them to:

Digits *model = [[Digits alloc] init].fritz;

All of your prediction calls stay the same and they will be instrumented by the SDK.

3. Build and run your app

Test out each part of your app that uses the prediction, then look at the Fritz dashboard to see if data is showing up.

Note

You do not have to bundle the mlmodel in your app package. You can download the model after a user installs your app. You may want to do this if you are trying to shrink the size of your initial app download.

Be aware of UX difficulties this may create. You will be unable to run any predictions until the download completes.

Here is an example of how to download the Digits model created above.

First you must generate and include the Digits.swift class file:

xcrun coremlc generate --language Swift Digits.mlmodel .

Then, add that generated file to your app:

Digits.download { url
    guard url != nil else { return }
    self.model = try! Digits(contentsOf: url).fritz()
}