Search code examples
swiftcoremlcoremltools

How to use CoreML model output MLMultiArray


I need to parse the results in the output of prediction from a CoreML model.

I can see that the type of output is MLMultiArray, but when I use the type within my code like this:

let a = MultiArray<Float>(result.transpose_1_tmp_0)

It always throws an error:

Cannot find 'MultiArray' in scope

Anyone has any idea?


Solution

  • MultiArray is not a built-in datatype. Use the variable name:

    var result: MLMultiArray //Your populated array
    let a = result[[z, y, x] as [NSNumber]].floatValue
    

    Also you might want to check out Swiftier MultiArray. Follow the CoreMLHelpers install instructions.

    EDIT:

    MLMultiArray is part of the CoreML framework. To use it, you must import the framework like this:

    import CoreML