小编Cer*_*ise的帖子

动态加载 mlmodel

我正在体验 CoreML 的项目能力。这是我设法做的:

  1. 在 Python 中使用 scikit-learn 创建 .pkl 文件
  2. 使用 coremltools 包将其转换为 .mlmodel 文件
  3. 将其下载到我的 iOS 应用程序
  4. 在运行时编译它:

    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)

ios coreml

5
推荐指数
1
解决办法
1751
查看次数

在Linux服务器上编译mlmodel

我想知道是否有解决方案可以在 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 服务器吗?

ios coreml

3
推荐指数
1
解决办法
2346
查看次数

标签 统计

coreml ×2

ios ×2