FastDeploy  latest
Fast & Easy to Deploy!
utils.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 #pragma once
15 
16 #include <memory>
17 #include <thread> // NOLINT
18 #include <unordered_map>
19 #include "fastdeploy/utils/utils.h"
20 #include "fastdeploy/core/fd_tensor.h"
21 #if defined(ENABLE_BENCHMARK) && defined(ENABLE_VISION)
22 #include "fastdeploy/vision/common/result.h"
23 #endif
24 
25 namespace fastdeploy {
26 namespace benchmark {
27 
28 #if defined(ENABLE_BENCHMARK)
29 
31 class FASTDEPLOY_DECL ResourceUsageMonitor {
32  public:
38  explicit ResourceUsageMonitor(int sampling_interval_ms, int gpu_id = 0);
39 
40  ~ResourceUsageMonitor() { StopInternal(); }
41 
43  void Start();
45  void Stop();
47  float GetMaxCpuMem() const {
48  if (!is_supported_ || check_memory_thd_ == nullptr) {
49  return -1.0f;
50  }
51  return max_cpu_mem_;
52  }
54  float GetMaxGpuMem() const {
55  if (!is_supported_ || check_memory_thd_ == nullptr) {
56  return -1.0f;
57  }
58  return max_gpu_mem_;
59  }
61  float GetMaxGpuUtil() const {
62  if (!is_supported_ || check_memory_thd_ == nullptr) {
63  return -1.0f;
64  }
65  return max_gpu_util_;
66  }
67 
68  ResourceUsageMonitor(ResourceUsageMonitor&) = delete;
69  ResourceUsageMonitor& operator=(const ResourceUsageMonitor&) = delete;
70  ResourceUsageMonitor(ResourceUsageMonitor&&) = delete;
71  ResourceUsageMonitor& operator=(const ResourceUsageMonitor&&) = delete;
72 
73  private:
74  void StopInternal();
75  // Get current gpu memory info
76  std::string GetCurrentGpuMemoryInfo(int device_id);
77 
78  bool is_supported_ = false;
79  bool stop_signal_ = false;
80  const int sampling_interval_;
81  float max_cpu_mem_ = 0.0f; // MB
82  float max_gpu_mem_ = 0.0f; // MB
83  float max_gpu_util_ = 0.0f;
84  const int gpu_id_ = 0;
85  std::unique_ptr<std::thread> check_memory_thd_ = nullptr;
86 };
87 
88 // Remove the ch characters at both ends of str
89 FASTDEPLOY_DECL std::string Strip(const std::string& str, char ch = ' ');
90 
91 // Split string
92 FASTDEPLOY_DECL void Split(const std::string& s,
93  std::vector<std::string>& tokens,
94  char delim = ' ');
95 
97 struct FASTDEPLOY_DECL BaseDiff {};
98 
99 struct FASTDEPLOY_DECL EvalStatis {
100  double mean = -1.0;
101  double min = -1.0;
102  double max = -1.0;
103 };
104 
105 struct FASTDEPLOY_DECL TensorDiff: public BaseDiff {
106  EvalStatis data;
107 };
108 
109 #if defined(ENABLE_VISION)
110 struct FASTDEPLOY_DECL DetectionDiff: public BaseDiff {
111  EvalStatis boxes;
112  EvalStatis scores;
113  EvalStatis labels;
114 };
115 
116 struct FASTDEPLOY_DECL ClassifyDiff: public BaseDiff {
117  EvalStatis scores;
118  EvalStatis labels;
119 };
120 
121 struct FASTDEPLOY_DECL SegmentationDiff: public BaseDiff {
122  EvalStatis scores;
123  EvalStatis labels;
124 };
125 
126 struct FASTDEPLOY_DECL OCRDetDiff: public BaseDiff {
127  EvalStatis boxes;
128 };
129 
130 struct FASTDEPLOY_DECL MattingDiff: public BaseDiff {
131  EvalStatis alpha;
132  EvalStatis foreground;
133 };
134 
135 #endif // ENABLE_VISION
136 #endif // ENABLE_BENCHMARK
137 
139 struct FASTDEPLOY_DECL ResultManager {
140 #if defined(ENABLE_BENCHMARK)
141  static bool SaveFDTensor(const FDTensor& tensor, const std::string& path);
143  static bool LoadFDTensor(FDTensor* tensor, const std::string& path);
145  static TensorDiff CalculateDiffStatis(const FDTensor& lhs,
146  const FDTensor& rhs);
148  static void SaveBenchmarkResult(const std::string& res,
149  const std::string& path);
151  static bool LoadBenchmarkConfig(const std::string& path,
152  std::unordered_map<std::string, std::string>* config_info);
154  static std::vector<std::vector<int32_t>> GetInputShapes(
155  const std::string& raw_shapes);
156 #if defined(ENABLE_VISION)
157  static bool SaveDetectionResult(const vision::DetectionResult& res,
159  const std::string& path);
160  static bool LoadDetectionResult(vision::DetectionResult* res,
161  const std::string& path);
162  static bool SaveClassifyResult(const vision::ClassifyResult& res,
163  const std::string& path);
164  static bool LoadClassifyResult(vision::ClassifyResult* res,
165  const std::string& path);
166  static bool SaveSegmentationResult(const vision::SegmentationResult& res,
167  const std::string& path);
168  static bool LoadSegmentationResult(vision::SegmentationResult* res,
169  const std::string& path);
170  static bool SaveOCRDetResult(const std::vector<std::array<int, 8>>& res,
171  const std::string& path);
172  static bool LoadOCRDetResult(std::vector<std::array<int, 8>>* res,
173  const std::string& path);
174  static bool SaveMattingResult(const vision::MattingResult& res,
175  const std::string& path);
176  static bool LoadMattingResult(vision::MattingResult* res,
177  const std::string& path);
179  static DetectionDiff CalculateDiffStatis(const vision::DetectionResult& lhs,
180  const vision::DetectionResult& rhs,
181  const float& score_threshold = 0.3f);
182  static ClassifyDiff CalculateDiffStatis(const vision::ClassifyResult& lhs,
183  const vision::ClassifyResult& rhs);
184  static SegmentationDiff CalculateDiffStatis(
185  const vision::SegmentationResult& lhs,
186  const vision::SegmentationResult& rhs);
187  static OCRDetDiff CalculateDiffStatis(
188  const std::vector<std::array<int, 8>>& lhs,
189  const std::vector<std::array<int, 8>>& rhs);
190  static MattingDiff CalculateDiffStatis(
191  const vision::MattingResult& lhs,
192  const vision::MattingResult& rhs);
193 #endif // ENABLE_VISION
194 #endif // ENABLE_BENCHMARK
195 };
196 
197 } // namespace benchmark
198 } // namespace fastdeploy
Utils for precision evaluation.
Definition: utils.h:139
Segmentation result structure for all the segmentation models.
Definition: result.h:270
FDTensor object used to represend data matrix.
Definition: fd_tensor.h:31
Detection result structure for all the object detection models and instance segmentation models...
Definition: result.h:106
Classify result structure for all the image classify models.
Definition: result.h:46
Matting result structure for all the Matting models.
Definition: result.h:331
void Split(const FDTensor &x, const std::vector< int > &num_or_sections, std::vector< FDTensor > *out, int axis)
Definition: split.cc:152
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16