Reputation: 293
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.
Upvotes: 0
Views: 1310
Reputation: 7892
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
Upvotes: 1
Reputation: 293
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.
Upvotes: 0