in SemanticSegmentationSample/SemanticSegmentationSample/DataModel.swift [59:85]
func runModel() async {
try! loadModel()
let clock = ContinuousClock()
var durations = [ContinuousClock.Duration]()
while !Task.isCancelled {
let image = lastImage.withLock({ $0 })
if let pixelBuffer = image?.pixelBuffer {
let duration = await clock.measure {
try? await performInference(pixelBuffer)
}
durations.append(duration)
}
let measureInterval = 100
if durations.count == measureInterval {
let total = durations.reduce(Duration(secondsComponent: 0, attosecondsComponent: 0), +)
let average = total / measureInterval
print("Average model runtime: \(average.formatted(.units(allowed: [.milliseconds])))")
durations.removeAll(keepingCapacity: true)
}
// Slow down inference to prevent freezing the UI
try? await Task.sleep(for: .milliseconds(10))
}
}