Skip to content

Commit 17a1189

Browse files
[Other] Set TRT dynamic input shape for PPOCR examples. (#566)
* Imporve OCR Readme * Improve OCR Readme * Improve OCR Readme * Improve OCR Readme * Improve OCR Readme * Add Initialize function to PP-OCR * Add Initialize function to PP-OCR * Add Initialize function to PP-OCR * Make all the model links come from PaddleOCR * Improve OCR readme * Improve OCR readme * Improve OCR readme * Improve OCR readme * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add Readme for vision results * Add check for label file in postprocess of Rec model * Add check for label file in postprocess of Rec model * Add check for label file in postprocess of Rec model * Add check for label file in postprocess of Rec model * Add check for label file in postprocess of Rec model * Add check for label file in postprocess of Rec model * Add comments to create API docs * Improve OCR comments * Rename OCR and add comments * Make sure previous python example works * Make sure previous python example works * Fix Rec model bug * Fix Rec model bug * Fix rec model bug * Add SetTrtMaxBatchSize function for TensorRT * Add SetTrtMaxBatchSize Pybind * Add set_trt_max_batch_size python function * Set TRT dynamic shape in PPOCR examples * Set TRT dynamic shape in PPOCR examples * Set TRT dynamic shape in PPOCR examples Co-authored-by: Jason <jiangjiajun@baidu.com>
1 parent 0d0389f commit 17a1189

File tree

4 files changed

+77
-21
lines changed

4 files changed

+77
-21
lines changed

examples/vision/ocr/PP-OCRv2/cpp/infer.cc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,25 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
2929
auto rec_model_file = rec_model_dir + sep + "inference.pdmodel";
3030
auto rec_params_file = rec_model_dir + sep + "inference.pdiparams";
3131

32-
auto det_model = fastdeploy::vision::ocr::DBDetector(det_model_file, det_params_file, option);
33-
auto cls_model = fastdeploy::vision::ocr::Classifier(cls_model_file, cls_params_file, option);
34-
auto rec_model = fastdeploy::vision::ocr::Recognizer(rec_model_file, rec_params_file, rec_label_file, option);
32+
auto det_option = option;
33+
auto cls_option = option;
34+
auto rec_option = option;
35+
36+
// If use TRT backend, the dynamic shape will be set as follow.
37+
det_option.SetTrtInputShape("x", {1, 3, 50, 50}, {1, 3, 640, 640},
38+
{1, 3, 1536, 1536});
39+
cls_option.SetTrtInputShape("x", {1, 3, 48, 10}, {1, 3, 48, 320}, {1, 3, 48, 1024});
40+
rec_option.SetTrtInputShape("x", {1, 3, 32, 10}, {1, 3, 32, 320},
41+
{1, 3, 32, 2304});
42+
43+
// Users could save TRT cache file to disk as follow.
44+
// det_option.SetTrtCacheFile(det_model_dir + sep + "det_trt_cache.trt");
45+
// cls_option.SetTrtCacheFile(cls_model_dir + sep + "cls_trt_cache.trt");
46+
// rec_option.SetTrtCacheFile(rec_model_dir + sep + "rec_trt_cache.trt");
47+
48+
auto det_model = fastdeploy::vision::ocr::DBDetector(det_model_file, det_params_file, det_option);
49+
auto cls_model = fastdeploy::vision::ocr::Classifier(cls_model_file, cls_params_file, cls_option);
50+
auto rec_model = fastdeploy::vision::ocr::Recognizer(rec_model_file, rec_params_file, rec_label_file, rec_option);
3551

3652
assert(det_model.Initialized());
3753
assert(cls_model.Initialized());

examples/vision/ocr/PP-OCRv2/python/infer.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,29 @@ def build_option(args):
9696
rec_params_file = os.path.join(args.rec_model, "inference.pdiparams")
9797
rec_label_file = args.rec_label_file
9898

99-
# 对于三个模型,均采用同样的部署配置
100-
# 用户也可根据自行需求分别配置
101-
runtime_option = build_option(args)
99+
det_option = runtime_option
100+
cls_option = runtime_option
101+
rec_option = runtime_option
102+
103+
# 当使用TRT时,分别给三个Runtime设置动态shape
104+
det_option.set_trt_input_shape("x", [1, 3, 50, 50], [1, 3, 640, 640],
105+
[1, 3, 1536, 1536])
106+
cls_option.set_trt_input_shape("x", [1, 3, 48, 10], [1, 3, 48, 320],
107+
[1, 3, 48, 1024])
108+
rec_option.set_trt_input_shape("x", [1, 3, 32, 10], [1, 3, 32, 320],
109+
[1, 3, 32, 2304])
110+
111+
# 用户可以把TRT引擎文件保存至本地
112+
# det_option.set_trt_cache_file(args.det_model + "/det_trt_cache.trt")
113+
# cls_option.set_trt_cache_file(args.cls_model + "/cls_trt_cache.trt")
114+
# rec_option.set_trt_cache_file(args.rec_model + "/rec_trt_cache.trt")
102115

103116
det_model = fd.vision.ocr.DBDetector(
104-
det_model_file, det_params_file, runtime_option=runtime_option)
117+
det_model_file, det_params_file, runtime_option=det_option)
105118
cls_model = fd.vision.ocr.Classifier(
106-
cls_model_file, cls_params_file, runtime_option=runtime_option)
119+
cls_model_file, cls_params_file, runtime_option=cls_option)
107120
rec_model = fd.vision.ocr.Recognizer(
108-
rec_model_file,
109-
rec_params_file,
110-
rec_label_file,
111-
runtime_option=runtime_option)
121+
rec_model_file, rec_params_file, rec_label_file, runtime_option=rec_option)
112122

113123
# 创建PP-OCR,串联3个模型,其中cls_model可选,如无需求,可设置为None
114124
ppocr_v2 = fd.vision.ocr.PPOCRv2(

examples/vision/ocr/PP-OCRv3/cpp/infer.cc

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,25 @@ void InitAndInfer(const std::string& det_model_dir, const std::string& cls_model
2929
auto rec_model_file = rec_model_dir + sep + "inference.pdmodel";
3030
auto rec_params_file = rec_model_dir + sep + "inference.pdiparams";
3131

32-
auto det_model = fastdeploy::vision::ocr::DBDetector(det_model_file, det_params_file, option);
33-
auto cls_model = fastdeploy::vision::ocr::Classifier(cls_model_file, cls_params_file, option);
34-
auto rec_model = fastdeploy::vision::ocr::Recognizer(rec_model_file, rec_params_file, rec_label_file, option);
32+
auto det_option = option;
33+
auto cls_option = option;
34+
auto rec_option = option;
35+
36+
// If use TRT backend, the dynamic shape will be set as follow.
37+
det_option.SetTrtInputShape("x", {1, 3, 50, 50}, {1, 3, 640, 640},
38+
{1, 3, 1536, 1536});
39+
cls_option.SetTrtInputShape("x", {1, 3, 48, 10}, {1, 3, 48, 320}, {1, 3, 48, 1024});
40+
rec_option.SetTrtInputShape("x", {1, 3, 48, 10}, {1, 3, 48, 320},
41+
{1, 3, 48, 2304});
42+
43+
// Users could save TRT cache file to disk as follow.
44+
// det_option.SetTrtCacheFile(det_model_dir + sep + "det_trt_cache.trt");
45+
// cls_option.SetTrtCacheFile(cls_model_dir + sep + "cls_trt_cache.trt");
46+
// rec_option.SetTrtCacheFile(rec_model_dir + sep + "rec_trt_cache.trt");
47+
48+
auto det_model = fastdeploy::vision::ocr::DBDetector(det_model_file, det_params_file, det_option);
49+
auto cls_model = fastdeploy::vision::ocr::Classifier(cls_model_file, cls_params_file, cls_option);
50+
auto rec_model = fastdeploy::vision::ocr::Recognizer(rec_model_file, rec_params_file, rec_label_file, rec_option);
3551

3652
assert(det_model.Initialized());
3753
assert(cls_model.Initialized());

examples/vision/ocr/PP-OCRv3/python/infer.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,29 @@ def build_option(args):
100100
# 用户也可根据自行需求分别配置
101101
runtime_option = build_option(args)
102102

103+
det_option = runtime_option
104+
cls_option = runtime_option
105+
rec_option = runtime_option
106+
107+
# 当使用TRT时,分别给三个Runtime设置动态shape
108+
det_option.set_trt_input_shape("x", [1, 3, 50, 50], [1, 3, 640, 640],
109+
[1, 3, 1536, 1536])
110+
cls_option.set_trt_input_shape("x", [1, 3, 48, 10], [1, 3, 48, 320],
111+
[1, 3, 48, 1024])
112+
rec_option.set_trt_input_shape("x", [1, 3, 48, 10], [1, 3, 48, 320],
113+
[1, 3, 48, 2304])
114+
115+
# 用户可以把TRT引擎文件保存至本地
116+
# det_option.set_trt_cache_file(args.det_model + "/det_trt_cache.trt")
117+
# cls_option.set_trt_cache_file(args.cls_model + "/cls_trt_cache.trt")
118+
# rec_option.set_trt_cache_file(args.rec_model + "/rec_trt_cache.trt")
119+
103120
det_model = fd.vision.ocr.DBDetector(
104-
det_model_file, det_params_file, runtime_option=runtime_option)
121+
det_model_file, det_params_file, runtime_option=det_option)
105122
cls_model = fd.vision.ocr.Classifier(
106-
cls_model_file, cls_params_file, runtime_option=runtime_option)
123+
cls_model_file, cls_params_file, runtime_option=cls_option)
107124
rec_model = fd.vision.ocr.Recognizer(
108-
rec_model_file,
109-
rec_params_file,
110-
rec_label_file,
111-
runtime_option=runtime_option)
125+
rec_model_file, rec_params_file, rec_label_file, runtime_option=rec_option)
112126

113127
# 创建PP-OCR,串联3个模型,其中cls_model可选,如无需求,可设置为None
114128
ppocr_v3 = fd.vision.ocr.PPOCRv3(

0 commit comments

Comments
 (0)