FastDeploy  latest
Fast & Easy to Deploy!
normalize_and_permute.h
1 // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "fastdeploy/vision/common/processors/base.h"
18 
19 namespace fastdeploy {
20 namespace vision {
23 class FASTDEPLOY_DECL NormalizeAndPermute : public Processor {
24  public:
25  NormalizeAndPermute(const std::vector<float>& mean,
26  const std::vector<float>& std, bool is_scale = true,
27  const std::vector<float>& min = std::vector<float>(),
28  const std::vector<float>& max = std::vector<float>(),
29  bool swap_rb = false);
30  bool ImplByOpenCV(FDMat* mat);
31 #ifdef ENABLE_FLYCV
32  bool ImplByFlyCV(FDMat* mat);
33 #endif
34 #ifdef WITH_GPU
35  bool ImplByCuda(FDMat* mat);
36  bool ImplByCuda(FDMatBatch* mat_batch);
37 #endif
38 #ifdef ENABLE_CVCUDA
39  bool ImplByCvCuda(FDMat* mat);
40  bool ImplByCvCuda(FDMatBatch* mat_batch);
41 #endif
42  std::string Name() { return "NormalizeAndPermute"; }
43 
44  // While use normalize, it is more recommend not use this function
45  // this function will need to compute result = ((mat / 255) - mean) / std
46  // if we use the following method
47  // ```
48  // auto norm = Normalize(...)
49  // norm(mat)
50  // ```
51  // There will be some precomputation in contruct function
52  // and the `norm(mat)` only need to compute result = mat * alpha + beta
53  // which will reduce lots of time
65  static bool Run(FDMat* mat, const std::vector<float>& mean,
66  const std::vector<float>& std, bool is_scale = true,
67  const std::vector<float>& min = std::vector<float>(),
68  const std::vector<float>& max = std::vector<float>(),
69  ProcLib lib = ProcLib::DEFAULT, bool swap_rb = false);
70 
75  void SetAlpha(const std::vector<float>& alpha) {
76  alpha_.clear();
77  std::vector<float>().swap(alpha_);
78  alpha_.assign(alpha.begin(), alpha.end());
79  }
80 
85  void SetBeta(const std::vector<float>& beta) {
86  beta_.clear();
87  std::vector<float>().swap(beta_);
88  beta_.assign(beta.begin(), beta.end());
89  }
90 
91  bool GetSwapRB() {
92  return swap_rb_;
93  }
94 
99  void SetSwapRB(bool swap_rb) {
100  swap_rb_ = swap_rb;
101  }
102 
103  private:
104  std::vector<float> alpha_;
105  std::vector<float> beta_;
106  FDTensor gpu_alpha_;
107  FDTensor gpu_beta_;
108  bool swap_rb_;
109 };
110 } // namespace vision
111 } // namespace fastdeploy
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
Definition: float16.h:572
void SetBeta(const std::vector< float > &beta)
Process the input images.
Definition: normalize_and_permute.h:85
void SetAlpha(const std::vector< float > &alpha)
Process the input images.
Definition: normalize_and_permute.h:75
Processor for Normalize and Permute images from HWC to CHW.
Definition: normalize_and_permute.h:23
FDMat is a structure for replace cv::Mat.
Definition: mat.h:34
Processor base class for processors in fastdeploy/vision/common/processors.
Definition: base.h:42
FDMatBatch contains batch data for preprocess.
Definition: mat_batch.h:28
void SetSwapRB(bool swap_rb)
Process the input images.
Definition: normalize_and_permute.h:99
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16