feature_extractor.cc 2.6 KB
Newer Older
D
dongshuilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2022 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.

L
lubin 已提交
15
#include "include/feature_extractor.h"
D
dongshuilong 已提交
16 17
#include <cmath>
#include <numeric>
D
dongshuilong 已提交
18 19

namespace PPShiTu {
D
dongshuilong 已提交
20
void FeatureExtract::RunRecModel(const cv::Mat &img, double &cost_time,
L
lubin 已提交
21
                                 std::vector<float> &feature) {
D
dongshuilong 已提交
22 23
  // Read img
  cv::Mat img_fp;
D
dongshuilong 已提交
24 25 26 27
  this->resize_op_.Run_feature(img, img_fp, this->size, this->size);
  this->normalize_op_.Run_feature(&img_fp, this->mean, this->std, this->scale);
  std::vector<float> input(1 * 3 * img_fp.rows * img_fp.cols, 0.0f);
  this->permute_op_.Run_feature(&img_fp, input.data());
D
dongshuilong 已提交
28 29

  // Prepare input data from image
D
dongshuilong 已提交
30
  std::unique_ptr<Tensor> input_tensor(std::move(this->predictor->GetInput(0)));
D
dongshuilong 已提交
31
  input_tensor->Resize({1, 3, this->size, this->size});
D
dongshuilong 已提交
32 33
  auto *data0 = input_tensor->mutable_data<float>();

D
dongshuilong 已提交
34 35
  // const float *dimg = reinterpret_cast<const float *>(img_fp.data);
  // NeonMeanScale(dimg, data0, img_fp.rows * img_fp.cols);
D
dongshuilong 已提交
36
  for (int i = 0; i < input.size(); ++i) {
D
dongshuilong 已提交
37 38
    data0[i] = input[i];
  }
D
dongshuilong 已提交
39 40 41

  auto start = std::chrono::system_clock::now();
  // Run predictor
D
dongshuilong 已提交
42
  this->predictor->Run();
D
dongshuilong 已提交
43 44 45

  // Get output and post process
  std::unique_ptr<const Tensor> output_tensor(
D
dongshuilong 已提交
46
      std::move(this->predictor->GetOutput(0))); // only one output
D
dongshuilong 已提交
47 48 49 50 51 52 53
  auto end = std::chrono::system_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  cost_time = double(duration.count()) *
              std::chrono::microseconds::period::num /
              std::chrono::microseconds::period::den;

D
dongshuilong 已提交
54
  // do postprocess
D
dongshuilong 已提交
55 56 57 58
  int output_size = 1;
  for (auto dim : output_tensor->shape()) {
    output_size *= dim;
  }
L
lubin 已提交
59 60
  feature.resize(output_size);
  output_tensor->CopyToCpu(feature.data());
D
dongshuilong 已提交
61

D
dongshuilong 已提交
62
  // postprocess include sqrt or binarize.
D
dongshuilong 已提交
63
  FeatureNorm(feature);
L
lubin 已提交
64
  return;
D
dongshuilong 已提交
65 66
}

D
dongshuilong 已提交
67 68 69 70 71
void FeatureExtract::FeatureNorm(std::vector<float> &feature) {
  float feature_sqrt = std::sqrt(std::inner_product(
      feature.begin(), feature.end(), feature.begin(), 0.0f));
  for (int i = 0; i < feature.size(); ++i)
    feature[i] /= feature_sqrt;
D
dongshuilong 已提交
72
}
D
dongshuilong 已提交
73
} // namespace PPShiTu