tensorflow lite inference returns all nan

Issue

So, i decided to try out tensorflow lite. i created my tensorflow model:

import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt
tf.__version__

output:

'2.3.0'

loading dataset

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

building model

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10)
])

predictions = model(x_train[:1]).numpy()
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test,  y_test, verbose=2)

this gives

[nan, 0.09799999743700027]

the loss during entire training remains ‘nan’
…now i converted the model to tflite model

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS # enable TensorFlow Lite ops.
]
tflite_model = converter.convert()

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

Now the inference: i used a random image from test set

image = np.array([x_test[3]], dtype=np.float32)

interpreter = tf.lite.Interpreter("model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()[0]
_, height, width, = input_details['shape']
tensor_index = input_details['index']
interpreter.set_tensor(tensor_index, image)
interpreter.invoke()
output = interpreter.tensor(interpreter.get_output_details()[0]['index'])
output()

this gives output:
array([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan]], dtype=float32)

I have tried a lot of different things such as putting in function block, np.squeeze and running on different machine but the output remains nan. any ideas where am I going wrong?

Solution

It seems there is an issue in your execution. Now i am able to execute training with out any issues in Tensorflow 2.5 as shown below

import numpy as np
import tensorflow as tf
print(tf.__version__)
from matplotlib import pyplot as plt

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0


model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10)
])

predictions = model(x_train[:1]).numpy()
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test,  y_test, verbose=2)

converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS # enable TensorFlow Lite ops.
]
tflite_model = converter.convert()

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

image = np.array([x_test[3]], dtype=np.float32)

#interpreter = tf.lite.Interpreter("model.tflite") 
interpreter = tf.lite.Interpreter("newmodel.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()[0]
_, height, width, = input_details['shape']
tensor_index = input_details['index']
interpreter.set_tensor(tensor_index, image)
interpreter.invoke()
output = interpreter.tensor(interpreter.get_output_details()[0]['index'])
output()

Output:

2.5.0
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2971 - accuracy: 0.9138
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.1432 - accuracy: 0.9577
Epoch 3/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1080 - accuracy: 0.9682
Epoch 4/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0865 - accuracy: 0.9731
Epoch 5/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0739 - accuracy: 0.9769
313/313 - 0s - loss: 0.0806 - accuracy: 0.9741
[0.0806254968047142, 0.9740999937057495]

INFO:tensorflow:Assets written to: /tmp/tmpqyhpmxnb/assets


array([[10.962509  , -9.535783  , -3.5547743 , -9.498501  ,  0.26378444,
        -3.641434  , -0.20065163,  1.7456708 , -6.428345  , -2.605583  ]],
      dtype=float32)

Answered By – TFer2

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published