首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以将TFLite配置为将带有偏置量化的模型返回到int8?

是否可以将TFLite配置为将带有偏置量化的模型返回到int8?
EN

Stack Overflow用户
提问于 2020-08-07 21:53:09
回答 1查看 397关注 0票数 2

我正在与Keras/Tensorflow合作开发一个将被部署到低端MCU的ANN。为此,我使用Tensorflow Lite提供的训练后量化机制对原始ANN进行了量化。如果权重确实量化为int8,则会将偏移从float转换为int32。考虑到我假装在CMSIS-NN中实现这个ANN,这是一个问题,因为他们只支持int8和int16数据。

是否可以将TF Lite配置为也将偏差量化为int8?下面是我正在执行的代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-10-23 01:38:41

来自评论

不可能将TFLite配置为这样做。Biases是故意int32的,否则量化精度不会很好。为了实现这一点,您必须添加一个新的op或自定义op,然后想出一个自定义量化工具。(改编自Meghna Natraj)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63303255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档