Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

ios - My coremlmodel gives different output on swift

I create my MLMultiArray like this

var mlInput = try? MLMultiArray(shape: [1,2,12,120], dataType: MLMultiArrayDataType.float32)

and fill this with values which I get from json file like below

mlInput![0 * 12 * 120 + 0 * 120 + 5] = value as! NSNumber

basically I take index 0 over 2 index 0 over 12 and index 5 over 120

and giving it to model like below

let obj = try! mlModel.prediction(input: ml3dmodelInput(input: mlInput))

and get the result with these steps you see. It gives wrong prediction output. I did same things in python to test coremlmodel it did work well with python. I just did same steps fill the array with values from json giving it to model and get the output. What did I do wrong?

question from:https://stackoverflow.com/questions/65898372/my-coremlmodel-gives-different-output-on-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use the array's strides to calculate the indices.

If the array has shape [A, B, C, D] then the correct index is:

a * strides[0] + b * strides[1] + c * strides[2] + d * strides[3]

However, you can also simply do:

mlInput[[a, b, c, d] as [NSNumber]] = value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...