CoreML error: "Different batch numbers for input features" trying to generate prediction

Hi, I'm working with Xcode 9 on MacOS High Sierra 10.13.5, and Keras 2.1.5. I don't know the CoreML version; I don't see how to find it. CoreML was installed recently, so perhaps it's up to date.


I'm working with an LSTM model which was created using Keras and then exported to CoreML. I've successfully linked the exported model into an iOS app. The app is built successfully and it runs, but when I try to call the prediction method on the LSTM model, I get an error; here are the error messages that are logged:

2018-07-10 16:05:38.434899-0700 mymlmodelapp[81851:444232] [coreml] Different batch numbers for input features.

2018-07-10 16:05:38.435077-0700 mymlmodelapp[81851:444232] [coreml] Failure in resetSizes.

I am having a lot of trouble debugging this. I tried telling Xcode to break at the place that the exception is thrown, but that doesn't have the intended effect. I wish I could look at the source code for CoreML so that I could see what, exactly, is being tested and found erroneous, but I can't find the source code; I suppose it might be proprietary. I looked for posts by others reporting the same problem, and found only these two, which don't have useful answers:

https://forums.developer.apple.com/thread/85737

https://forums.developer.apple.com/thread/85544


Any advice is appreciated. Thanks for your time, I appreciate it.


yours truly, LG


PS. Here is the code. "Foo_Bar" is the name of the input that was assigned when the LSTM model was created. The call to mymodel.prediction throws the CoreML exception with the message "Different batch numbers for input features". The intent here is to get a string from the myInput text field, parse it into numbers, put the numbers into an MLMultiArray, get a prediction, and paste the prediction into the myoutput text field.

   

@IBAction func mybuttonAction(_ sender: Any) {

let s: String? = myinput.text

if (s != nil && s!.count > 0) {

guard let input_data = try? MLMultiArray(shape:[1, 4, 2], dataType:.double) else {

fatalError("Unexpected runtime error. MLMultiArray")

}


let w = s!.split(separator: " ")

let x = w.map { s1 in Double(s1) }


var x8 = [Double](repeating: 0.0, count: 8)

for i in 0..<(x.count > 8 ? 8 : x.count) {

if x[i] != nil {

x8[i] = x[i]!

}

}

for i in 0..

input_data[i] = NSNumber(value: x8[i])

}


let mymodel = myLSTM_regressionModel_sos06252018 ()

do {

et lstm_input = myLSTM_regressionModel_sos06252018Input (Foo_Bar: input_data)

let output_data = try mymodel.prediction (input: lstm_input)

let y = output_data.featureValue (for: "Predictions")

let z = y?.multiArrayValue

myoutput.text = String(z!.count)

}

catch {

myoutput.text = "OOPS: \(error)"

}


}

}

Replies

PPS. Sorry about the formatting. I hope it's clear enough, if not, I apologize.