Search code examples
swiftcoremlapple-vision

How to create MLFeatureProvider class for vision framework


I am new to CoreML, and am having difficulties with turning a MLMultiArray (named modelInput) into the required type MLFeatureProvider to feed as a parameter when using myMLModel.prediction(from: modelInput). The error reads:

Argument type 'MLMultiArray' does not conform to expected type 'MLFeatureProvider'

From what I've read, I believe I have to create a class that subclasses 'MLFeatureProvider' that allows me to initialize modelInput as an 'MLFeatureProvider'. But I'm stuck on how to do this.

Are these files generated by Xcode, as suggested by this article? Or must I create these on my own?

Any input is appreciated.

//function inside of Predictor class

func makePrediction(){

let model: MLModel = configureModel(url: url)

let poseMultiArrays = [MLMultiArray] = getPoseMultiArrays()


let modelInput = MLMultiArray(concatenating: poseMultiArrays, axis: 0, dataType: .float)


//Perform prediction

var prediction: MLFeatureProvider?

do{
   
   prediction = try? model.prediction(from: modelInput) //< The error occurs here

}catch{print(error)}

}

Solution

  • You can certainly make an MLFeatureProvider subclass, but you don't need to.

    First off, your code snippet is not using the Vision framework but Core ML. Xcode automatically generates a class for you that creates the MLFeatureProvider. It's a good idea to use that class instead of using the MLModel directly, since the automatically generated class hides all the boilerplate from you.

    If you still want to create your own MLFeatureProvider, the easiest solution is to first make an MLFeatureValue object containing the MLMultiArray, and then pass that into an MLDictionaryFeatureProvider.