Hi,
I've been using CoreML and Vision to do some basic detection/recognition on my images.
Now, making a more complex framework, and allowing users (developers) to easily append multiple models and get results, I've stumbled upon a rather annoying problem:
A Vision request handler, VNImageRequestHandler, returns wrong results when the perform function contains multiple model request (VNCoreMLRequest).
To simplify, let's say that I am trying to get gender, age and emotion of a face in an image. I create one Vision handler and dispatch its perform function in a queue as follow:
DispatchQueue.global(qos: .userInteractive).async { do {
try handler.perform([self.ageClassificationRequest, self.genderClassificationRequest, self.emotionsClassificationRequest])
catch { print(error)}}}Each of the xxxClassificationRequests is a VNCoreMLRequest with a completion handler that checks the observation results (VNClassificationObservation).
This code fails because the observations are exactly the same as the very first (in this case, Age is the first in the array, so Gender and Emotions' observation results will be a duplicate of Age's ones)
Doing this on the other hand solves the problem:
DispatchQueue.global(qos: .userInteractive).async { do {
try handler.perform([self.ageClassificationRequest])
} catch { print(error) } }
DispatchQueue.global(qos: .userInteractive).async { do {
try handler.perform([self.genderClassificationRequest])
} catch { print(error) } }
DispatchQueue.global(qos: .userInteractive).async { do {
try handler.perform([self.emotionsClassificationRequest])
} catch { print(error) } }In other words, dispatching multiple times with the same handler allows the results to be correct.
Now, the question is, why is this behavior acceptable or is it just a bug? Is there a way to ensure correct results, especially that each completion handler is correctly called.
Thank you.