Run a Core ML image model with Vision
Wiring Core ML into Vision for image inference.
Add the .mlmodel so Xcode generates a class, wrap it in a VNCoreMLModel, run a VNCoreMLRequest via a VNImageRequestHandler, read results off the main queue.
WHAT THIS TESTS This checks that you know how Core ML and Vision compose for image tasks, and that Vision handles the preprocessing that developers otherwise get wrong.
A GOOD ANSWER COVERS You add the .mlmodel file to the Xcode project; Xcode compiles it and generates a strongly typed Swift class for the model. For image inputs the idiomatic path is through Vision: create a VNCoreMLModel by wrapping the generated model, then a VNCoreMLRequest with that model and a completion handler. To run it on an image, create a VNImageRequestHandler from the input's CGImage, CIImage, or CVPixelBuffer along with orientation, and call perform with the request. Vision automatically scales and crops the image to the model's expected input size according to request.imageCropAndScaleOption and handles color formats, so you do not resize pixels manually. In the completion handler you read the results, typically an array of VNClassificationObservation sorted by confidence, and update UI on the main queue.
COMMON WRONG ANSWERS Manually resizing or reformatting the pixel buffer instead of letting Vision do it. Running the request on the main thread, blocking the UI. Forgetting to pass the correct CGImagePropertyOrientation, producing wrong predictions on rotated photos. Calling the Core ML prediction directly with a mismatched pixel buffer.
LIKELY FOLLOW-UPS How to batch or run on a background queue and dispatch results back. The difference between VNClassificationObservation and VNRecognizedObjectObservation for detectors. How MLModelConfiguration can pin compute to CPU or Neural Engine. Updating models over the air with on-device model deployment.
ONE CONCRETE EXAMPLE You wrap the generated class: let model = try VNCoreMLModel(for: MyClassifier(configuration: .init()).model). You build let request = VNCoreMLRequest(model: model) { req, _ in let top = (req.results as? [VNClassificationObservation])?.first; DispatchQueue.main.async { self.label.text = top?.identifier } }. On a background queue you create let handler = VNImageRequestHandler(cgImage: image.cgImage!, orientation: .up) and call try handler.perform([request]). Vision resizes the photo to the model's input, runs inference, and your handler shows the top label.
Interview question
When running a Core ML image classifier through Vision, why is wrapping it in a VNCoreMLModel and using VNImageRequestHandler preferred over calling the model's prediction directly?
- a.It encrypts the model file at rest automatically
- b.Vision handles required scaling, cropping, and color conversion to the model's input formatCorrect
- c.It removes the need to add the .mlmodel to the project
- d.It runs the model exclusively on the Neural Engine for speed
Why? this is the answer
Vision normalizes the image to the model's expected input size and pixel format and respects orientation, eliminating error-prone manual preprocessing. It does not auto-encrypt the model or remove the need to bundle it.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
- #ios
- #coreml
- #vision
- #machine-learning
- #image
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles