Oha*_*eir 5 quantization tensorflow tensorflow-lite quantization-aware-training
我正在使用 TensorFlow 2.1 来训练具有量化感知训练的模型。
这样做的代码是:
import tensorflow_model_optimization as tfmot
model = tfmot.quantization.keras.quantize_annotate_model(model)
Run Code Online (Sandbox Code Playgroud)
这将向图中添加假量化节点。这些节点应该调整模型的权重,以便它们更容易被量化为 int8 并使用 int8 数据。
训练结束后,我将模型转换并量化为 TF-Lite,如下所示:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = [give data provider]
quantized_tflite_model = converter.convert()
Run Code Online (Sandbox Code Playgroud)
在这一点上,我不希望在 TL-Lite 图中看到假量化层。但令人惊讶的是,我确实看到了它们。此外,当我在 TF-Lite C++示例应用程序中运行这个量化模型时,我发现它在推理过程中也在运行假量化节点。除此之外,它还对每层之间的激活进行反量化和量化。
这是 C++ 代码的输出示例:
节点 0 运算符内置代码 80 FAKE_QUANT
输入:1
输出:237
节点 1 运算符内置代码 114 QUANTIZE
输入:237
输出:238
节点 2 运算符内置代码 3 CONV_2D
输入:238 59 58
输出:167 运算符内置代码 6
临时代码:237
DEQUANTIZE
输入:167
输出:239
节点 4 运算符内置代码 80 FAKE_QUANT
输入:239
输出:166
节点 5 运算符内置代码 114 QUANTIZE
输入:166
输出:240
节点 6 运算符内置代码 3 CONV_2D
输入:
输出:169
所以我觉得这一切都非常奇怪,还考虑到这个模型应该只在 int8 上运行并且实际上假量化节点将 float32 作为输入。
任何帮助在这里将不胜感激。
小智 0
代表数据集主要用于训练后量化。
将您的命令与 QAT 示例进行比较,您可能想要删除该行。
https://www.tensorflow.org/model_optimization/guide/quantization/training_example
converter = tf.lite.TFLiteConverter.from_keras_model(q_aware_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
quantized_tflite_model = converter.convert()
# Create float TFLite model.
float_converter = tf.lite.TFLiteConverter.from_keras_model(model)
float_tflite_model = float_converter.convert()
# Measure sizes of models.
_, float_file = tempfile.mkstemp('.tflite')
_, quant_file = tempfile.mkstemp('.tflite')
with open(quant_file, 'wb') as f:
f.write(quantized_tflite_model)
with open(float_file, 'wb') as f:
f.write(float_tflite_model)
print("Float model in Mb:", os.path.getsize(float_file) / float(2**20))
print("Quantized model in Mb:", os.path.getsize(quant_file) / float(2**20))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
946 次 |
| 最近记录: |