FastDeploy  latest
Fast & Easy to Deploy!
normalize.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 Normalize : public Processor {
24  public:
25  Normalize(const std::vector<float>& mean, const std::vector<float>& std,
26  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(Mat* mat);
31 #ifdef ENABLE_FLYCV
32  bool ImplByFlyCV(Mat* 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 "Normalize"; }
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(Mat* 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 
71  std::vector<float> GetAlpha() const { return alpha_; }
72  std::vector<float> GetBeta() const { return beta_; }
73 
74  bool GetSwapRB() {
75  return swap_rb_;
76  }
77 
82  void SetSwapRB(bool swap_rb) {
83  swap_rb_ = swap_rb;
84  }
85 
86  private:
87  std::vector<float> alpha_;
88  std::vector<float> beta_;
89  FDTensor gpu_alpha_;
90  FDTensor gpu_beta_;
91  bool swap_rb_;
92 };
93 } // namespace vision
94 } // namespace fastdeploy
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
Definition: float16.h:572
Processor for Normalize images with given paramters.
Definition: normalize.h:23
FDMat is a structure for replace cv::Mat.
Definition: mat.h:34
void SetSwapRB(bool swap_rb)
Process the input images.
Definition: normalize.h:82
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
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16