繁体   English   中英

tensorflow 2.2.0 中的量化感知训练产生更长的推理时间

[英]Quantization aware training in tensorflow 2.2.0 producing higher inference time

我正在使用 Mobil.netV2 对个人数据集进行迁移学习量化。 我尝试了两种方法:

i.) 仅训练后量化:它工作正常并且产生 0.04 秒的平均时间来推断 60 张 224,224 维的图像。

ii.) 量化感知训练 + 训练后量化:它比仅训练后量化产生更高的准确性,但对于相同的 60 张图像产生 0.55 秒的更高推理时间。

1.) 只有训练后量化模型(.tflite)可以通过以下方式推断:

        images_ = cv2.resize(cv2.cvtColor(cv2.imread(imagepath), cv2.COLOR_BGR2RGB), (224, 224))
        images = preprocess_input(images_)
        interpreter.set_tensor(
                    interpreter.get_input_details()[0]['index'], [x])
        interpreter.invoke()
        classes = interpreter.get_tensor(
            interpreter.get_output_details()[0]['index'])

2.) 量化感知训练+训练后量化可以通过以下代码推断。 不同的是这里它要求输入 float32。

        images_ = cv2.resize(cv2.cvtColor(cv2.imread(imagepath), cv2.COLOR_BGR2RGB), (224, 224))
        images = preprocess_input(images_)
        x = np.expand_dims(images, axis=0).astype(np.float32)
        interpreter.set_tensor(
                    interpreter.get_input_details()[0]['index'], x)
        interpreter.invoke()
        classes = interpreter.get_tensor(
            interpreter.get_output_details()[0]['index'])

我进行了很多搜索,但没有得到对此查询的任何回复。 如果可能的话,请帮助解释为什么与仅训练后量化相比,在量化感知训练 + 训练后量化的情况下我的推理时间高?

我不认为你应该一起做量化意识训练+训练后量化。

根据https://www.tensorflow.org/model_optimization/guide/quantization/training_example ,如果您使用量化感知训练,转换将为您提供一个具有 int8 权重的模型。 因此,这里没有必要进行训练后量化。

我认为从uint8转换为float32 ( .astype(np.float32) ) 的部分使它变慢了。 否则,它们应该以相同的速度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM