ocr_det.cpp 7.2 KB
Newer Older
littletomatodonkey's avatar
littletomatodonkey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <include/ocr_det.h>
M
MissPenguin 已提交
16

littletomatodonkey's avatar
littletomatodonkey 已提交
17 18
namespace PaddleOCR {

littletomatodonkey's avatar
littletomatodonkey 已提交
19
void DBDetector::LoadModel(const std::string &model_dir) {
L
LDOUBLEV 已提交
20 21
  //   AnalysisConfig config;
  paddle_infer::Config config;
文幕地方's avatar
文幕地方 已提交
22 23
  config.SetModel(model_dir + "/inference.pdmodel",
                  model_dir + "/inference.pdiparams");
littletomatodonkey's avatar
littletomatodonkey 已提交
24

littletomatodonkey's avatar
littletomatodonkey 已提交
25 26
  if (this->use_gpu_) {
    config.EnableUseGpu(this->gpu_mem_, this->gpu_id_);
L
LDOUBLEV 已提交
27
    if (this->use_tensorrt_) {
M
MissPenguin 已提交
28 29 30 31
      auto precision = paddle_infer::Config::Precision::kFloat32;
      if (this->precision_ == "fp16") {
        precision = paddle_infer::Config::Precision::kHalf;
      }
文幕地方's avatar
文幕地方 已提交
32
      if (this->precision_ == "int8") {
M
MissPenguin 已提交
33
        precision = paddle_infer::Config::Precision::kInt8;
文幕地方's avatar
文幕地方 已提交
34
      }
35
      config.EnableTensorRtEngine(1 << 20, 1, 20, precision, false, false);
L
LDOUBLEV 已提交
36 37
      std::map<std::string, std::vector<int>> min_input_shape = {
          {"x", {1, 3, 50, 50}},
38 39 40 41 42 43 44 45 46
          {"conv2d_92.tmp_0", {1, 120, 20, 20}},
          {"conv2d_91.tmp_0", {1, 24, 10, 10}},
          {"conv2d_59.tmp_0", {1, 96, 20, 20}},
          {"nearest_interp_v2_1.tmp_0", {1, 256, 10, 10}},
          {"nearest_interp_v2_2.tmp_0", {1, 256, 20, 20}},
          {"conv2d_124.tmp_0", {1, 256, 20, 20}},
          {"nearest_interp_v2_3.tmp_0", {1, 64, 20, 20}},
          {"nearest_interp_v2_4.tmp_0", {1, 64, 20, 20}},
          {"nearest_interp_v2_5.tmp_0", {1, 64, 20, 20}},
L
LDOUBLEV 已提交
47
          {"elementwise_add_7", {1, 56, 2, 2}},
48
          {"nearest_interp_v2_0.tmp_0", {1, 256, 2, 2}}};
L
LDOUBLEV 已提交
49
      std::map<std::string, std::vector<int>> max_input_shape = {
文幕地方's avatar
文幕地方 已提交
50
          {"x", {1, 3, 1536, 1536}},
51 52 53 54 55 56 57 58 59
          {"conv2d_92.tmp_0", {1, 120, 400, 400}},
          {"conv2d_91.tmp_0", {1, 24, 200, 200}},
          {"conv2d_59.tmp_0", {1, 96, 400, 400}},
          {"nearest_interp_v2_1.tmp_0", {1, 256, 200, 200}},
          {"nearest_interp_v2_2.tmp_0", {1, 256, 400, 400}},
          {"conv2d_124.tmp_0", {1, 256, 400, 400}},
          {"nearest_interp_v2_3.tmp_0", {1, 64, 400, 400}},
          {"nearest_interp_v2_4.tmp_0", {1, 64, 400, 400}},
          {"nearest_interp_v2_5.tmp_0", {1, 64, 400, 400}},
L
LDOUBLEV 已提交
60
          {"elementwise_add_7", {1, 56, 400, 400}},
61
          {"nearest_interp_v2_0.tmp_0", {1, 256, 400, 400}}};
L
LDOUBLEV 已提交
62 63
      std::map<std::string, std::vector<int>> opt_input_shape = {
          {"x", {1, 3, 640, 640}},
64 65 66 67 68 69 70 71 72
          {"conv2d_92.tmp_0", {1, 120, 160, 160}},
          {"conv2d_91.tmp_0", {1, 24, 80, 80}},
          {"conv2d_59.tmp_0", {1, 96, 160, 160}},
          {"nearest_interp_v2_1.tmp_0", {1, 256, 80, 80}},
          {"nearest_interp_v2_2.tmp_0", {1, 256, 160, 160}},
          {"conv2d_124.tmp_0", {1, 256, 160, 160}},
          {"nearest_interp_v2_3.tmp_0", {1, 64, 160, 160}},
          {"nearest_interp_v2_4.tmp_0", {1, 64, 160, 160}},
          {"nearest_interp_v2_5.tmp_0", {1, 64, 160, 160}},
L
LDOUBLEV 已提交
73
          {"elementwise_add_7", {1, 56, 40, 40}},
74
          {"nearest_interp_v2_0.tmp_0", {1, 256, 40, 40}}};
L
LDOUBLEV 已提交
75 76 77

      config.SetTRTDynamicShapeInfo(min_input_shape, max_input_shape,
                                    opt_input_shape);
L
LDOUBLEV 已提交
78
    }
littletomatodonkey's avatar
littletomatodonkey 已提交
79 80
  } else {
    config.DisableGpu();
littletomatodonkey's avatar
littletomatodonkey 已提交
81 82
    if (this->use_mkldnn_) {
      config.EnableMKLDNN();
W
WenmuZhou 已提交
83 84
      // cache 10 different shapes for mkldnn to avoid memory leak
      config.SetMkldnnCacheCapacity(10);
littletomatodonkey's avatar
littletomatodonkey 已提交
85
    }
littletomatodonkey's avatar
littletomatodonkey 已提交
86 87
    config.SetCpuMathLibraryNumThreads(this->cpu_math_library_num_threads_);
  }
L
LDOUBLEV 已提交
88 89
  // use zero_copy_run as default
  config.SwitchUseFeedFetchOps(false);
littletomatodonkey's avatar
littletomatodonkey 已提交
90
  // true for multiple input
littletomatodonkey's avatar
littletomatodonkey 已提交
91
  config.SwitchSpecifyInputNames(true);
littletomatodonkey's avatar
littletomatodonkey 已提交
92 93 94 95

  config.SwitchIrOptim(true);

  config.EnableMemoryOptim();
L
LDOUBLEV 已提交
96
  // config.DisableGlogInfo();
littletomatodonkey's avatar
littletomatodonkey 已提交
97

L
LDOUBLEV 已提交
98
  this->predictor_ = CreatePredictor(config);
littletomatodonkey's avatar
littletomatodonkey 已提交
99 100 101
}

void DBDetector::Run(cv::Mat &img,
M
MissPenguin 已提交
102
                     std::vector<std::vector<std::vector<int>>> &boxes,
103
                     std::vector<double> &times) {
littletomatodonkey's avatar
littletomatodonkey 已提交
104 105 106 107 108 109
  float ratio_h{};
  float ratio_w{};

  cv::Mat srcimg;
  cv::Mat resize_img;
  img.copyTo(srcimg);
文幕地方's avatar
文幕地方 已提交
110

M
MissPenguin 已提交
111
  auto preprocess_start = std::chrono::steady_clock::now();
文幕地方's avatar
文幕地方 已提交
112 113
  this->resize_op_.Run(img, resize_img, this->limit_type_,
                       this->limit_side_len_, ratio_h, ratio_w,
R
root 已提交
114
                       this->use_tensorrt_);
littletomatodonkey's avatar
littletomatodonkey 已提交
115 116 117 118

  this->normalize_op_.Run(&resize_img, this->mean_, this->scale_,
                          this->is_scale_);

littletomatodonkey's avatar
littletomatodonkey 已提交
119 120
  std::vector<float> input(1 * 3 * resize_img.rows * resize_img.cols, 0.0f);
  this->permute_op_.Run(&resize_img, input.data());
M
MissPenguin 已提交
121
  auto preprocess_end = std::chrono::steady_clock::now();
文幕地方's avatar
文幕地方 已提交
122

123
  // Inference.
L
LDOUBLEV 已提交
124 125 126
  auto input_names = this->predictor_->GetInputNames();
  auto input_t = this->predictor_->GetInputHandle(input_names[0]);
  input_t->Reshape({1, 3, resize_img.rows, resize_img.cols});
M
update  
MissPenguin 已提交
127
  auto inference_start = std::chrono::steady_clock::now();
L
LDOUBLEV 已提交
128
  input_t->CopyFromCpu(input.data());
文幕地方's avatar
文幕地方 已提交
129

L
LDOUBLEV 已提交
130
  this->predictor_->Run();
文幕地方's avatar
文幕地方 已提交
131

littletomatodonkey's avatar
littletomatodonkey 已提交
132 133
  std::vector<float> out_data;
  auto output_names = this->predictor_->GetOutputNames();
L
LDOUBLEV 已提交
134
  auto output_t = this->predictor_->GetOutputHandle(output_names[0]);
littletomatodonkey's avatar
littletomatodonkey 已提交
135 136 137 138 139
  std::vector<int> output_shape = output_t->shape();
  int out_num = std::accumulate(output_shape.begin(), output_shape.end(), 1,
                                std::multiplies<int>());

  out_data.resize(out_num);
L
LDOUBLEV 已提交
140
  output_t->CopyToCpu(out_data.data());
M
MissPenguin 已提交
141
  auto inference_end = std::chrono::steady_clock::now();
文幕地方's avatar
文幕地方 已提交
142

M
MissPenguin 已提交
143
  auto postprocess_start = std::chrono::steady_clock::now();
littletomatodonkey's avatar
littletomatodonkey 已提交
144 145 146 147
  int n2 = output_shape[2];
  int n3 = output_shape[3];
  int n = n2 * n3;

littletomatodonkey's avatar
littletomatodonkey 已提交
148 149
  std::vector<float> pred(n, 0.0);
  std::vector<unsigned char> cbuf(n, ' ');
littletomatodonkey's avatar
littletomatodonkey 已提交
150 151 152 153 154 155

  for (int i = 0; i < n; i++) {
    pred[i] = float(out_data[i]);
    cbuf[i] = (unsigned char)((out_data[i]) * 255);
  }

littletomatodonkey's avatar
littletomatodonkey 已提交
156 157
  cv::Mat cbuf_map(n2, n3, CV_8UC1, (unsigned char *)cbuf.data());
  cv::Mat pred_map(n2, n3, CV_32F, (float *)pred.data());
littletomatodonkey's avatar
littletomatodonkey 已提交
158

littletomatodonkey's avatar
littletomatodonkey 已提交
159
  const double threshold = this->det_db_thresh_ * 255;
littletomatodonkey's avatar
littletomatodonkey 已提交
160 161 162
  const double maxvalue = 255;
  cv::Mat bit_map;
  cv::threshold(cbuf_map, bit_map, threshold, maxvalue, cv::THRESH_BINARY);
文幕地方's avatar
文幕地方 已提交
163 164 165 166 167 168
  if (this->use_dilation_) {
    cv::Mat dila_ele =
        cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2, 2));
    cv::dilate(bit_map, bit_map, dila_ele);
  }

169
  boxes = post_processor_.BoxesFromBitmap(
文幕地方's avatar
文幕地方 已提交
170
      pred_map, bit_map, this->det_db_box_thresh_, this->det_db_unclip_ratio_,
171
      this->det_db_score_mode_);
littletomatodonkey's avatar
littletomatodonkey 已提交
172

littletomatodonkey's avatar
littletomatodonkey 已提交
173
  boxes = post_processor_.FilterTagDetRes(boxes, ratio_h, ratio_w, srcimg);
M
MissPenguin 已提交
174 175
  auto postprocess_end = std::chrono::steady_clock::now();

文幕地方's avatar
文幕地方 已提交
176 177
  std::chrono::duration<float> preprocess_diff =
      preprocess_end - preprocess_start;
178
  times.push_back(double(preprocess_diff.count() * 1000));
M
MissPenguin 已提交
179
  std::chrono::duration<float> inference_diff = inference_end - inference_start;
180
  times.push_back(double(inference_diff.count() * 1000));
文幕地方's avatar
文幕地方 已提交
181 182
  std::chrono::duration<float> postprocess_diff =
      postprocess_end - postprocess_start;
183
  times.push_back(double(postprocess_diff.count() * 1000));
littletomatodonkey's avatar
littletomatodonkey 已提交
184 185
}

littletomatodonkey's avatar
littletomatodonkey 已提交
186
} // namespace PaddleOCR