使用 tensorflow lite 进行CNN模型量化过程中遇到的问题

本文记录了在使用TensorFlow Lite对CNN模型进行量化过程中遇到的问题及解决方案。从float32模型压缩到uint8,通过quantization aware training训练模型,遇到batchnorm与relu量化问题,通过修改代码和参考Mobilenet模型最终成功量化。关键在于正确设置is_training参数,并探讨了可能的tf.layers.batch_normalization量化方法。
摘要由CSDN通过智能技术生成

 

首先,吐槽下tensorflow lite(1.13)的量化工具太不友好了,不仅相关论文写的晦涩难懂,而且实际操作过程中莫名其妙的问题层出不穷。为了解决这些bug,博主可谓费尽脑汁,终于在头发掉完之前将模型量化成功。

言归正传。

1. 我使用tensorflow lite的目的是将CNN模型从float32量化压缩成uint8的,理论上模型的大小能减少到原来的1/4。为了简单起见,我使用quantization aware training的训练方式(参考:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/speech_commands),首先训练了一个3层的CNN模型,模型每层的结构都是conv2d+relu+dropout,模型训练好之后,根据常规套路将checkpoint模型freeze、TOCO转换就可以将模型成功量化。

2. 后期将模型每层的结构改成了conv2d+batchnorm+relu,还是按照常规套路进行训练和freeze,但是在TOCO转换的时候出现了八阿哥:

代码:

    filter1_height = 3
    filter1_width = 5
    filter1_count = filter_list[0]
    weights1 = tf.Variable(tf.truncated_normal(
        [filter1_height, filter1_width, channels, filter1_count],
        stddev=0.01))
    bias1 = tf.Variable(tf.zeros([filter1_count]))
    conv1 = tf.nn.conv2d(fingerprint_4d, weights1, [1, 1, 2, 1], 'VALID')+bias1
    bn1 = tf.layers.batch_normalization(conv1, training=is_training)
    relu1 = tf.nn.relu(bn1)

bug:

F tensorflow/lite/toco/tooling_util.cc:1702] Array batch_normalization/FusedBatchNorm_mul_0, which is an input to the Add operator producing the output array Relu, is lacking min/max
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值