3rd-Party On-Device Models

Sep 19 2024 · Swift 6.0, iOS 18.0, Xcode 16.0

Lesson 02: Converting Models with Core ML Tools

Demo

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the last lesson, you converted a model using CoreML Tools. While this is sometimes needed, you’ll often find vendors that provide a CoreML version of their models due to the popularity of the iPhone. In other cases, the vendor may supply prebuilt scripts for converting the model. You’ll work with such a model named YOLOv8 from Ultralytics. This library performs a number of tasks, including:

pip install ultralytics

Running a Model

As you installed the library using the pip package manager, you should expect Ultralytics to support Python. Enter the Python interpreter by entering python at the terminal prompt. The first thing you need is the Ultralytics YOLO library. Enter the following at the prompt:

from ultralytics import YOLO
model = YOLO("yolov8x-oiv7.pt")
model("sample-image.jpg")

Converting a Model

From the previous section, you should remember the .pt extensions identify a PyTorch model. For your iOS app, you want a CoreML model. Fortunately, you can get a .mlpackage CoreML file. Enter the following at the Python prompt:

model.export(format="coreml", nms=True)
exit()

Adding CoreML Models to a Project

Now that you have a model, you’ll integrate this model into your iOS app. Find and open the Starter project for this lesson. Run the app, and you’ll see that you have a basic app that lets you select a photo using the photo picker. When you select a photo, it’ll show in the view. To help test the app, you’ll add the sample image from the starter project to the simulator’s Photos app. Open the folder with the sample image in Finder. Now drag the sample-image.jpg file on top of the simulator. The simulator will add it to its Photos collection.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 1 Next: Instruction 2