Using MLMultiArray as input of an mlmodel in Xcode

Hi,

I'm struggling to implement my model in Xcode using CoreML tools.

The implemented model looks like this :


The code of the viewController looks like this :


let model=blackjack()
let mlMultiArray = try? MLMultiArray (shape:[13,1,1],dataType : MLMultiArrayDataType.double)  
for (index,element) in vecteurCarte.enumerated() {
  print(type(of: element))
  mlMultiArray![index] = NSNumber(floatLiteral: element)
  }

let i = blackjackInput(cardPlayed: mlMultiArray!)
let prediction = try? model.prediction(input : i)
print("prediction label: ")
print(prediction?.classLabel ?? "No prediction possible")
print("probability per label :")
print(prediction?.output ?? "No prediction possible")
The error I get using this is : [coreml] Failure verifying inputs.

Any idea what I'm doing wrong? Thanks in advance for your help.


Alex

Replies

Hey Alex,

I found this example!, I hope help you.


Code Block
import Foundation
import CoreML
class HeightWeightModelWrapper {
enum *** {
case Female
case Male
}
let cmToInch = Float(0.393701)
let poundToKilo = Float(0.453592)
let model = HeightWeightExtended_model()
func predictHeight(inches: Float, ***: ***) -> Float {
do {
let mlMultiArrayInput = try? MLMultiArray(shape:[3], dataType:MLMultiArrayDataType.double)
mlMultiArrayInput![0] = NSNumber(floatLiteral: Double(inches))
switch *** {
case .Female:
mlMultiArrayInput![1] = NSNumber(floatLiteral: Double(1))
mlMultiArrayInput![2] = NSNumber(floatLiteral: Double(0))
case .Male:
mlMultiArrayInput![1] = NSNumber(floatLiteral: Double(0))
mlMultiArrayInput![2] = NSNumber(floatLiteral: Double(1))
}
let heightWeightExtended_modelOutput = try model.prediction(input: HeightWeightExtended_modelInput(matrixHeightFemaleMale: mlMultiArrayInput!))
return heightWeightExtended_modelOutput.weight[0].floatValue
}
catch let error {
print(error)
}
return -1
}
func predictHeight(cm: Float, ***: ***) -> Float {
return predictHeight(inches: cm * cmToInch, ***: ***) * poundToKilo
}
}


Robert
🤖