| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | #ifndef RCARDS_H |
| 15 | #define RCARDS_H |
| 16 | |
| 17 | #include <cstdint> |
| 18 | #include <cmath> |
| 19 | #include <deque> |
| 20 | #include <list> |
| 21 | #include <array> |
| 22 | #include <memory> |
| 23 | #include <iostream> |
| 24 | #include <iomanip> |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <istream> |
| 28 | #include <fstream> |
| 29 | #include <sstream> |
| 30 | #include <algorithm> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <unistd.h> |
| 34 | #include <chrono> |
| 35 | #include <thread> |
| 36 | |
| 37 | //--------------------------------------------------------------------------- |
| 38 | // Global hardcoded parameters |
| 39 | //--------------------------------------------------------------------------- |
| 40 | // LERP(a,b,c) = linear interpolation macro, is 'a' when c == 0.0 and 'b' when c == 1.0 */ |
| 41 | #define MIN(a, b) ((a) > (b) ? (b) : (a)) |
| 42 | #define MAX(a, b) ((a) < (b) ? (b) : (a)) |
| 43 | #define LIM(a, b, c) (((a) > (c)) ? (c) : ((a) < (b)) ? (b) : (a)) |
| 44 | #define LERP(a, b, c) (((b) - (a)) * (c) + (a)) |
| 45 | #define ROUND(a) (static_cast<int>((a) + 0.5)) |
| 46 | #define EUCLIDEAN(x1, y1, x2, y2) sqrt(((x1) - (x2)) * ((x1) - (x2)) + ((y1) - (y2)) * ((y1) - (y2))) |
| 47 | //--------------------------------------------------------------------------- |
| 48 | struct TModel |
| 49 | { |
| 50 | std::string Name; |
| 51 | float AvrTime{0.0}; |
| 52 | }; |
| 53 | //--------------------------------------------------------------------------- |
| 54 | struct TModelSet |
| 55 | { |
| 56 | std::vector<TModel> Mset; |
| 57 | |
| 58 | //use push_back to prevent <brace-enclosed initializer list> issues with CMake |
| 59 | inline TModelSet(void) |
| 60 | { |
| 61 | TModel model; |
| 62 | model.Name = "squeezenet"; |
| 63 | Mset.push_back(model); |
| 64 | model.Name = "squeezenet_int8"; |
| 65 | Mset.push_back(model); |
| 66 | model.Name = "mobilenet"; |
| 67 | Mset.push_back(model); |
| 68 | model.Name = "mobilenet_int8"; |
| 69 | Mset.push_back(model); |
| 70 | model.Name = "mobilenet_v2"; |
| 71 | Mset.push_back(model); |
| 72 | model.Name = "mobilenet_v3"; |
| 73 | Mset.push_back(model); |
| 74 | model.Name = "shufflenet"; |
| 75 | Mset.push_back(model); |
| 76 | model.Name = "shufflenet_v2"; |
| 77 | Mset.push_back(model); |
| 78 | model.Name = "mnasnet"; |
| 79 | Mset.push_back(model); |
| 80 | model.Name = "proxylessnasnet"; |
| 81 | Mset.push_back(model); |
| 82 | model.Name = "efficientnet_b0"; |
| 83 | Mset.push_back(model); |
| 84 | model.Name = "efficientnetv2_b0"; |
| 85 | Mset.push_back(model); |
| 86 | model.Name = "regnety_400m"; |
| 87 | Mset.push_back(model); |
| 88 | model.Name = "blazeface"; |
| 89 | Mset.push_back(model); |
| 90 | model.Name = "googlenet"; |
| 91 | Mset.push_back(model); |
| 92 | model.Name = "googlenet_int8"; |
| 93 | Mset.push_back(model); |
| 94 | model.Name = "resnet18"; |
| 95 | Mset.push_back(model); |
| 96 | model.Name = "resnet18_int8"; |
| 97 | Mset.push_back(model); |
| 98 | model.Name = "alexnet"; |
| 99 | Mset.push_back(model); |
| 100 | model.Name = "vgg16"; |
| 101 | Mset.push_back(model); |
| 102 | model.Name = "vgg16_int8"; |
| 103 | Mset.push_back(model); |
| 104 | model.Name = "resnet50"; |
| 105 | Mset.push_back(model); |
| 106 | model.Name = "resnet50_int8"; |
| 107 | Mset.push_back(model); |
| 108 | model.Name = "squeezenet_ssd"; |
| 109 | Mset.push_back(model); |
| 110 | model.Name = "squeezenet_ssd_int8"; |
| 111 | Mset.push_back(model); |
| 112 | model.Name = "mobilenet_ssd"; |
| 113 | Mset.push_back(model); |
| 114 | model.Name = "mobilenet_ssd_int8"; |
| 115 | Mset.push_back(model); |
| 116 | model.Name = "mobilenet_yolo"; |
| 117 | Mset.push_back(model); |
| 118 | model.Name = "mobilenetv2_yolov3"; |
| 119 | Mset.push_back(model); |
| 120 | model.Name = "yolov4-tiny"; |
| 121 | Mset.push_back(model); |
| 122 | model.Name = "nanodet_m"; |
| 123 | Mset.push_back(model); |
| 124 | model.Name = "yolo-fastest-1.1"; |
| 125 | Mset.push_back(model); |
| 126 | model.Name = "yolo-fastestv2"; |
| 127 | Mset.push_back(model); |
| 128 | model.Name = "vision_transformer"; |
| 129 | Mset.push_back(model); |
| 130 | model.Name = "FastestDet"; |
| 131 | Mset.push_back(model); |
| 132 | } |
| 133 | |
| 134 | void Store(const TModel& model) |
| 135 | { |
| 136 | for (size_t i = 0; i < Mset.size(); i++) |
| 137 | { |
| 138 | if (Mset[i].Name == model.Name) |
| 139 | { |
| 140 | Mset[i].AvrTime = model.AvrTime; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | float Sum(void) |
| 147 | { |
| 148 | float t = 0; |
| 149 | |
| 150 | for (size_t i = 0; i < Mset.size(); i++) t += Mset[i].AvrTime; |
| 151 | |
| 152 | return t; |
| 153 | } |
| 154 | |
| 155 | float Ratio(const TModelSet& Rset) |
| 156 | { |
| 157 | float w; |
| 158 | float s = 0; |
| 159 | float t = 0; |
| 160 | |
| 161 | for (size_t r = 0; r < Rset.Mset.size(); r++) |
| 162 | { |
| 163 | if (Rset.Mset[r].AvrTime > 0.0) |
| 164 | { |
| 165 | for (size_t i = 0; i < Mset.size(); i++) |
| 166 | { |
| 167 | if (Mset[i].AvrTime > 0.0) |
| 168 | { |
| 169 | if (Mset[i].Name == Rset.Mset[r].Name) |
| 170 | { |
| 171 | w = log(Rset.Mset[r].AvrTime); |
| 172 | s += w * (Mset[i].AvrTime / Rset.Mset[r].AvrTime); |
| 173 | t += w; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | if (t > 0) s /= t; |
| 180 | return s; |
| 181 | } |
| 182 | }; |
| 183 | //--------------------------------------------------------------------------- |
| 184 | struct TBoard |
| 185 | { |
| 186 | std::string Name; |
| 187 | size_t StartLine; |
| 188 | size_t EndLine; |
| 189 | std::vector<TModelSet> BenchSet; |
| 190 | int BestSet; |
| 191 | float Ratio; |
| 192 | }; |
| 193 | //--------------------------------------------------------------------------- |
| 194 | inline bool FileExists(const std::string& name) |
| 195 | { |
| 196 | struct stat buffer; |
| 197 | return (stat(name.c_str(), &buffer) == 0); |
| 198 | } |
| 199 | //--------------------------------------------------------------------------- |
| 200 | inline void FileCopy(const std::string& Src, const std::string& Dst) |
| 201 | { |
| 202 | std::ifstream src(Src, std::ios::binary); |
| 203 | std::ofstream dst(Dst, std::ios::binary); |
| 204 | |
| 205 | dst << src.rdbuf(); |
| 206 | } |
| 207 | //--------------------------------------------------------------------------- |
| 208 | // to lower case |
| 209 | static inline void lcase(std::string& s) |
| 210 | { |
| 211 | std::transform(s.begin(), s.end(), s.begin(), |
| 212 | [](unsigned char c) { |
| 213 | return std::tolower(c); |
| 214 | }); |
| 215 | } |
| 216 | //--------------------------------------------------------------------------- |
| 217 | // to lower case (copying) |
| 218 | static inline std::string lcase_copy(std::string s) |
| 219 | { |
| 220 | lcase(s); |
| 221 | return s; |
| 222 | } |
| 223 | //--------------------------------------------------------------------------- |
| 224 | // trim from start (in place) |
| 225 | static inline void ltrim(std::string& s) |
| 226 | { |
| 227 | s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { |
| 228 | return !std::isspace(ch); |
| 229 | })); |
| 230 | } |
| 231 | //--------------------------------------------------------------------------- |
| 232 | // trim from end (in place) |
| 233 | static inline void rtrim(std::string& s) |
| 234 | { |
| 235 | s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { |
| 236 | return !std::isspace(ch); |
| 237 | }).base(), |
| 238 | s.end()); |
| 239 | } |
| 240 | //--------------------------------------------------------------------------- |
| 241 | // trim from both ends (in place) |
| 242 | static inline void trim(std::string& s) |
| 243 | { |
| 244 | ltrim(s); |
| 245 | rtrim(s); |
| 246 | } |
| 247 | //--------------------------------------------------------------------------- |
| 248 | // trim from start (copying) |
| 249 | static inline std::string ltrim_copy(std::string s) |
| 250 | { |
| 251 | ltrim(s); |
| 252 | return s; |
| 253 | } |
| 254 | //--------------------------------------------------------------------------- |
| 255 | // trim from end (copying) |
| 256 | static inline std::string rtrim_copy(std::string s) |
| 257 | { |
| 258 | rtrim(s); |
| 259 | return s; |
| 260 | } |
| 261 | //--------------------------------------------------------------------------- |
| 262 | // trim from both ends (copying) |
| 263 | static inline std::string trim_copy(std::string s) |
| 264 | { |
| 265 | trim(s); |
| 266 | return s; |
| 267 | } |
| 268 | //--------------------------------------------------------------------------- |
| 269 | static inline void GetNameAver(std::string line, TModel& model) |
| 270 | { |
| 271 | // line example: squeezenet min = 46.28 max = 46.91 avg = 46.65 |
| 272 | |
| 273 | size_t p = line.find("min ="); |
| 274 | |
| 275 | if (p != std::string::npos) |
| 276 | { |
| 277 | model.Name = trim_copy(line.substr(0, p)); |
| 278 | p = line.find("avg ="); |
| 279 | if (p != std::string::npos) |
| 280 | { |
| 281 | try |
| 282 | { |
| 283 | model.AvrTime = std::stof(trim_copy(line.substr(p + 5, line.length() - p - 5))); |
| 284 | } |
| 285 | catch (...) |
| 286 | { |
| 287 | } |
| 288 | } |
| 289 | else |
| 290 | model.AvrTime = 0.0; |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | model.Name = ""; |
| 295 | model.AvrTime = 0.0; |
| 296 | } |
| 297 | } |
| 298 | //--------------------------------------------------------------------------- |
| 299 | #endif // RCARDS_H |