"src/include/Array.hpp" did not exist on "87d8740bf5d8030f9e4e54c9b7e64f353a6f944e"
ocr_cls.cpp 3.21 KB
Newer Older
WenmuZhou's avatar
WenmuZhou committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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_cls.h>

namespace PaddleOCR {

cv::Mat Classifier::Run(cv::Mat &img) {
  cv::Mat src_img;
  img.copyTo(src_img);
  cv::Mat resize_img;

  std::vector<int> cls_image_shape = {3, 48, 192};
  int index = 0;
  float wh_ratio = float(img.cols) / float(img.rows);

root's avatar
root committed
28
  this->resize_op_.Run(img, resize_img, this->use_tensorrt_, cls_image_shape);
WenmuZhou's avatar
WenmuZhou committed
29
30
31
32
33
34
35
36
37

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

  std::vector<float> input(1 * 3 * resize_img.rows * resize_img.cols, 0.0f);

  this->permute_op_.Run(&resize_img, input.data());

  // Inference.
LDOUBLEV's avatar
LDOUBLEV committed
38
39
40
41
42
  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});
  input_t->CopyFromCpu(input.data());
  this->predictor_->Run();
WenmuZhou's avatar
WenmuZhou committed
43
44
45
46

  std::vector<float> softmax_out;
  std::vector<int64_t> label_out;
  auto output_names = this->predictor_->GetOutputNames();
LDOUBLEV's avatar
LDOUBLEV committed
47
  auto softmax_out_t = this->predictor_->GetOutputHandle(output_names[0]);
WenmuZhou's avatar
WenmuZhou committed
48
49
50
51
52
53
54
55
  auto softmax_shape_out = softmax_out_t->shape();

  int softmax_out_num =
      std::accumulate(softmax_shape_out.begin(), softmax_shape_out.end(), 1,
                      std::multiplies<int>());

  softmax_out.resize(softmax_out_num);

LDOUBLEV's avatar
LDOUBLEV committed
56
  softmax_out_t->CopyToCpu(softmax_out.data());
WenmuZhou's avatar
WenmuZhou committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

  float score = 0;
  int label = 0;
  for (int i = 0; i < softmax_out_num; i++) {
    if (softmax_out[i] > score) {
      score = softmax_out[i];
      label = i;
    }
  }
  if (label % 2 == 1 && score > this->cls_thresh) {
    cv::rotate(src_img, src_img, 1);
  }
  return src_img;
}

void Classifier::LoadModel(const std::string &model_dir) {
  AnalysisConfig config;
WenmuZhou's avatar
WenmuZhou committed
74
75
  config.SetModel(model_dir + "/inference.pdmodel",
                  model_dir + "/inference.pdiparams");
WenmuZhou's avatar
WenmuZhou committed
76
77
78

  if (this->use_gpu_) {
    config.EnableUseGpu(this->gpu_mem_, this->gpu_id_);
79
80
81
82
83
84
85
    if (this->use_tensorrt_) {
      config.EnableTensorRtEngine(
          1 << 20, 10, 3,
          this->use_fp16_ ? paddle_infer::Config::Precision::kHalf
                          : paddle_infer::Config::Precision::kFloat32,
          false, false);
    }
WenmuZhou's avatar
WenmuZhou committed
86
87
88
89
90
  } else {
    config.DisableGpu();
    if (this->use_mkldnn_) {
      config.EnableMKLDNN();
    }
littletomatodonkey's avatar
littletomatodonkey committed
91
    config.SetCpuMathLibraryNumThreads(this->cpu_math_library_num_threads_);
WenmuZhou's avatar
WenmuZhou committed
92
93
94
  }

  // false for zero copy tensor
LDOUBLEV's avatar
LDOUBLEV committed
95
  config.SwitchUseFeedFetchOps(false);
WenmuZhou's avatar
WenmuZhou committed
96
97
98
99
100
101
102
103
  // true for multiple input
  config.SwitchSpecifyInputNames(true);

  config.SwitchIrOptim(true);

  config.EnableMemoryOptim();
  config.DisableGlogInfo();

LDOUBLEV's avatar
LDOUBLEV committed
104
  this->predictor_ = CreatePredictor(config);
WenmuZhou's avatar
WenmuZhou committed
105
106
}
} // namespace PaddleOCR