代码之家  ›  专栏  ›  技术社区  ›  Aparajit Garg

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

  •  1
  • Aparajit Garg  · 技术社区  · 4 年前

    我正在使用MobilenetV2对个人数据集进行迁移学习中的量化研究。我尝试了两种方法:

    i.)仅训练后量化:它工作良好,在224224维上推断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'])
    

    我搜索了很多,但这个问题没有得到任何回应。如果可能的话,请帮忙 为什么与仅训练后量化相比,在量化感知训练+训练后量化的情况下,我的推理时间很高?

    0 回复  |  直到 4 年前
        1
  •  0
  •   Thaink    4 年前

    我认为你不应该把量化感知训练+训练后量化结合起来。

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

        2
  •  0
  •   Louis Yang dontloo    2 年前

    我认为从 uint8 float32 ( .astype(np.float32) )这就是它变慢的原因。否则,它们应该以相同的速度。