2
votes

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
Please clarify what is inside of X[0] - Alex K.
np.array consist of 1134 np.float64 numbers - capybar133213
Just in case check out inp to make sure it has floats inside. Also what is yours input_shape ? - Alex K.
Inside the inp are indeed float64, I checked this. input_shape based on the documentation is - Shape tuple (not including the batch axis), or TensorShape instance (not including the batch axis). - capybar133213
Then everything should work .) But set_tensor() makes a copy of data, maybe you have some issues during this. Try to use tensor strategy. - Alex K.

1 Answers

0
votes

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)