gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:22 26 4
gpt4 key购买 nike

我有一个非常简单的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com