How to include a Core ML model in a Swift Package

Photo by Lukasz Szmigiel on UnsplashWhen I started creating my first ever machine learning app, I decided to use CoreML instead of Tensorflow or PyTorch just because of the convenience. Not only was I able to understand the training a lot better, but a…


This content originally appeared on Level Up Coding - Medium and was authored by Niklas Büchner

Photo by Lukasz Szmigiel on Unsplash

When I started creating my first ever machine learning app, I decided to use CoreML instead of Tensorflow or PyTorch just because of the convenience. Not only was I able to understand the training a lot better, but also the deployment: Just throw it into Xcode and use the class which is created for the model. That’s it.

However, factoring out your code and model into a library — a Swift Package — removes the convenience of dragging and dropping the model into Xcode. Xcode can’t handle this for some reason… ? So what do we need to do?

1.) Compiling the model yourself

When you add the .mlmodel file into your Swift Package nothing will happen. You will need to compile the model yourself. This can be done with the coremlcompiler which Xcode bundles with its command line tools. This will create a my-model.mlmodelc directory.

Important: The created artifact must be located in your Sources/MyPackageName folder since you will not be able to include it into your package otherwise.

2.) Add it as a resource to your library

Unfortunately the compiler is not smart enough to include your model right away. You’ll also need to add the resources into your Package.swift. This should be in your targets > your-target >resources as shown below:

Important: Firstly, you need to add the resource to every target individually. Secondly, the path in copy is relative to your Sources/MyPackageName source root folder.

That’s all. ?

Good luck with your project. ?


How to include a Core ML model in a Swift Package was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Niklas Büchner


Print Share Comment Cite Upload Translate Updates
APA

Niklas Büchner | Sciencx (2021-03-19T16:09:09+00:00) How to include a Core ML model in a Swift Package. Retrieved from https://www.scien.cx/2021/03/19/how-to-include-a-core-ml-model-in-a-swift-package/

MLA
" » How to include a Core ML model in a Swift Package." Niklas Büchner | Sciencx - Friday March 19, 2021, https://www.scien.cx/2021/03/19/how-to-include-a-core-ml-model-in-a-swift-package/
HARVARD
Niklas Büchner | Sciencx Friday March 19, 2021 » How to include a Core ML model in a Swift Package., viewed ,<https://www.scien.cx/2021/03/19/how-to-include-a-core-ml-model-in-a-swift-package/>
VANCOUVER
Niklas Büchner | Sciencx - » How to include a Core ML model in a Swift Package. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/19/how-to-include-a-core-ml-model-in-a-swift-package/
CHICAGO
" » How to include a Core ML model in a Swift Package." Niklas Büchner | Sciencx - Accessed . https://www.scien.cx/2021/03/19/how-to-include-a-core-ml-model-in-a-swift-package/
IEEE
" » How to include a Core ML model in a Swift Package." Niklas Büchner | Sciencx [Online]. Available: https://www.scien.cx/2021/03/19/how-to-include-a-core-ml-model-in-a-swift-package/. [Accessed: ]
rf:citation
» How to include a Core ML model in a Swift Package | Niklas Büchner | Sciencx | https://www.scien.cx/2021/03/19/how-to-include-a-core-ml-model-in-a-swift-package/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.