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