Search code examples
swiftmacoscoremlcoremltools

CoreML – How to create a simple MLFeatureProvider class


I have a MLModel that contains one input column called length and one output column called depth.

You give it a length and it predicts a depth.

I know that there are other ways of doing this but this is for the whole purpose of learning CoreML.

I need to build a MLFeatureProvider class to get that length and output a depth.

I am not sure how to create that class because this is the first time I am dealing with CoreML and because, as usual, Apple do not cares to create decent documentation.

So this is what I have

class FeatureProviderX: MLFeatureProvider {
  var featureNames: Set<String> {
    get {
      return  ["length", "depth"]
    }
  }

  func featureValue(for featureName: String) -> MLFeatureValue? {
    guard featureName == "length" else {
      return MLFeatureValue(????????????) \\1
    }
  }

}

I am not sure if this class is remotely correct.

If this is correct, how should \\1 be written?


Solution

  • Matthijs Hollemans was in the right track and gave me clues on how to solve that but, unfortunately his solution was lacking a lot of stuff.

    After a while I have figured out the correct solution...

    class FeatureProviderX: MLFeatureProvider {
    
      var featureNames: Set<String> {
        get {
          return  ["length"]
        }
      }
    
      var sorteio: Double
    
      init(length: Double) {
        self.length = length
      }
    
      func featureValue(for featureName: String) -> MLFeatureValue? {
        if featureName == "length" {
          return MLFeatureValue(double: length)
        }
        return nil
      }
    }