exporting QAT-trained model for deployment
Hi,
I ran into 2 issues while trying to export & deploy a model trained with quantization-aware-training.
- When I try to do inference with the exported model using
deploy/python/infer.py, I get the following error. I settrt_calib_modetoFalsebecause this model wasn't produced by offline quantization, as per the comment in https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/deploy/python/infer.py#L369.
----------- Running Arguments -----------
batch_size: 1
camera_id: -1
cpu_threads: 1
enable_mkldnn: False
image_dir: imgs/test_imgs
image_file: None
model_dir: models/export/ppyolo_r50vd_qat_pact/
output_dir: output/test_imgs/ppyolo_r50vd_qat_pact/trt_int8
run_benchmark: False
run_mode: trt_int8
save_image: False
save_text: False
threshold: 0.25
trt_calib_mode: False
trt_max_shape: 1280
trt_min_shape: 1
trt_opt_shape: 640
use_dynamic_shape: False
use_gpu: True
video_file: None
------------------------------------------
----------- Model Configuration -----------
Model Arch: YOLO
Transform Order:
--transform op: Resize
--transform op: NormalizeImage
--transform op: Permute
--------------------------------------------
I0703 15:17:26.743402 117143 analysis_config.cc:424] use_dlnne_:0
I0703 15:17:26.743464 117143 analysis_config.cc:424] use_dlnne_:0
I0703 15:17:26.743527 117143 analysis_config.cc:424] use_dlnne_:0
I0703 15:17:26.743553 117143 analysis_config.cc:424] use_dlnne_:0
I0703 15:17:26.743574 117143 analysis_config.cc:424] use_dlnne_:0
E0703 15:17:28.494822 117143 helper.h:84] Calibration failure occurred with no scaling factors detected. This could be due to no int8 calibrator or insufficient custom scales for network layers. Please see int8 sample to setup calibration correctly.
E0703 15:17:28.494869 117143 helper.h:84] Builder failed while configuring INT8 mode.
Traceback (most recent call last):
File "deploy/python/infer.py", line 711, in <module>
main()
File "deploy/python/infer.py", line 654, in main
enable_mkldnn=FLAGS.enable_mkldnn)
File "deploy/python/infer.py", line 88, in __init__
enable_mkldnn=enable_mkldnn)
File "deploy/python/infer.py", line 478, in load_predictor
predictor = create_predictor(config)
SystemError: (Fatal) Build TensorRT cuda engine failed! Please recheck you configurations related to paddle-TensorRT.
[Hint: infer_engine_ should not be null.] (at /paddle/paddle/fluid/inference/tensorrt/engine.cc:242)
- Just to check the inference speed, I performed offline quantization to create
_opt_cachein the exported model directory, and did inference by setting--trt_calib_mode True --run_mode trt_int8. However, the inference speed is very slow (60 ms/img on V100) compared to non-QAT post-quantized int8 model (7 ms/img on V100).
Am I missing something here? Any advice would be much appreciated. FYI, I am using PaddleDetection release/2.1, CUDA 10.2, cuDNN 8.0 and TensorRT 7.
@ichbing1
- You need set
--trt_calib_mode Falsewhen do inference. - Please check you model, is it a quantitative model? You can test the pre-trained model we trained to see whether it works: https://paddledet.bj.bcebos.com/models/slim/ppyolo_r50vd_qat_pact.tar
@yghstill Thanks for your prompt feedback.
- You need set
--trt_calib_mode Falsewhen do inference.
Yes, that's what I am doing. In fact, using --trt_calib_mode False results in trt_calib_mode being set to True, so I omitted --trt_calib_mode option instead. I think it's a bug that should be fixed by modifying https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.1/deploy/python/utils.py#L98 to type=ast.literal_eval.
- Please check you model, is it a quantitative model? You can test the pre-trained model we trained to see whether it works: https://paddledet.bj.bcebos.com/models/slim/ppyolo_r50vd_qat_pact.tar
Yes, I also tested with the pre-trained model and I get the same error. The error only disappears when I set --trt_calib_mode True. Since this requires calibration cache in _opt_cache subdirectory, I first created it with calibration images, then tested the inference speed, which is approx. 10 times slower than non-QAT model.
Also, instead of downloading the pre-trained model directly, I downloaded the .pdparams file from https://paddledet.bj.bcebos.com/models/slim/ppyolo_r50vd_qat_pact.pdparams and exported it myself with following command:
python tools/export_model.py -c configs/ppyolo/ppyolo_r50vd_dcn_1x_coco.yml --slim_config configs/slim/quant/ppyolo_r50vd_qat_pact.yml -o weights=models/ppyolo_r50vd_dcn_1x_coco.pdparams
Then I get a different error message when I run deploy/python/infer.py:
Traceback (most recent call last):
File "deploy/python/infer.py", line 711, in <module>
main()
File "deploy/python/infer.py", line 654, in main
enable_mkldnn=FLAGS.enable_mkldnn)
File "deploy/python/infer.py", line 88, in __init__
enable_mkldnn=enable_mkldnn)
File "deploy/python/infer.py", line 478, in load_predictor
predictor = create_predictor(config)
ValueError: (InvalidArgument) Weight scale should be nonzero, but get zero.
[Hint: Expected weight_scale[i] != 0, but received weight_scale[i]:0 == 0:0.] (at /paddle/paddle/fluid/framework/ir/delete_quant_dequant_filter_op_pass.cc:137)
@ichbing1 We will test to see is there a problem
Since this issue has not been updated for more than three months, it will be closed, if it is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up. It is recommended to pull and try the latest code first. 由于该问题超过三个月未更新,将会被关闭,若问题未解决或有后续问题,请随时重新打开(建议先拉取最新代码进行尝试),我们会继续跟进。