Tensorflow Tensoflow模型转换后输出错误

Tensorflow Tensoflow模型转换后输出错误,tensorflow,keras,tensorflow-lite,Tensorflow,Keras,Tensorflow Lite,我的目标是开发一个可以进行图像分类的应用程序。Keras模型似乎运行良好,但在我尝试转换为Tensorflow Lite后,输出完全错误 这些是模型的层 model = Sequential([ layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)), layers.Conv2D(16, 3, padding='same', activation='relu')

我的目标是开发一个可以进行图像分类的应用程序。Keras模型似乎运行良好,但在我尝试转换为Tensorflow Lite后,输出完全错误

这些是模型的层

model = Sequential([
  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes)
])
# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="/content/tf_lite_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)
这就是我保存然后转换模型的方式

model.save('/content/drive/My Drive/my_model.h5') 

TF_LITE_MODEL_NAME = "tf_lite_model.tflite"
tf_lite_converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = tf_lite_converter.convert()
tflite_model_name = TF_LITE_MODEL_NAME
open(tflite_model_name, "wb").write(tflite_model)
输出如下:

[[ 1.4364377  -0.02920453 -0.149581   -0.7537567 ]]

为什么会出现这个问题

编辑(为模型提供随机输入的代码)


TFLite模型的预测是如何错误的?Python模型的预期结果是什么?我使用随机数据来测试模型的输出,并且输出总是相同的,只有很少的差别(我用生成随机输入的代码编辑了文章)。我还想知道输出是否可能是负的。