一些机器学习模型需要多个输入。
我们如何将此类模型转换为 CoreML?

方法
假设我们用两个输入 x 和 y 转换模型。
import torch
import coremltools as ct
traced_model = torch.jit.trace(model, (x,y)) # 转换为 torchscript
mlmodel = ct.convert(traced_model, inputs=[ct.TensorType(shape=x.shape),ct. TensorType(shape=y.shape)]) # 转换为 coreml
在 Swift 中的用法
在 Swift 中运行转换后的模型。
do {
let model = try myModel().model
let input = myModelInput(x: x, y: y) // 在这种情况下,x 和 y 是 MLMultiArray。
let out = 尝试model.prediction(from: input)
} catch let error {
print (error)
}
这篇博客介绍了如何将具有多个输入的Pytorch机器学习模型转换为CoreML模型,以便在SwiftUI应用中使用。详细讲解了转换过程和在Swift中运行此类模型的方法。
350

被折叠的 条评论
为什么被折叠?



