TensorFlowLite error interpreter.set_tensor()

Viewed 1250

My keras model:

model = Sequential()
model.add(keras.layers.InputLayer(input_shape=(1134,), dtype='float64'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(512, activation='relu'))
model.add(keras.layers.Dropout(0.35))
model.add(Dense(3, activation='softmax'))

After training, i convert model to tflite

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [tf.lite.OpsSet.SELECT_TF_OPS] <-- without this I will get an error
tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

Then i want to test model:

interpreter = tf.lite.Interpreter(model_path="/content/model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']
inp = np.expand_dims(X[0], axis=0)
interpreter.set_tensor(input_details[0]['index'], inp) <-- in this line i get error```

Error:

ValueError: Cannot set tensor: Got value of type NOTYPE but expected type FLOAT64 for input 0, name: input_1 ```

1 Answers

Try this:

interpreter = tf.lite.Interpreter(model_path="/content/model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_shape = input_details[0]['shape']
inp = np.expand_dims(X[0], axis=0)

inp = inp.astype(np.float64) # This was missing

interpreter.set_tensor(input_details[0]['index'], inp)
Related