Create a machine learning library for iOS

How to create a library with a machine learning model

You can incorporate machine learning functions into your library.

We want to use a machine learning model in a library

There are many useful machine learning models such as image recognition, object detection, segmentation, speech recognition, character recognition, image generation, super-resolution, depth estimation, and image restoration .
We want to put those powers into the library.

When using a machine learning model in an iOS project, CoreMLModel is installed in the project, but files with the extension mlmodel cannot be directly loaded in the library that supports Swift Package Manager.
However, it is troublesome to have the user download only the mlmodel separately and copy it to the project.
Is it possible to incorporate the entire model into the package somehow? .. ..

Compile and bundle to solve

If you compile mlmodel and load a file called mlmodelc and a model reading file of swift into the library, you can use the machine learning model as it is.

How to compile

Open a console and do the following:

xcrun coremlcompiler compile pathToYourModel.mlmodel .xcrun coremlcompiler generate pathToYourModel.mlmodel . --language Swift


Will be generated, so copy it to the Source / (package name) folder of the library package.

Add .mlmodelc file to your target resouces.

name: “MyPackage”,
dependencies: [],
resources: [.process(“yourModel.mlmodelc”)]),

Now you can use this model in the Swift Package library as well.

Call from code

let url = try? Bundle.module.url(forResource: “yourModel”, withExtension: “mlmodelc”)let mlModel = try! MLModel(contentsOf: url!, configuration: MLModelConfiguration())


let mlModel = try! yourModel(configuration:MLModelConfiguration()).model


I’m a freelance engineer.
Work consultation
Please feel free to contact us with a brief development description.

I am making an app that uses Core ML and ARKit.
We send machine learning / AR related information.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store