From 64a2a78ed2fc12ad3d929509d2ae6e81c6d881a3 Mon Sep 17 00:00:00 2001 From: wangguanzhong Date: Thu, 16 Jul 2020 17:52:13 +0800 Subject: [PATCH] fix cpp inference without resize (#1073) --- configs/face_detection/blazeface.yml | 10 +++--- configs/face_detection/blazeface_keypoint.yml | 10 +++--- configs/face_detection/blazeface_nas.yml | 10 +++--- configs/face_detection/blazeface_nas_v2.yml | 10 +++--- configs/face_detection/faceboxes.yml | 11 ++++--- configs/face_detection/faceboxes_lite.yml | 11 ++++--- deploy/cpp/include/preprocess_op.h | 8 +++++ deploy/cpp/src/preprocess_op.cc | 32 ++++++++++++------- 8 files changed, 67 insertions(+), 35 deletions(-) diff --git a/configs/face_detection/blazeface.yml b/configs/face_detection/blazeface.yml index c52478690..d9701541a 100644 --- a/configs/face_detection/blazeface.yml +++ b/configs/face_detection/blazeface.yml @@ -96,11 +96,12 @@ EvalReader: - !DecodeImage to_rgb: true - !NormalizeBox {} - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 TestReader: @@ -112,9 +113,10 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 diff --git a/configs/face_detection/blazeface_keypoint.yml b/configs/face_detection/blazeface_keypoint.yml index 07481f409..5d6aa4449 100644 --- a/configs/face_detection/blazeface_keypoint.yml +++ b/configs/face_detection/blazeface_keypoint.yml @@ -104,11 +104,12 @@ EvalReader: - !DecodeImage to_rgb: true - !NormalizeBox {} - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 TestReader: @@ -120,9 +121,10 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 diff --git a/configs/face_detection/blazeface_nas.yml b/configs/face_detection/blazeface_nas.yml index 5e0f81b23..c19a3a12b 100644 --- a/configs/face_detection/blazeface_nas.yml +++ b/configs/face_detection/blazeface_nas.yml @@ -98,11 +98,12 @@ EvalReader: - !DecodeImage to_rgb: true - !NormalizeBox {} - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 TestReader: @@ -114,9 +115,10 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 diff --git a/configs/face_detection/blazeface_nas_v2.yml b/configs/face_detection/blazeface_nas_v2.yml index f13c6c37a..48593460b 100644 --- a/configs/face_detection/blazeface_nas_v2.yml +++ b/configs/face_detection/blazeface_nas_v2.yml @@ -98,11 +98,12 @@ EvalReader: - !DecodeImage to_rgb: true - !NormalizeBox {} - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 TestReader: @@ -114,9 +115,10 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 diff --git a/configs/face_detection/faceboxes.yml b/configs/face_detection/faceboxes.yml index 92b0a39b2..c0e189e23 100644 --- a/configs/face_detection/faceboxes.yml +++ b/configs/face_detection/faceboxes.yml @@ -97,11 +97,13 @@ EvalReader: sample_transforms: - !DecodeImage to_rgb: true - - !Permute {} + - !NormalizeBox {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} TestReader: inputs_def: @@ -112,9 +114,10 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 diff --git a/configs/face_detection/faceboxes_lite.yml b/configs/face_detection/faceboxes_lite.yml index fd346eb1d..dbda51021 100644 --- a/configs/face_detection/faceboxes_lite.yml +++ b/configs/face_detection/faceboxes_lite.yml @@ -98,11 +98,13 @@ EvalReader: - !DecodeImage to_rgb: true - !NormalizeBox {} - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} + TestReader: inputs_def: @@ -113,9 +115,10 @@ TestReader: sample_transforms: - !DecodeImage to_rgb: true - - !Permute {} - !NormalizeImage + is_channel_first: false is_scale: false - mean: [104, 117, 123] + mean: [123, 117, 104] std: [127.502231, 127.502231, 127.502231] + - !Permute {} batch_size: 1 diff --git a/deploy/cpp/include/preprocess_op.h b/deploy/cpp/include/preprocess_op.h index af672ea81..1ec10061e 100644 --- a/deploy/cpp/include/preprocess_op.h +++ b/deploy/cpp/include/preprocess_op.h @@ -50,6 +50,12 @@ class PreprocessOp { virtual void Run(cv::Mat* im, ImageBlob* data) = 0; }; +class InitInfo : public PreprocessOp{ + public: + virtual void Init(const YAML::Node& item, const std::string& arch) {} + virtual void Run(cv::Mat* im, ImageBlob* data); +}; + class Normalize : public PreprocessOp { public: virtual void Init(const YAML::Node& item, const std::string& arch) { @@ -127,6 +133,8 @@ class Preprocessor { public: void Init(const YAML::Node& config_node, const std::string& arch) { arch_ = arch; + // initialize image info at first + ops_["InitInfo"] = std::make_shared(); for (const auto& item : config_node) { auto op_name = item["type"].as(); ops_[op_name] = CreateOp(op_name); diff --git a/deploy/cpp/src/preprocess_op.cc b/deploy/cpp/src/preprocess_op.cc index f572bac74..cec3feb7a 100644 --- a/deploy/cpp/src/preprocess_op.cc +++ b/deploy/cpp/src/preprocess_op.cc @@ -19,6 +19,24 @@ namespace PaddleDetection { +void InitInfo::Run(cv::Mat* im, ImageBlob* data) { + data->ori_im_size_ = { + static_cast(im->rows), + static_cast(im->cols) + }; + data->ori_im_size_f_ = { + static_cast(im->rows), + static_cast(im->cols), + 1.0 + }; + data->eval_im_size_f_ = { + static_cast(im->rows), + static_cast(im->cols), + 1.0 + }; + data->scale_factor_f_ = {1., 1., 1., 1.}; +} + void Normalize::Run(cv::Mat* im, ImageBlob* data) { double e = 1.0; if (is_scale_) { @@ -44,20 +62,12 @@ void Permute::Run(cv::Mat* im, ImageBlob* data) { (data->im_data_).resize(rc * rh * rw); float* base = (data->im_data_).data(); for (int i = 0; i < rc; ++i) { - cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, base + i * rh * rw), i); + int cur_c = to_bgr_ ? rc - i - 1 : i; + cv::extractChannel(*im, cv::Mat(rh, rw, CV_32FC1, base + cur_c * rh * rw), i); } } void Resize::Run(cv::Mat* im, ImageBlob* data) { - data->ori_im_size_ = { - static_cast(im->rows), - static_cast(im->cols) - }; - data->ori_im_size_f_ = { - static_cast(im->rows), - static_cast(im->cols), - 1.0 - }; auto resize_scale = GenerateScale(*im); cv::resize( *im, *im, cv::Size(), resize_scale.first, resize_scale.second, interp_); @@ -137,7 +147,7 @@ void PadStride::Run(cv::Mat* im, ImageBlob* data) { // Preprocessor op running order const std::vector Preprocessor::RUN_ORDER = { - "Resize", "Normalize", "PadStride", "Permute" + "InitInfo", "Resize", "Normalize", "PadStride", "Permute" }; void Preprocessor::Run(cv::Mat* im, ImageBlob* data) { -- GitLab