如何在CoreML中初始化MLMultiArray [英] How to initialize a MLMultiArray in CoreML
问题描述
我有一个包含40个数组的数组,这些数组具有12个双重特征,因此类型为[[double]].目前,我正在将此数据发送到Google Cloud ML API以获得相关的预测.
I've got an array of 40 arrays with 12 double features, so the type is [[double]]. Currently I'm sending this data to Google Cloud ML API to get a related prediction.
自从苹果公司最近推出CoreML和coremltools以来,我就将模型从keras转换为.mlmodel,从而避免了数千次Google Cloud api调用,并直接在我的iPhone上进行了推断:
Since Apple recently introduced CoreML and coremltools, I converted my model from keras to .mlmodel to avoid thousand of google cloud api calls and do inference directly on my iPhone:
coreml_model = coremltools.converters.keras.convert(new_Model, input_names=['accelerations'],
output_names=['scores'])
coreml_model.save('PredictionModel.mlmodel')
将模型添加到我的Xcode项目后,它看起来像:
After adding the model to my Xcode Project, it looks like:
我不知道这些其他输入和输出来自何处. 为了获得预测,我需要将12个双精度数组的Array转换为MLMultiArray,但是我不知道该怎么做.有没有人遇到过类似的问题?这是我目前未完成的方法:
I have no idea, where these others inputs and outputs are comming from. To get a prediction, I need to convert my Array of Arrays of 12 doubles to an MLMultiArray, but I don't know how to do this. Has anyone faced a similar problem? Here is my current unfinished approach:
_predictionModel = PredictionModel()
guard let mlMultiArray = try? MLMultiArray(dataPointer: <#T##UnsafeMutableRawPointer#>, shape: <#T##[NSNumber]#>, dataType: <#T##MLMultiArrayDataType#>, strides: <#T##[NSNumber]#>, deallocator: <#T##((UnsafeMutableRawPointer) -> Void)?##((UnsafeMutableRawPointer) -> Void)?##(UnsafeMutableRawPointer) -> Void#>) else {
fatalError("Unexpected runtime error.")
}
guard let predictionOutput = try? _predictionModel.prediction(accelerations: mlMultiArray, lstm_1_h_in: nil, lstm_1_c_in: nil, lstm_2_h_in: nil, lstm_2_c_in: nil) else {
fatalError("Unexpected runtime error.")
}
相关文档可以在此处找到.
推荐答案
我是通过阅读此博客来实现的:)
I achieved it by reading this blog :)
let data = _currentScaledMotionArrays.reduce([], +) //result is of type [Double] with 480 elements
guard let mlMultiArray = try? MLMultiArray(shape:[40,12], dataType:MLMultiArrayDataType.double) else {
fatalError("Unexpected runtime error. MLMultiArray")
}
for (index, element) in data.enumerated() {
mlMultiArray[index] = NSNumber(floatLiteral: element)
}
let input = PredictionModelInput(accelerations: mlMultiArray)
guard let predictionOutput = try? _predictionModel.prediction(input: input) else {
fatalError("Unexpected runtime error. model.prediction")
}
这篇关于如何在CoreML中初始化MLMultiArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!