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.





Freelance iOS developer. You can ask me for a job from any country.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

1. What is SwiftUI?

Combine with UIControl

How to Customize SwiftUI Chat Messaging App Powered by Stream

Let’s Get Swifty! Self-Sizing UITableViewCell in iOS/Swift using UITextView & UIImage ⌜Beginner⌟

Rewriting My First SwiftUI App: TabView and SidebarView

Automate Flutter App Deployment on iOS to TestFlight using Fastlane and Semaphore

Stop resolving Merge conflicts in .pbxproj

How to apply a filter to an existing video

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


Freelance iOS developer. You can ask me for a job from any country.

More from Medium

How to create a model based on Emotion Recognition using the LiDAR sensor behind FaceID

ARKit 911 — Image Tracking in Swift Playgrounds

Creating an ARCore powered indoor navigation application in Unity

How to implement GraphQL in your iOS project.