tensorflow 2.2.0中的量化意识训练产生了更长的推理时间

时间:2020-09-09 06:22:00

标签: python computer-vision tensorflow2.0 quantization-aware-training optimizer-deeplearning

我正在使用用于个人数据集的MobilenetV2在转移学习中进行量化。我尝试了2种方法:

i。)仅在训练后量化:它工作正常,并且在224,224个维度上生成60幅图像的平均时间为0.04s。

ii。)量化意识训练+训练后量化:与仅训练后量化相比,它产生更高的准确性,但是对于相同的60张图像,其推理时间更高,为0.55s。

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'])

我进行了很多搜索,但此查询没有任何响应。如果可能的话,请提供帮助。为什么量化意识训练+训练后量化而不是训练后量化使推理时间变长?

1 个答案:

答案 0 :(得分:0)

我不认为您应该同时进行量化意识训练和训练后量化。

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