【问题标题】:How to initialise a multi-dimensional MLMultiArray如何初始化多维 MLMultiArray
【发布时间】:2021-08-22 11:27:50
【问题描述】:

我正在托管一个 CoreML 模型,该模型将排名第 4 的 MLMultiArray 作为其输入:

我的输入数据如下:

let inputArray: [[[Float]]]

但我不知道如何从中初始化 MLMultiArray。以下是一些尝试:

MLMultiArray(inputArray) // error: No exact matches in call to initializer
let multiArray = try! MLMultiArray(shape: [0, 128, 128, 1], dataType: .float32)
multiArray[0,1] = inputArray[0] // Cannot assign value of type '[[Float]]' to subscript of type 'NSNumber'
memset(multiArray.dataPointer, 0, 128*128) // EXC_BAD_ACCESS (code=1, address=0x0)

如何初始化这样的 MLMultiArray?

【问题讨论】:

    标签: swift machine-learning coreml onnx coremltools


    【解决方案1】:

    将其初始化为

    let input = try! MLMultiArray(shape: [1, 128, 128, 1], dataType: .float64)
    

    是正确的(请注意,我在 coremltools 中编辑了输入形状)。

    填充应该如下:

    for batchIndex in 0..<inputArray.count {
        for heightIndex in 0..<inputArray[0].count {
            for widthIndex in 0..<inputArray[0][0].count {
                input[[batchIndex, heightIndex, widthIndex, 0] as [NSNumber]] = (inputArray[batchIndex][heightIndex][widthIndex]) as NSNumber
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-11
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多