perf_remap.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. const isNodeJs = (typeof window) === 'undefined'? true : false;
  2. if (isNodeJs) {
  3. var Benchmark = require('benchmark');
  4. var cv = require('../../opencv');
  5. var HelpFunc = require('../perf_helpfunc');
  6. var Base = require('../base');
  7. } else {
  8. var paramsElement = document.getElementById('params');
  9. var runButton = document.getElementById('runButton');
  10. var logElement = document.getElementById('log');
  11. }
  12. function perf() {
  13. console.log('opencv.js loaded');
  14. if (isNodeJs) {
  15. global.cv = cv;
  16. global.combine = HelpFunc.combine;
  17. global.log = HelpFunc.log;
  18. global.decodeParams2Case = HelpFunc.decodeParams2Case;
  19. global.setBenchmarkSuite = HelpFunc.setBenchmarkSuite;
  20. global.addKernelCase = HelpFunc.addKernelCase;
  21. global.cvSize = Base.getCvSize();
  22. } else {
  23. enableButton();
  24. cvSize = getCvSize();
  25. }
  26. let totalCaseNum, currentCaseId;
  27. const RemapSize = [cvSize.szVGA, cvSize.sz1080p];
  28. const RemapSrcType = ["CV_16UC1", "CV_16SC1", "CV_32FC1"];
  29. const RemapType = ["CV_16SC2", "CV_32FC1", "CV_32FC2"];
  30. const InterType = ["INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LANCZOS4"];
  31. const combiRemap = combine(RemapSize, RemapSrcType, RemapType, InterType);
  32. function addRemapCase(suite, type) {
  33. suite.add('remap', function() {
  34. cv.remap(src, dst, map1, map2, interType);
  35. }, {
  36. 'setup': function() {
  37. let size = this.params.size;
  38. let matType = cv[this.params.matType];
  39. let mapType = cv[this.params.mapType];
  40. let interType = cv[this.params.interType];
  41. let src = new cv.Mat(size, matType);
  42. let dst = new cv.Mat(size, matType);
  43. let map1 = new cv.Mat(size, mapType);
  44. let map2;
  45. if (mapType == cv.CV_32FC1) {
  46. map2 = new cv.Mat(size, mapType);
  47. } else if (interType != cv.INTER_NEAREST && mapType == cv.CV_16SC2) {
  48. map2 = new cv.Mat.zeros(size, cv.CV_16UC1);
  49. } else {
  50. map2 = new cv.Mat();
  51. }
  52. for (let j = 0; j < map1.rows; j++) {
  53. for (let i = 0; i < map1.cols; i++) {
  54. let randNum = Math.random();
  55. let view, view1;
  56. switch(matType) {
  57. case cv.CV_16UC1:
  58. view = src.ushortPtr(j,i);
  59. view[0] = Math.floor(randNum*256);
  60. break;
  61. case cv.CV_16SC1:
  62. view = src.shortPtr(j,i);
  63. view[0] = Math.floor(randNum*256);
  64. break;
  65. case cv.CV_32FC1:
  66. view = src.floatPtr(j,i);
  67. view[0] = randNum*256;
  68. break;
  69. default:
  70. console.error("Unknown conversion type 1");
  71. break;
  72. }
  73. switch(mapType) {
  74. case cv.CV_32FC1:
  75. view1 = map1.floatPtr(j,i);
  76. let view2 = map2.floatPtr(j,i);
  77. view1[0] = src.cols - i - 1;
  78. view2[0] = j;
  79. break;
  80. case cv.CV_32FC2:
  81. view1 = map1.floatPtr(j,i);
  82. view1[0] = src.cols - i - 1;
  83. view1[1] = j;
  84. break;
  85. case cv.CV_16SC2:
  86. view1 = map1.shortPtr(j,i);
  87. view1[0] = src.cols - i - 1;
  88. view1[1] = j;
  89. break;
  90. default:
  91. console.error("Unknown conversion type 2");
  92. break;
  93. }
  94. }
  95. }
  96. },
  97. 'teardown': function() {
  98. src.delete();
  99. dst.delete();
  100. map1.delete();
  101. map2.delete();
  102. }
  103. });
  104. }
  105. function addRemapModeCase(suite, combination, type) {
  106. totalCaseNum += combination.length;
  107. for (let i = 0; i < combination.length; ++i) {
  108. let size = combination[i][0];
  109. let matType = combination[i][1];
  110. let mapType = combination[i][2];
  111. let interType = combination[i][3];
  112. let params = {size: size, matType:matType, mapType:mapType, interType:interType};
  113. addKernelCase(suite, params, type, addRemapCase);
  114. }
  115. }
  116. function genBenchmarkCase(paramsContent) {
  117. let suite = new Benchmark.Suite;
  118. totalCaseNum = 0;
  119. currentCaseId = 0;
  120. if (/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*CV\_\w+,[\ ]*INTER\_\w+\)/g.test(paramsContent.toString())) {
  121. let params = paramsContent.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*CV\_\w+,[\ ]*INTER\_\w+\)/g)[0];
  122. let paramObjs = [];
  123. paramObjs.push({name:"size", value:"", reg:[""], index:0});
  124. paramObjs.push({name:"matType", value:"", reg:["/CV\_[0-9]+[FSUfsu]C[0-9]/"], index:1});
  125. paramObjs.push({name:"mapType", value:"", reg:["/CV\_[0-9]+[FSUfsu]C[0-9]/g"], index:2, loc:1});
  126. paramObjs.push({name:"interType", value: "", reg:["/INTER\_\\w+/"], index:3});
  127. let locationList = decodeParams2Case(params, paramObjs, remapCombinations);
  128. for (let i = 0; i < locationList.length; i++){
  129. let first = locationList[i][0];
  130. let second = locationList[i][1];
  131. addRemapModeCase(suite, [remapCombinations[first][second]], first);
  132. }
  133. } else {
  134. log("no filter or getting invalid params, run all the cases");
  135. addRemapModeCase(suite, combiRemap, 0);
  136. }
  137. setBenchmarkSuite(suite, "remap", currentCaseId);
  138. log(`Running ${totalCaseNum} tests from remap`);
  139. suite.run({ 'async': true }); // run the benchmark
  140. }
  141. let remapCombinations = [combiRemap];
  142. if (isNodeJs) {
  143. const args = process.argv.slice(2);
  144. let paramsContent = '';
  145. if (/--test_param_filter=\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*CV\_\w+,[\ ]*INTER\_\w+\)/g.test(args.toString())) {
  146. paramsContent = args.toString().match(/\([0-9]+x[0-9]+,[\ ]*CV\_\w+,[\ ]*CV\_\w+,[\ ]*INTER\_\w+\)/g)[0];
  147. }
  148. genBenchmarkCase(paramsContent);
  149. } else {
  150. runButton.onclick = function() {
  151. let paramsContent = paramsElement.value;
  152. genBenchmarkCase(paramsContent);
  153. if (totalCaseNum !== 0) {
  154. disableButton();
  155. }
  156. }
  157. }
  158. };
  159. async function main() {
  160. if (cv instanceof Promise) {
  161. cv = await cv;
  162. perf();
  163. } else {
  164. cv.onRuntimeInitialized = perf;
  165. }
  166. }
  167. main();