我正在与Keras/Tensorflow合作开发一个将被部署到低端MCU的ANN。为此,我使用Tensorflow Lite提供的训练后量化机制对原始ANN进行了量化。如果权重确实量化为int8,则会将偏移从float转换为int32。考虑到我假装在CMSIS-NN中实现这个ANN,这是一个问题,因为他们只支持int8和int16数据。
是否可以将TF Lite配置为也将偏差量化为int8?下面是我正在执行的代码:
def quantizeToInt8(representativeDataset):
# Cast the dataset to float32
data = tf.cast(representativeDataset, tf.float32)
data = tf.data.Dataset.from_tensor_slices((data)).batch(1)
# Generator function that returns one data point per iteration
def representativeDatasetGen():
for inputValue in data:
yield[inputValue]
# ANN quantization
model = tf.keras.models.load_model("C:/Users/miguel/Documents/Universidade/PhD/Code_Samples/TensorFlow/originalModel.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representativeDatasetGen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.target_spec.supported_types = [tf.int8]
converter.inference_type = tf.int8
converter.inference_input_type = tf.int8 # or tf.uint8
converter.inference_output_type = tf.int8 # or tf.uint8
tflite_quant_model = converter.convert()
return tflite_quant_model发布于 2020-10-23 01:38:41
来自评论
不可能将
TFLite配置为这样做。Biases是故意int32的,否则量化精度不会很好。为了实现这一点,您必须添加一个新的op或自定义op,然后想出一个自定义量化工具。(改编自Meghna Natraj)。
https://stackoverflow.com/questions/63303255
复制相似问题