0

I have a CoreML model (created using TF and converted to CoreML). For it

input is: MultiArray (Double 1 x 40 x 3)
output is: MultiArray (Double)

I will be getting these [a,b,c] tuples and need to collect 40 of them before sending into to the model for prediction. I am looking through the MLMultiArray documentation and am stuck. May be because of I am new to Swift.

I have a variable called modelInput that I want to initialize and then as the tuples come in, add them to the modelInput variable.

modelInput = MLMultiArray(shape:[1,40,3], dataType:MLMultiArrayDataType.double))

The modelInput.count is 120 after this call. So I am guessing an empty array is created.

However now I want to add the tuples as they come in. I am not sure how to do this. For this I have a currCount variable which is updated after every call. The following code however gives me an error.

"Value of type 'UnsafeMutableRawPointer' has no subscripts"

    var currPtr : UnsafeMutableRawPointer = modelInput.dataPointer + currCount
    currPtr[0] = a
    currPtr[1] = b
    currPtr[2] = c
    currCount = currCount + 3

How do I update the multiArray?

Is my approach even correct? Is this the correct way to create a multi array for the prediction input?

I would also like to print the contents of the MLMultiArray. There doesn't appear to be any helper functions to do that though.

2 Answers 2

1

You can use pointers, but you have to change the raw pointer into a typed one. For example:

let ptr = UnsafeMutablePointer<Float>(OpaquePointer(multiArray.dataPointer))
ptr[0] = a
ptr[1] = b
ptr[2] = c
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Matthijs. I have an array of FP 32 values that I have loaded into my app using a JSON file. The shape of the array is [1, 12, 128, 192]. Then I have created an MLMultiArray as follows: let data = try? MLMultiArray(shape: [1, 12, 128, 192], dataType: .int32) So, how do I get the data into this MLMultiarray now ? Can you please provide a code example or point me in the right direction? Love your CoreML book btw. Thanks
Since the data is already in the correct order in the array, only the datatype is different. So you can simply do a for loop to loop through the elements in the array, convert them to Int32 and put it into the MLMultiArray.
Hi Matthijs, thanks for the tip. I tried this code: let yHat = try? MLMultiArray(shape: [1, 12, 128, 192], dataType: .float32) for i in 0..<12{ for j in 0..<128{ for k in 0..<192{ yHat![[0 as NSNumber, i as NSNumber, j as NSNumber, k as NSNumber]] = (self.inputData[0][i][j][k]) as NSNumber } } } but this looks like giving a wrong output i.e. a single big array of values rather than an array of arrays.
0

I figured it out. I have to this --

    modelInput[currCount+0] = NSNumber(floatLiteral: a)
    modelInput[currCount+1] = NSNumber(floatLiteral: b)
    modelInput[currCount+2] = NSNumber(floatLiteral: c)

I cannot use the raw pointer to access elements.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.