简体   繁体   中英

Loading mlmodel dynamically

I'm experiencing the capacity of CoreML for a project. Here's what I managed to do :

  1. Creating a .pkl file using scikit-learn in Python
  2. Converting it to a .mlmodel file using coremltools package
  3. Downloading it to my iOS application
  4. Compile it at run time :

     let classifierName = "classifier1" let fileName = NSString(format:"%@.mlmodel",classifierName) let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL! let destinationFileUrl = documentsUrl.appendingPathComponent(fileName as String) let compiledModelUrl = try? MLModel.compileModel(at: destinationFileUrl) let model = try? MLModel(contentsOf: compiledModelUrl!) 

Now, I would like to use my model to make prediction. I tried in a sample app to directly embed the .mlmodel file, which allow XCode to create a wrapper class at build time to instantiate input :

let multiArr = try? MLMultiArray.init(shape: [1], dataType: .double)
let input = classifier1Input(input: multiArr!)
let output = try? model.prediction(input: input)

But because I'm downloading the file from server at run time, I do not have access to this kind of wrapper class.

let predict = model?.prediction(from: <MLFeatureProvider>)

Any ideas ?

Simplest solution: copy that Xcode-generated wrapper class into a Swift file and add it to your project. (Note that this wrapper class also shows how to make an MLFeatureProvider etc.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM