0

I'm trying to convert a LSTM model from TensorFlow into ONNX. The code for generating data for TensorFlow model training is as below:

def make_dataset(self, data):
  data = np.array(data, dtype=np.float32)
  ds = tf.keras.utils.timeseries_dataset_from_array(
        data=data,
        targets=None,
        sequence_length=self.total_window_size,
        sequence_stride=1,
        shuffle=True,
        batch_size=32, )
    
  ds = ds.map(self.split_window)

The model training code is actually from the official tutorial. Then after conversion to ONNX, I try to perform prediction as follows:

import onnx
import onnxruntime as rt
from tf_lstm import WindowGenerator
import tensorflow as tf

wide_window = WindowGenerator(
    input_width=24, label_width=24, shift=1,
    label_columns=['T (degC)'])

model = onnx.load_model('models/onnx/tf-lstm-weather.onnx')
print(model)

sess = rt.InferenceSession('models/onnx/tf-lstm-weather.onnx')
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name

pred = sess.run([label_name], {input_name: wide_window.test})[0]

But it throws this error:

RuntimeError: Input must be a list of dictionaries or a single numpy array for input 'lstm_input'.

I tried to convert wide_window.test into numpy array and use it instead as follows:

test_data = []
test_label = []

for x, y in wide_window.test:
    test_data.append(x.numpy())
    test_label.append(y.numpy())

test_data2 = np.array(test_data, dtype=np.float)
pred = sess.run([label_name], {input_name: test_data2})[0]

Then it gives this error:

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (219,) + inhomogeneous part.

Any idea?

user2552845
  • 45
  • 1
  • 5

1 Answers1

0

That's a numpy error. Each row you add to the input array has to have the same number of elements.

setting an array element with a sequence requested array has an inhomogeneous shape after 1 dimensions The detected shape was (2,)+inhomogeneous part

Scott McKay
  • 190
  • 1
  • 8