python - 我可以从量化的 Tensorflow 模型中得到什么结果?

标签 python tensorflow tpu

我有一个非常简单的 Tensorflow 模型,它根据 6 个传感器的输入(输入范围从 0.0 到 1.0)输出 3 个值(左、直、右)

inputSensor = Input(shape=(3))
modelSensor = Dense(32, activation="relu")(inputSensor)
modelSensor = Dense(32, activation="relu")(modelSensor)
modelSensor = Dense(numClasses, activation="softmax")(modelSensor)
model = Model(inputs=inputSensor, outputs=modelSensor)

训练模型后,我可以使用将其转换为普通的 TFLite 模型

converter = tf.compat.v1.lite.TFLiteConverter.from_keras_model_file("models/modelpath.h5")
converter.optimizations = [tf.lite.Optimize.DEFAULT]

tflite_model =转换器.convert()

然后我对模型运行一些推理测试,得到了我期望的结果。 3 个输出之一 99%,其他都很低。

测试正确 输入[0.0,0.0,1.0] 结果 [[0.00089144 0.00154889 0.99755967]]

测试直 输入[0.0,1.0,0.0] 结果 [[0.00087439 0.996424 0.00270158]]

向左测试 输入[1.0,0.0,0.0] 结果[[0.9948014 0.00256803 0.00263061]]

然后我量化模型(以便在边缘 TPU 上使用它)

converter =tf.compat.v1.lite.TFLiteConverter. from_keras_model_file 
("models/modelpath.h5")
converter.representative_dataset = representativeDataset_gen
converter.target_spec.supported_ops = 
[tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
converter.optimizations = [tf.lite.Optimize.DEFAULT]

我运行了与上面相同的测试。结果仍然正确,但最大输出为 132,其他输出为 62。

测试正确 输入[0.0,0.0,1.0] [[ 62 62 132]]

测试直 输入[0.0,1.0,0.0] [[ 62 132 62]]

向左测试 输入[1.0,0.0,0.0] [[132 62 62]]

我希望得到 [[255 0 0]] 范围内的结果

当我使用整数输入时,也会发生同样的事情 输入[0,0,255]

量化转换有问题吗? 我应该使用浮点整数作为输入吗?

最佳答案

当您进行训练后量化时,小尺寸模型肯定会遭受较大的精度下降。事实上,TensorFlow 团队建议在应用训练后量化后执行准确性检查。

如果准确性受到严重影响,建议使用 quantization-aware-training .

在撰写此答案时,量化感知训练尚未启动。

关于python - 我可以从量化的 Tensorflow 模型中得到什么结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57730078/

相关文章:

python - 显示 python sleep 函数的倒计时

python - 用于文本分类的 Tensorflow 模型未按预期进行预测?

python - 带有 tensorflow-gpu 的 Keras 完全卡住了 PC

python - 如何在 Tensorflow 2.0 中使用 Google Colab 的 TPU?

python - 如何在 Apache Superset 中定义自定义指标?

python - 文本语料库的数据结构

tensorflow - Google Colab - 连接到 Google Cloud Storage 几天后未创建 adc.json 文件

Python 3 模板引擎

tensorflow - 我可以使用 conv2d 和张量操作等现有操作在 python 中的 tensorflow 中编写自定义层吗?