Shaping input for a coremlmodel

288 Views Asked by At

I have a coremlmodel taking an input of shape MultiArray (Float32 67 x 256 x 320) I am having a hard time to shape the input to this model. Currently, I am trying to achieve it like so,

var m = try! MLMultiArray(shape: [67,256,320], dataType: .double)
for i in 0...66{           
    var cost = rand((256,320)) // this is coming from swix [SWIX]
    memcpy(m.dataPointer+i*256*320, &cur_cost.flat.grid , 256*320)
}

I will have to replace the rand with matrices of that size later. I am using this for testing purposes first.

Any pointers on how to mould input to fit the volume would be greatly appreciated..

[SWIX]

1

There are 1 best solutions below

2
On BEST ANSWER

What seems to be wrong in your code is that you're copying bytes instead of doubles. A double is 8 bytes, so your offset should be i*256*320*MemoryLayout<Double>.stride and the amount you're copying should be 256*320*MemoryLayout<Double>.stride.

Note that you can also use the MLMultiArray's strides property to compute the offset for a given data element in the array:

let offset = i0 * strides[0].intValue + i1 * strides[1].intValue + i2 * strides[2].intValue