test_common.impl.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. // Used in accuracy and perf tests as a content of .cpp file
  5. // Note: don't use "precomp.hpp" here
  6. #include "opencv2/ts.hpp"
  7. #include "opencv2/ts/ts_perf.hpp"
  8. #include "opencv2/core/utility.hpp"
  9. #include "opencv2/core/ocl.hpp"
  10. #include "opencv2/dnn.hpp"
  11. #include "test_common.hpp"
  12. #include <opencv2/core/utils/configuration.private.hpp>
  13. #include <opencv2/core/utils/logger.hpp>
  14. #ifdef _WIN32
  15. #ifndef NOMINMAX
  16. #define NOMINMAX
  17. #endif
  18. #include <windows.h>
  19. #include <psapi.h>
  20. #endif // _WIN32
  21. namespace cv { namespace dnn {
  22. CV__DNN_INLINE_NS_BEGIN
  23. void PrintTo(const cv::dnn::Backend& v, std::ostream* os)
  24. {
  25. switch (v) {
  26. case DNN_BACKEND_DEFAULT: *os << "DEFAULT"; return;
  27. case DNN_BACKEND_HALIDE: *os << "HALIDE"; return;
  28. case DNN_BACKEND_INFERENCE_ENGINE: *os << "DLIE*"; return;
  29. case DNN_BACKEND_VKCOM: *os << "VKCOM"; return;
  30. case DNN_BACKEND_OPENCV: *os << "OCV"; return;
  31. case DNN_BACKEND_CUDA: *os << "CUDA"; return;
  32. case DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019: *os << "DLIE"; return;
  33. case DNN_BACKEND_INFERENCE_ENGINE_NGRAPH: *os << "NGRAPH"; return;
  34. case DNN_BACKEND_WEBNN: *os << "WEBNN"; return;
  35. case DNN_BACKEND_TIMVX: *os << "TIMVX"; return;
  36. case DNN_BACKEND_CANN: *os << "CANN"; return;
  37. } // don't use "default:" to emit compiler warnings
  38. *os << "DNN_BACKEND_UNKNOWN(" << (int)v << ")";
  39. }
  40. void PrintTo(const cv::dnn::Target& v, std::ostream* os)
  41. {
  42. switch (v) {
  43. case DNN_TARGET_CPU: *os << "CPU"; return;
  44. case DNN_TARGET_OPENCL: *os << "OCL"; return;
  45. case DNN_TARGET_OPENCL_FP16: *os << "OCL_FP16"; return;
  46. case DNN_TARGET_MYRIAD: *os << "MYRIAD"; return;
  47. case DNN_TARGET_HDDL: *os << "HDDL"; return;
  48. case DNN_TARGET_VULKAN: *os << "VULKAN"; return;
  49. case DNN_TARGET_FPGA: *os << "FPGA"; return;
  50. case DNN_TARGET_CUDA: *os << "CUDA"; return;
  51. case DNN_TARGET_CUDA_FP16: *os << "CUDA_FP16"; return;
  52. case DNN_TARGET_NPU: *os << "NPU"; return;
  53. case DNN_TARGET_CPU_FP16: *os << "CPU_FP16"; return;
  54. } // don't use "default:" to emit compiler warnings
  55. *os << "DNN_TARGET_UNKNOWN(" << (int)v << ")";
  56. }
  57. void PrintTo(const tuple<cv::dnn::Backend, cv::dnn::Target> v, std::ostream* os)
  58. {
  59. PrintTo(get<0>(v), os);
  60. *os << "/";
  61. PrintTo(get<1>(v), os);
  62. }
  63. CV__DNN_INLINE_NS_END
  64. }} // namespace
  65. namespace opencv_test {
  66. void normAssert(
  67. cv::InputArray ref, cv::InputArray test, const char *comment /*= ""*/,
  68. double l1 /*= 0.00001*/, double lInf /*= 0.0001*/)
  69. {
  70. double normL1 = cvtest::norm(ref, test, cv::NORM_L1) / ref.getMat().total();
  71. EXPECT_LE(normL1, l1) << comment << " |ref| = " << cvtest::norm(ref, cv::NORM_INF);
  72. double normInf = cvtest::norm(ref, test, cv::NORM_INF);
  73. EXPECT_LE(normInf, lInf) << comment << " |ref| = " << cvtest::norm(ref, cv::NORM_INF);
  74. }
  75. std::vector<cv::Rect2d> matToBoxes(const cv::Mat& m)
  76. {
  77. EXPECT_EQ(m.type(), CV_32FC1);
  78. EXPECT_EQ(m.dims, 2);
  79. EXPECT_EQ(m.cols, 4);
  80. std::vector<cv::Rect2d> boxes(m.rows);
  81. for (int i = 0; i < m.rows; ++i)
  82. {
  83. CV_Assert(m.row(i).isContinuous());
  84. const float* data = m.ptr<float>(i);
  85. double l = data[0], t = data[1], r = data[2], b = data[3];
  86. boxes[i] = cv::Rect2d(l, t, r - l, b - t);
  87. }
  88. return boxes;
  89. }
  90. void normAssertDetections(
  91. const std::vector<int>& refClassIds,
  92. const std::vector<float>& refScores,
  93. const std::vector<cv::Rect2d>& refBoxes,
  94. const std::vector<int>& testClassIds,
  95. const std::vector<float>& testScores,
  96. const std::vector<cv::Rect2d>& testBoxes,
  97. const char *comment /*= ""*/, double confThreshold /*= 0.0*/,
  98. double scores_diff /*= 1e-5*/, double boxes_iou_diff /*= 1e-4*/)
  99. {
  100. ASSERT_FALSE(testClassIds.empty()) << "No detections";
  101. std::vector<bool> matchedRefBoxes(refBoxes.size(), false);
  102. std::vector<double> refBoxesIoUDiff(refBoxes.size(), 1.0);
  103. for (int i = 0; i < testBoxes.size(); ++i)
  104. {
  105. //cout << "Test[i=" << i << "]: score=" << testScores[i] << " id=" << testClassIds[i] << " box " << testBoxes[i] << endl;
  106. double testScore = testScores[i];
  107. if (testScore < confThreshold)
  108. continue;
  109. int testClassId = testClassIds[i];
  110. const cv::Rect2d& testBox = testBoxes[i];
  111. bool matched = false;
  112. double topIoU = 0;
  113. for (int j = 0; j < refBoxes.size() && !matched; ++j)
  114. {
  115. if (!matchedRefBoxes[j] && testClassId == refClassIds[j] &&
  116. std::abs(testScore - refScores[j]) < scores_diff)
  117. {
  118. double interArea = (testBox & refBoxes[j]).area();
  119. double iou = interArea / (testBox.area() + refBoxes[j].area() - interArea);
  120. topIoU = std::max(topIoU, iou);
  121. refBoxesIoUDiff[j] = std::min(refBoxesIoUDiff[j], 1.0f - iou);
  122. if (1.0 - iou < boxes_iou_diff)
  123. {
  124. matched = true;
  125. matchedRefBoxes[j] = true;
  126. }
  127. }
  128. }
  129. if (!matched)
  130. {
  131. std::cout << cv::format("Unmatched prediction: class %d score %f box ",
  132. testClassId, testScore) << testBox << std::endl;
  133. std::cout << "Highest IoU: " << topIoU << std::endl;
  134. }
  135. EXPECT_TRUE(matched) << comment;
  136. }
  137. // Check unmatched reference detections.
  138. for (int i = 0; i < refBoxes.size(); ++i)
  139. {
  140. if (!matchedRefBoxes[i] && refScores[i] > confThreshold)
  141. {
  142. std::cout << cv::format("Unmatched reference: class %d score %f box ",
  143. refClassIds[i], refScores[i]) << refBoxes[i]
  144. << " IoU diff: " << refBoxesIoUDiff[i]
  145. << std::endl;
  146. EXPECT_LE(refScores[i], confThreshold) << comment;
  147. }
  148. }
  149. }
  150. // For SSD-based object detection networks which produce output of shape 1x1xNx7
  151. // where N is a number of detections and an every detection is represented by
  152. // a vector [batchId, classId, confidence, left, top, right, bottom].
  153. void normAssertDetections(
  154. cv::Mat ref, cv::Mat out, const char *comment /*= ""*/,
  155. double confThreshold /*= 0.0*/, double scores_diff /*= 1e-5*/,
  156. double boxes_iou_diff /*= 1e-4*/)
  157. {
  158. CV_Assert(ref.total() % 7 == 0);
  159. CV_Assert(out.total() % 7 == 0);
  160. ref = ref.reshape(1, ref.total() / 7);
  161. out = out.reshape(1, out.total() / 7);
  162. cv::Mat refClassIds, testClassIds;
  163. ref.col(1).convertTo(refClassIds, CV_32SC1);
  164. out.col(1).convertTo(testClassIds, CV_32SC1);
  165. std::vector<float> refScores(ref.col(2)), testScores(out.col(2));
  166. std::vector<cv::Rect2d> refBoxes = matToBoxes(ref.colRange(3, 7));
  167. std::vector<cv::Rect2d> testBoxes = matToBoxes(out.colRange(3, 7));
  168. normAssertDetections(refClassIds, refScores, refBoxes, testClassIds, testScores,
  169. testBoxes, comment, confThreshold, scores_diff, boxes_iou_diff);
  170. }
  171. // For text detection networks
  172. // Curved text polygon is not supported in the current version.
  173. // (concave polygon is invalid input to intersectConvexConvex)
  174. void normAssertTextDetections(
  175. const std::vector<std::vector<Point>>& gtPolys,
  176. const std::vector<std::vector<Point>>& testPolys,
  177. const char *comment /*= ""*/, double boxes_iou_diff /*= 1e-4*/)
  178. {
  179. std::vector<bool> matchedRefBoxes(gtPolys.size(), false);
  180. for (uint i = 0; i < testPolys.size(); ++i)
  181. {
  182. const std::vector<Point>& testPoly = testPolys[i];
  183. bool matched = false;
  184. double topIoU = 0;
  185. for (uint j = 0; j < gtPolys.size() && !matched; ++j)
  186. {
  187. if (!matchedRefBoxes[j])
  188. {
  189. std::vector<Point> intersectionPolygon;
  190. float intersectArea = intersectConvexConvex(testPoly, gtPolys[j], intersectionPolygon, true);
  191. double iou = intersectArea / (contourArea(testPoly) + contourArea(gtPolys[j]) - intersectArea);
  192. topIoU = std::max(topIoU, iou);
  193. if (1.0 - iou < boxes_iou_diff)
  194. {
  195. matched = true;
  196. matchedRefBoxes[j] = true;
  197. }
  198. }
  199. }
  200. if (!matched) {
  201. std::cout << cv::format("Unmatched-det:") << testPoly << std::endl;
  202. std::cout << "Highest IoU: " << topIoU << std::endl;
  203. }
  204. EXPECT_TRUE(matched) << comment;
  205. }
  206. // Check unmatched groundtruth.
  207. for (uint i = 0; i < gtPolys.size(); ++i)
  208. {
  209. if (!matchedRefBoxes[i]) {
  210. std::cout << cv::format("Unmatched-gt:") << gtPolys[i] << std::endl;
  211. }
  212. EXPECT_TRUE(matchedRefBoxes[i]);
  213. }
  214. }
  215. void readFileContent(const std::string& filename, CV_OUT std::vector<char>& content)
  216. {
  217. const std::ios::openmode mode = std::ios::in | std::ios::binary;
  218. std::ifstream ifs(filename.c_str(), mode);
  219. ASSERT_TRUE(ifs.is_open());
  220. content.clear();
  221. ifs.seekg(0, std::ios::end);
  222. const size_t sz = ifs.tellg();
  223. content.resize(sz);
  224. ifs.seekg(0, std::ios::beg);
  225. ifs.read((char*)content.data(), sz);
  226. ASSERT_FALSE(ifs.fail());
  227. }
  228. testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargets(
  229. bool withInferenceEngine /*= true*/,
  230. bool withHalide /*= false*/,
  231. bool withCpuOCV /*= true*/,
  232. bool withVkCom /*= true*/,
  233. bool withCUDA /*= true*/,
  234. bool withNgraph /*= true*/,
  235. bool withWebnn /*= false*/,
  236. bool withCann /*= true*/
  237. )
  238. {
  239. bool withVPU = validateVPUType();
  240. std::vector< tuple<Backend, Target> > targets;
  241. std::vector< Target > available;
  242. if (withHalide)
  243. {
  244. available = getAvailableTargets(DNN_BACKEND_HALIDE);
  245. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  246. targets.push_back(make_tuple(DNN_BACKEND_HALIDE, *i));
  247. }
  248. if (withInferenceEngine)
  249. {
  250. available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019);
  251. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  252. {
  253. if ((*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL) && !withVPU)
  254. continue;
  255. targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019, *i));
  256. }
  257. }
  258. if (withNgraph)
  259. {
  260. available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
  261. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  262. {
  263. if ((*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL) && !withVPU)
  264. continue;
  265. targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH, *i));
  266. }
  267. }
  268. if (withVkCom)
  269. {
  270. available = getAvailableTargets(DNN_BACKEND_VKCOM);
  271. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  272. targets.push_back(make_tuple(DNN_BACKEND_VKCOM, *i));
  273. }
  274. #ifdef HAVE_CUDA
  275. if(withCUDA)
  276. {
  277. for (auto target : getAvailableTargets(DNN_BACKEND_CUDA))
  278. targets.push_back(make_tuple(DNN_BACKEND_CUDA, target));
  279. }
  280. #endif
  281. #ifdef HAVE_WEBNN
  282. if (withWebnn)
  283. {
  284. for (auto target : getAvailableTargets(DNN_BACKEND_WEBNN)) {
  285. targets.push_back(make_tuple(DNN_BACKEND_WEBNN, target));
  286. }
  287. }
  288. #else
  289. CV_UNUSED(withWebnn);
  290. #endif
  291. #ifdef HAVE_CANN
  292. if (withCann)
  293. {
  294. for (auto target : getAvailableTargets(DNN_BACKEND_CANN))
  295. targets.push_back(make_tuple(DNN_BACKEND_CANN, target));
  296. }
  297. #else
  298. CV_UNUSED(withCann);
  299. #endif // HAVE_CANN
  300. {
  301. available = getAvailableTargets(DNN_BACKEND_OPENCV);
  302. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  303. {
  304. if (!withCpuOCV && *i == DNN_TARGET_CPU)
  305. continue;
  306. targets.push_back(make_tuple(DNN_BACKEND_OPENCV, *i));
  307. }
  308. }
  309. if (targets.empty()) // validate at least CPU mode
  310. targets.push_back(make_tuple(DNN_BACKEND_OPENCV, DNN_TARGET_CPU));
  311. return testing::ValuesIn(targets);
  312. }
  313. testing::internal::ParamGenerator< tuple<Backend, Target> > dnnBackendsAndTargetsIE()
  314. {
  315. #ifdef HAVE_INF_ENGINE
  316. bool withVPU = validateVPUType();
  317. std::vector< tuple<Backend, Target> > targets;
  318. std::vector< Target > available;
  319. {
  320. available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH);
  321. for (std::vector< Target >::const_iterator i = available.begin(); i != available.end(); ++i)
  322. {
  323. if ((*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL) && !withVPU)
  324. continue;
  325. targets.push_back(make_tuple(DNN_BACKEND_INFERENCE_ENGINE_NGRAPH, *i));
  326. }
  327. }
  328. return testing::ValuesIn(targets);
  329. #else
  330. return testing::ValuesIn(std::vector< tuple<Backend, Target> >());
  331. #endif
  332. }
  333. static std::string getTestInferenceEngineVPUType()
  334. {
  335. static std::string param_vpu_type = utils::getConfigurationParameterString("OPENCV_TEST_DNN_IE_VPU_TYPE", "");
  336. return param_vpu_type;
  337. }
  338. static bool validateVPUType_()
  339. {
  340. std::string test_vpu_type = getTestInferenceEngineVPUType();
  341. if (test_vpu_type == "DISABLED" || test_vpu_type == "disabled")
  342. {
  343. return false;
  344. }
  345. std::vector<Target> available = getAvailableTargets(DNN_BACKEND_INFERENCE_ENGINE);
  346. bool have_vpu_target = false;
  347. for (std::vector<Target>::const_iterator i = available.begin(); i != available.end(); ++i)
  348. {
  349. if (*i == DNN_TARGET_MYRIAD || *i == DNN_TARGET_HDDL)
  350. {
  351. have_vpu_target = true;
  352. break;
  353. }
  354. }
  355. if (test_vpu_type.empty())
  356. {
  357. if (have_vpu_target)
  358. {
  359. CV_LOG_INFO(NULL, "OpenCV-DNN-Test: VPU type for testing is not specified via 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter.")
  360. }
  361. }
  362. else
  363. {
  364. if (!have_vpu_target)
  365. {
  366. CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'OPENCV_TEST_DNN_IE_VPU_TYPE' parameter requires VPU of type = '" << test_vpu_type << "', but VPU is not detected. STOP.");
  367. exit(1);
  368. }
  369. std::string dnn_vpu_type = getInferenceEngineVPUType();
  370. if (dnn_vpu_type != test_vpu_type)
  371. {
  372. CV_LOG_FATAL(NULL, "OpenCV-DNN-Test: 'testing' and 'detected' VPU types mismatch: '" << test_vpu_type << "' vs '" << dnn_vpu_type << "'. STOP.");
  373. exit(1);
  374. }
  375. }
  376. if (have_vpu_target)
  377. {
  378. std::string dnn_vpu_type = getInferenceEngineVPUType();
  379. if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_2)
  380. registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2);
  381. if (dnn_vpu_type == CV_DNN_INFERENCE_ENGINE_VPU_TYPE_MYRIAD_X)
  382. registerGlobalSkipTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X);
  383. }
  384. return true;
  385. }
  386. bool validateVPUType()
  387. {
  388. static bool result = validateVPUType_();
  389. return result;
  390. }
  391. void initDNNTests()
  392. {
  393. cvtest::addDataSearchEnv("OPENCV_DNN_TEST_DATA_PATH");
  394. registerGlobalSkipTag(
  395. CV_TEST_TAG_DNN_SKIP_OPENCV_BACKEND,
  396. CV_TEST_TAG_DNN_SKIP_CPU, CV_TEST_TAG_DNN_SKIP_CPU_FP16,
  397. CV_TEST_TAG_DNN_SKIP_OPENCL, CV_TEST_TAG_DNN_SKIP_OPENCL_FP16
  398. );
  399. #if defined(HAVE_HALIDE)
  400. registerGlobalSkipTag(
  401. CV_TEST_TAG_DNN_SKIP_HALIDE
  402. );
  403. #endif
  404. #if defined(INF_ENGINE_RELEASE)
  405. registerGlobalSkipTag(
  406. CV_TEST_TAG_DNN_SKIP_IE,
  407. #if INF_ENGINE_VER_MAJOR_EQ(2018050000)
  408. CV_TEST_TAG_DNN_SKIP_IE_2018R5,
  409. #elif INF_ENGINE_VER_MAJOR_EQ(2019010000)
  410. CV_TEST_TAG_DNN_SKIP_IE_2019R1,
  411. # if INF_ENGINE_RELEASE == 2019010100
  412. CV_TEST_TAG_DNN_SKIP_IE_2019R1_1,
  413. # endif
  414. #elif INF_ENGINE_VER_MAJOR_EQ(2019020000)
  415. CV_TEST_TAG_DNN_SKIP_IE_2019R2,
  416. #elif INF_ENGINE_VER_MAJOR_EQ(2019030000)
  417. CV_TEST_TAG_DNN_SKIP_IE_2019R3,
  418. #endif
  419. #ifdef HAVE_DNN_NGRAPH
  420. CV_TEST_TAG_DNN_SKIP_IE_NGRAPH,
  421. #endif
  422. #ifdef HAVE_DNN_IE_NN_BUILDER_2019
  423. CV_TEST_TAG_DNN_SKIP_IE_NN_BUILDER,
  424. #endif
  425. CV_TEST_TAG_DNN_SKIP_IE_CPU
  426. );
  427. registerGlobalSkipTag(
  428. // see validateVPUType(): CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_2, CV_TEST_TAG_DNN_SKIP_IE_MYRIAD_X
  429. CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16
  430. );
  431. #endif
  432. #ifdef HAVE_VULKAN
  433. registerGlobalSkipTag(
  434. CV_TEST_TAG_DNN_SKIP_VULKAN
  435. );
  436. #endif
  437. #ifdef HAVE_CUDA
  438. registerGlobalSkipTag(
  439. CV_TEST_TAG_DNN_SKIP_CUDA, CV_TEST_TAG_DNN_SKIP_CUDA_FP32, CV_TEST_TAG_DNN_SKIP_CUDA_FP16
  440. );
  441. #endif
  442. #ifdef HAVE_TIMVX
  443. registerGlobalSkipTag(
  444. CV_TEST_TAG_DNN_SKIP_TIMVX
  445. );
  446. #endif
  447. #ifdef HAVE_CANN
  448. registerGlobalSkipTag(
  449. CV_TEST_TAG_DNN_SKIP_CANN
  450. );
  451. #endif
  452. registerGlobalSkipTag(
  453. CV_TEST_TAG_DNN_SKIP_ONNX_CONFORMANCE,
  454. CV_TEST_TAG_DNN_SKIP_PARSER
  455. );
  456. }
  457. size_t DNNTestLayer::getTopMemoryUsageMB()
  458. {
  459. #ifdef _WIN32
  460. PROCESS_MEMORY_COUNTERS proc;
  461. GetProcessMemoryInfo(GetCurrentProcess(), &proc, sizeof(proc));
  462. return proc.PeakWorkingSetSize / pow(1024, 2); // bytes to megabytes
  463. #else
  464. std::ifstream status("/proc/self/status");
  465. std::string line, title;
  466. while (std::getline(status, line))
  467. {
  468. std::istringstream iss(line);
  469. iss >> title;
  470. if (title == "VmHWM:")
  471. {
  472. size_t mem;
  473. iss >> mem;
  474. return mem / 1024;
  475. }
  476. }
  477. return 0l;
  478. #endif
  479. }
  480. } // namespace