二元分类中的 Tensorflow Lite 负预测
Tensorflow lite negative predictions in binary classification
我基本上希望通过 pyinstaller 准备我的模型以进行分发。并且由于打包tensorflow导致最终的可执行文件大小约为500mbs。我使用了 tensorflow lite。
现在的问题是,当我将模型转换为 tflite(量化或未量化)时,它会为我输入的任何图像提供以下输出。
array([[-1.3749948e+23]], dtype=float32)
下面是我的模型转换代码
import tensorflow as tf
m = load_model("weights.best.hdf5")
converter = tf.lite.TFLiteConverter.from_keras_model(m)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
下面是测试tflite模型的代码
import tflite_runtime.interpreter as tflite
interpreter = tf.lite.Interpreter(model_content=tflite_quant_model)
interpreter.allocate_tensors()
inputdets = interpreter.get_input_details()
outputdets = interpreter.get_output_details()
import imageio
import cv2
import numpy as np
img = imageio.imread("1 (162).jpg")/256.0
final = cv2.resize(img,(150,150))
input_data = np.array([final],dtype=np.float32)
interpreter.set_tensor(inputdets[0]['index'], input_data)
interpreter.get_tensor(outputdets[0]['index'])
keras模型的输出
数组([[0.9934516]], dtype=float32)
tflite 模型的输出
数组([[-1.3749948e+23]], dtype=float32)
附加信息
在 keras 上训练时的模型准确性
98%
输入详细信息
[{'dtype': numpy.float32,
'index': 0,
'name': 'input_3',
'quantization': (0.0, 0),
'quantization_parameters': {'quantized_dimension': 0,
'scales': array([], dtype=float32),
'zero_points': array([], dtype=int32)},
'shape': array([ 1, 150, 150, 3], dtype=int32),
'shape_signature': array([ 1, 150, 150, 3], dtype=int32),
'sparsity_parameters': {}}]
输出详细信息
[{'dtype': numpy.float32,
'index': 21,
'name': 'Identity',
'quantization': (0.0, 0),
'quantization_parameters': {'quantized_dimension': 0,
'scales': array([], dtype=float32),
'zero_points': array([], dtype=int32)},
'shape': array([1, 1], dtype=int32),
'shape_signature': array([1, 1], dtype=int32),
'sparsity_parameters': {}}]
您需要 运行 interpreter.invoke()
才能阅读结果。 https://www.tensorflow.org/lite/guide/inference#load_and_run_a_model_in_python
按如下方式更新您的代码:
...
...
interpreter.set_tensor(inputdets[0]['index'], input_data)
interpreter.invoke()
result = interpreter.get_tensor(outputdets[0]['index'])
..
..
我基本上希望通过 pyinstaller 准备我的模型以进行分发。并且由于打包tensorflow导致最终的可执行文件大小约为500mbs。我使用了 tensorflow lite。
现在的问题是,当我将模型转换为 tflite(量化或未量化)时,它会为我输入的任何图像提供以下输出。
array([[-1.3749948e+23]], dtype=float32)
下面是我的模型转换代码
import tensorflow as tf
m = load_model("weights.best.hdf5")
converter = tf.lite.TFLiteConverter.from_keras_model(m)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
下面是测试tflite模型的代码
import tflite_runtime.interpreter as tflite
interpreter = tf.lite.Interpreter(model_content=tflite_quant_model)
interpreter.allocate_tensors()
inputdets = interpreter.get_input_details()
outputdets = interpreter.get_output_details()
import imageio
import cv2
import numpy as np
img = imageio.imread("1 (162).jpg")/256.0
final = cv2.resize(img,(150,150))
input_data = np.array([final],dtype=np.float32)
interpreter.set_tensor(inputdets[0]['index'], input_data)
interpreter.get_tensor(outputdets[0]['index'])
keras模型的输出
数组([[0.9934516]], dtype=float32)
tflite 模型的输出
数组([[-1.3749948e+23]], dtype=float32)
附加信息
在 keras 上训练时的模型准确性
98%
输入详细信息
[{'dtype': numpy.float32,
'index': 0,
'name': 'input_3',
'quantization': (0.0, 0),
'quantization_parameters': {'quantized_dimension': 0,
'scales': array([], dtype=float32),
'zero_points': array([], dtype=int32)},
'shape': array([ 1, 150, 150, 3], dtype=int32),
'shape_signature': array([ 1, 150, 150, 3], dtype=int32),
'sparsity_parameters': {}}]
输出详细信息
[{'dtype': numpy.float32,
'index': 21,
'name': 'Identity',
'quantization': (0.0, 0),
'quantization_parameters': {'quantized_dimension': 0,
'scales': array([], dtype=float32),
'zero_points': array([], dtype=int32)},
'shape': array([1, 1], dtype=int32),
'shape_signature': array([1, 1], dtype=int32),
'sparsity_parameters': {}}]
您需要 运行 interpreter.invoke()
才能阅读结果。 https://www.tensorflow.org/lite/guide/inference#load_and_run_a_model_in_python
按如下方式更新您的代码:
...
...
interpreter.set_tensor(inputdets[0]['index'], input_data)
interpreter.invoke()
result = interpreter.get_tensor(outputdets[0]['index'])
..
..