3rd-Party On-Device Models

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

Lesson 03: Releasing Third-Party Models in Your App

Demo 2

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

Comparing Models in Your App

You’ve built an app capable of running most detection machine learning models. Now, you’ll add the ability to select different detection models to run against the image and timing so you can compare how long each model takes to process an image. Open ContentView.swift and add the following new state properties to the top of the file after detectedObjects:

@State private var startTime: DispatchTime?
@State private var endTime: DispatchTime?
startTime = DispatchTime.now()
endTime = nil
self.endTime = DispatchTime.now()
if let start = startTime, let end = endTime {
  let elapsedNanoseconds = end.uptimeNanoseconds - start.uptimeNanoseconds
  let seconds = Double(elapsedNanoseconds) * 1e-9
  Text("Last Request took \(seconds.roundTwo) seconds")
}

Adding More Models

First, at the top of ContentView.swift, before the view definition, add the following code:

enum SelectedModel {
  case yolo8xfull
  case yolo8int8
  case yolo8m
  case yolo8n
}
@State private var currentModel: SelectedModel = .yolo8xfull
Picker("Select Model to Use", selection: $currentModel) {
  Text("yolov8x-oiv7").tag(SelectedModel.yolo8xfull)
  Text("yolov8x-oiv7-int").tag(SelectedModel.yolo8int8)
  Text("yolov8m-oiv7").tag(SelectedModel.yolo8m)
  Text("yolov8n-oiv7").tag(SelectedModel.yolo8n)
}
guard let cgImage = cgImage else {
  print("Unable to load photo.")
  return
}

var model: MLModel?
switch currentModel {
case .yolo8xfull:
  model = try? yolov8x_oiv7(configuration: .init()).model
case .yolo8int8:
  model = try? yolov8x_oiv7_int(configuration: .init()).model
case .yolo8m:
  model = try? yolov8m_oiv7(configuration: .init()).model
case .yolo8n:
  model = try? yolov8n_oiv7(configuration: .init()).model
}

guard let model = model,
      let detector = try? VNCoreMLModel(for: model) else {
  print("Unable to load model.")
  return
}
.onChange(of: currentModel) {
  runModel()
}
See forum comments
Cinema mode Download course materials from Github
Previous: Demo 1 Next: Conclusion