yolov3模型量化

在PC端训练的YOLOv3模型保存的是浮点型参数,模型较大且运算较慢,通过量化减小模型的大小,提高运算速度。需要注意的是,压缩包提供的模型以权重参数的模式保存,不包括图结构,因此在量化前,要先实例化模型。

yolo_v3 = YOLOV3(out_channels=3 * (CATEGORY_NUM + 5))-
yolo_v3.load_weights(filepath=save_model_dir+“epoch-4”)-
image = single_image_inference(image_dir=test_picture_dir, model=yolo_v3)-

converter = tf.lite.TFLiteConverter.from_keras_model(yolo_v3)-
converter.optimizations = [tf.lite.Optimize.DEFAULT]-
converter.representative_dataset = representative_data_gen-

quant_file = tempfile.mkstemp(‘.tflite’)-
with open(quant_file,‘wb’) as f:-
f.write(tflite_quant_model)-

converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]-
converter.inference_input_type = tf.uint8-
converter.inference_output_type = tf.uint8-
# tflite_quant_model = converter.convert()-
tflite_model_quant = converter.convert()

可以看到模型的输入输出和权重参数已经被量化为UINT8类型

1_20230705233725.png