我正在体验 CoreML 的项目能力。这是我设法做的:
在运行时编译它:
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!)
Run Code Online (Sandbox Code Playgroud)现在,我想用我的模型进行预测。我尝试在示例应用程序中直接嵌入 .mlmodel 文件,这允许 XCode 在构建时创建包装类来实例化输入:
let multiArr = try? MLMultiArray.init(shape: [1], dataType: .double)
let input = classifier1Input(input: multiArr!)
let output = try? model.prediction(input: input)
Run Code Online (Sandbox Code Playgroud)
但是因为我是在运行时从服务器下载文件,所以我无权访问这种包装类。
let …Run Code Online (Sandbox Code Playgroud) 我想知道是否有解决方案可以在 Linux 服务器上将 .mlmodel 编译为 .mlmodelc ?
我知道这可以用
xcrun coremlcompiler compile /path/to/MyModel.mlmodel /path/to/
Run Code Online (Sandbox Code Playgroud)
但 xcrun 不适用于 Linux。最好的解决方案是什么?我是否被迫在应用程序内构建它?
let compiledModelUrl = try? MLModel.compileModel(at: destinationFileUrl)
Run Code Online (Sandbox Code Playgroud)
我应该为此使用 OSX 服务器吗?