My keras model with input shape (none, 40 [timesteps], 12 [features]) looks like this:
model = Sequential([
Conv1D(nodes_per_layer, filter_length, subsample_length=2, activation='relu', input_shape=(timesteps, data_dim), name='accelerations'),
Conv1D(nodes_per_layer, filter_length, subsample_length=1, activation='relu'),
LSTM(nodes_per_layer, return_sequences=True),
LSTM(nodes_per_layer, return_sequences=False),
Dropout(dropout),
Dense(num_classes),
Activation('softmax', name='scores'),
])
After converting it to .mlmodel, I add it to my XCodeProject:
I then try to do inference and get prediction scores:
func makePredictionRequest(currentScaledMotionArrays: [[Double]]) {
let data = _currentScaledMotionArrays.reduce([], +) //result is of type [Double] with 480 elements
do {
let mlMultiArray = try MLMultiArray(shape:[40,12], dataType:MLMultiArrayDataType.double)
for (index, element) in data.enumerated() {
mlMultiArray[index] = NSNumber(value: element)
}
let input = PredictionModelInput(accelerations: mlMultiArray)
let predictionOutput = try _predictionModel.prediction(input: input)
}
catch {
print(error.localizedDescription)
}
}
But the predictionModel.prediction(input: input) method always fails and throws the following error:
"The model expects input feature lstm_1_h_in to be an array, but the input is of type 0."
So hidden states of the lstm layers need to be initialized. I don't know if this behavior is expected, since I never faced the same problem before. Neither while doing inference in keras itself or with google cloud ml. I also don't know the initial values, which are usually chosen for inference. Maybe just arrays of zeros? Did anyone face a similar problem?
The .mlmodel file could be found here.