[英]Loading mlmodel dynamically
I'm experiencing the capacity of CoreML for a project. 我正在体验CoreML对项目的能力。 Here's what I managed to do :
这是我设法做的:
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 :
我尝试在一个示例应用程序中直接嵌入.mlmodel文件,它允许XCode在构建时创建一个包装类来实例化输入:
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. 最简单的解决方案:将Xcode生成的包装类复制到Swift文件中并将其添加到项目中。 (Note that this wrapper class also shows how to make an
MLFeatureProvider
etc.) (请注意,此包装类还显示了如何创建
MLFeatureProvider
等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.