@srsergio/taptapp-ar 1.0.9 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/dist/compiler/controller.d.ts +15 -22
  2. package/dist/compiler/controller.js +73 -92
  3. package/dist/compiler/detector/crop-detector.d.ts +20 -51
  4. package/dist/compiler/detector/crop-detector.js +21 -15
  5. package/dist/compiler/input-loader.d.ts +15 -17
  6. package/dist/compiler/input-loader.js +58 -76
  7. package/dist/compiler/matching/hamming-distance.js +4 -4
  8. package/dist/compiler/matching/matcher.js +2 -2
  9. package/dist/compiler/matching/matching.d.ts +2 -16
  10. package/dist/compiler/matching/matching.js +72 -60
  11. package/dist/compiler/offline-compiler.d.ts +9 -29
  12. package/dist/compiler/offline-compiler.js +38 -72
  13. package/dist/compiler/three.js +0 -4
  14. package/dist/compiler/tracker/tracker.d.ts +26 -12
  15. package/dist/compiler/tracker/tracker.js +158 -259
  16. package/package.json +1 -1
  17. package/src/compiler/controller.js +71 -93
  18. package/src/compiler/detector/crop-detector.js +26 -15
  19. package/src/compiler/input-loader.js +62 -88
  20. package/src/compiler/matching/hamming-distance.js +4 -4
  21. package/src/compiler/matching/hough.js +1 -1
  22. package/src/compiler/matching/matcher.js +2 -2
  23. package/src/compiler/matching/matching.js +80 -72
  24. package/src/compiler/offline-compiler.js +38 -75
  25. package/src/compiler/three.js +0 -4
  26. package/src/compiler/tracker/tracker.js +183 -283
  27. package/dist/compiler/compiler-base.d.ts +0 -8
  28. package/dist/compiler/compiler-base.js +0 -179
  29. package/dist/compiler/compiler.d.ts +0 -9
  30. package/dist/compiler/compiler.js +0 -24
  31. package/dist/compiler/compiler.worker.d.ts +0 -1
  32. package/dist/compiler/compiler.worker.js +0 -28
  33. package/dist/compiler/detector/detector.d.ts +0 -97
  34. package/dist/compiler/detector/detector.js +0 -1042
  35. package/dist/compiler/detector/kernels/cpu/binomialFilter.d.ts +0 -6
  36. package/dist/compiler/detector/kernels/cpu/binomialFilter.js +0 -50
  37. package/dist/compiler/detector/kernels/cpu/buildExtremas.d.ts +0 -6
  38. package/dist/compiler/detector/kernels/cpu/buildExtremas.js +0 -89
  39. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.d.ts +0 -7
  40. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.js +0 -79
  41. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.d.ts +0 -6
  42. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.js +0 -68
  43. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.d.ts +0 -6
  44. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.js +0 -57
  45. package/dist/compiler/detector/kernels/cpu/computeLocalization.d.ts +0 -6
  46. package/dist/compiler/detector/kernels/cpu/computeLocalization.js +0 -50
  47. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.d.ts +0 -6
  48. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.js +0 -100
  49. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.d.ts +0 -6
  50. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.js +0 -29
  51. package/dist/compiler/detector/kernels/cpu/extremaReduction.d.ts +0 -6
  52. package/dist/compiler/detector/kernels/cpu/extremaReduction.js +0 -50
  53. package/dist/compiler/detector/kernels/cpu/fakeShader.d.ts +0 -20
  54. package/dist/compiler/detector/kernels/cpu/fakeShader.js +0 -80
  55. package/dist/compiler/detector/kernels/cpu/index.d.ts +0 -1
  56. package/dist/compiler/detector/kernels/cpu/index.js +0 -25
  57. package/dist/compiler/detector/kernels/cpu/prune.d.ts +0 -7
  58. package/dist/compiler/detector/kernels/cpu/prune.js +0 -62
  59. package/dist/compiler/detector/kernels/cpu/smoothHistograms.d.ts +0 -6
  60. package/dist/compiler/detector/kernels/cpu/smoothHistograms.js +0 -47
  61. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.d.ts +0 -6
  62. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.js +0 -43
  63. package/dist/compiler/detector/kernels/index.d.ts +0 -1
  64. package/dist/compiler/detector/kernels/index.js +0 -2
  65. package/dist/compiler/detector/kernels/webgl/binomialFilter.d.ts +0 -6
  66. package/dist/compiler/detector/kernels/webgl/binomialFilter.js +0 -67
  67. package/dist/compiler/detector/kernels/webgl/buildExtremas.d.ts +0 -6
  68. package/dist/compiler/detector/kernels/webgl/buildExtremas.js +0 -101
  69. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.d.ts +0 -6
  70. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.js +0 -78
  71. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.d.ts +0 -6
  72. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.js +0 -86
  73. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.d.ts +0 -6
  74. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.js +0 -52
  75. package/dist/compiler/detector/kernels/webgl/computeLocalization.d.ts +0 -6
  76. package/dist/compiler/detector/kernels/webgl/computeLocalization.js +0 -58
  77. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.d.ts +0 -6
  78. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.js +0 -116
  79. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.d.ts +0 -6
  80. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.js +0 -46
  81. package/dist/compiler/detector/kernels/webgl/extremaReduction.d.ts +0 -6
  82. package/dist/compiler/detector/kernels/webgl/extremaReduction.js +0 -48
  83. package/dist/compiler/detector/kernels/webgl/index.d.ts +0 -1
  84. package/dist/compiler/detector/kernels/webgl/index.js +0 -25
  85. package/dist/compiler/detector/kernels/webgl/smoothHistograms.d.ts +0 -6
  86. package/dist/compiler/detector/kernels/webgl/smoothHistograms.js +0 -49
  87. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.d.ts +0 -6
  88. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.js +0 -56
  89. package/dist/compiler/tensorflow-setup.d.ts +0 -6
  90. package/dist/compiler/tensorflow-setup.js +0 -99
  91. package/src/compiler/compiler-base.js +0 -210
  92. package/src/compiler/compiler.js +0 -25
  93. package/src/compiler/compiler.worker.js +0 -30
  94. package/src/compiler/detector/detector.js +0 -1119
  95. package/src/compiler/detector/kernels/cpu/binomialFilter.js +0 -58
  96. package/src/compiler/detector/kernels/cpu/buildExtremas.js +0 -108
  97. package/src/compiler/detector/kernels/cpu/computeExtremaAngles.js +0 -91
  98. package/src/compiler/detector/kernels/cpu/computeExtremaFreak.js +0 -92
  99. package/src/compiler/detector/kernels/cpu/computeFreakDescriptors.js +0 -68
  100. package/src/compiler/detector/kernels/cpu/computeLocalization.js +0 -67
  101. package/src/compiler/detector/kernels/cpu/computeOrientationHistograms.js +0 -124
  102. package/src/compiler/detector/kernels/cpu/downsampleBilinear.js +0 -33
  103. package/src/compiler/detector/kernels/cpu/extremaReduction.js +0 -53
  104. package/src/compiler/detector/kernels/cpu/fakeShader.js +0 -88
  105. package/src/compiler/detector/kernels/cpu/index.js +0 -26
  106. package/src/compiler/detector/kernels/cpu/prune.js +0 -78
  107. package/src/compiler/detector/kernels/cpu/smoothHistograms.js +0 -57
  108. package/src/compiler/detector/kernels/cpu/upsampleBilinear.js +0 -51
  109. package/src/compiler/detector/kernels/index.js +0 -2
  110. package/src/compiler/detector/kernels/webgl/binomialFilter.js +0 -72
  111. package/src/compiler/detector/kernels/webgl/buildExtremas.js +0 -109
  112. package/src/compiler/detector/kernels/webgl/computeExtremaAngles.js +0 -82
  113. package/src/compiler/detector/kernels/webgl/computeExtremaFreak.js +0 -105
  114. package/src/compiler/detector/kernels/webgl/computeFreakDescriptors.js +0 -56
  115. package/src/compiler/detector/kernels/webgl/computeLocalization.js +0 -70
  116. package/src/compiler/detector/kernels/webgl/computeOrientationHistograms.js +0 -129
  117. package/src/compiler/detector/kernels/webgl/downsampleBilinear.js +0 -50
  118. package/src/compiler/detector/kernels/webgl/extremaReduction.js +0 -50
  119. package/src/compiler/detector/kernels/webgl/index.js +0 -26
  120. package/src/compiler/detector/kernels/webgl/smoothHistograms.js +0 -53
  121. package/src/compiler/detector/kernels/webgl/upsampleBilinear.js +0 -62
  122. package/src/compiler/tensorflow-setup.js +0 -116
@@ -1,1119 +0,0 @@
1
- // result should be similar to previou
2
- // improve freka descriptors computation
3
- import * as tf from "@tensorflow/tfjs";
4
- import { FREAKPOINTS } from "./freak.js";
5
- import "./kernels/webgl/index.js";
6
- const PYRAMID_MIN_SIZE = 8;
7
- const PYRAMID_MAX_OCTAVE = 5;
8
-
9
- const NUM_BUCKETS_PER_DIMENSION = 10;
10
- const MAX_FEATURES_PER_BUCKET = 5;
11
- // total max feature points
12
-
13
- const ORIENTATION_GAUSSIAN_EXPANSION_FACTOR = 3.0;
14
- const ORIENTATION_REGION_EXPANSION_FACTOR = 1.5;
15
- //const FREAK_CONPARISON_COUNT = ((FREAKPOINTS.length - 1) * FREAKPOINTS.length) / 2; // 666
16
-
17
- class Detector {
18
- constructor(width, height, debugMode = false) {
19
- this.debugMode = debugMode;
20
- this.width = width;
21
- this.height = height;
22
- let numOctaves = 0;
23
- while (width >= PYRAMID_MIN_SIZE && height >= PYRAMID_MIN_SIZE) {
24
- width /= 2;
25
- height /= 2;
26
- numOctaves++;
27
- if (numOctaves === PYRAMID_MAX_OCTAVE) break;
28
- }
29
- this.numOctaves = numOctaves;
30
-
31
- this.tensorCaches = {};
32
- this.kernelCaches = {};
33
- }
34
-
35
- // used in compiler
36
- detectImageData(imageData) {
37
- const arr = new Uint8ClampedArray(4 * imageData.length);
38
- for (let i = 0; i < imageData.length; i++) {
39
- arr[4 * i] = imageData[i];
40
- arr[4 * i + 1] = imageData[i];
41
- arr[4 * i + 2] = imageData[i];
42
- arr[4 * i + 3] = 255;
43
- }
44
- const img = new ImageData(arr, this.width, this.height);
45
- return this.detect(img);
46
- }
47
- /**
48
- *
49
- * @param {tf.Tensor<tf.Rank>} inputImageT
50
- * @returns
51
- */
52
- detect(inputImageT) {
53
- let debugExtra = null;
54
-
55
- // Build gaussian pyramid images, two images per octave
56
- /** @type {Array<Array<tf.Tensor<tf.Rank>>} */
57
- const pyramidImagesT = [];
58
- //console.log("Detector::Building pyramid Images...");
59
- for (let i = 0; i < this.numOctaves; i++) {
60
- let image1T;
61
- let image2T;
62
-
63
- if (i === 0) {
64
- image1T = this._applyFilter(inputImageT);
65
- } else {
66
- image1T = this._downsampleBilinear(pyramidImagesT[i - 1][pyramidImagesT[i - 1].length - 1]);
67
- }
68
- image2T = this._applyFilter(image1T);
69
- pyramidImagesT.push([image1T, image2T]);
70
- }
71
- //console.log("Detector::Building dog images...");
72
- // Build difference-of-gaussian (dog) pyramid
73
- /** @type {tf.Tensor<tf.Rank>[]} */
74
- const dogPyramidImagesT = [];
75
- for (let i = 0; i < this.numOctaves; i++) {
76
- let dogImageT = this._differenceImageBinomial(pyramidImagesT[i][0], pyramidImagesT[i][1]);
77
- dogPyramidImagesT.push(dogImageT);
78
- }
79
-
80
- // find local maximum/minimum
81
- /** @type {tf.Tensor<tf.Rank>[]} */
82
- const extremasResultsT = [];
83
- for (let i = 1; i < this.numOctaves - 1; i++) {
84
- const extremasResultT = this._buildExtremas(
85
- dogPyramidImagesT[i - 1],
86
- dogPyramidImagesT[i],
87
- dogPyramidImagesT[i + 1],
88
- );
89
- extremasResultsT.push(extremasResultT);
90
- }
91
-
92
- // divide the input into N by N buckets, and for each bucket,
93
- // collect the top 5 most significant extrema across extremas in all scale level
94
- // result would be NUM_BUCKETS x NUM_FEATURES_PER_BUCKET extremas
95
- const prunedExtremasList = this._applyPrune(extremasResultsT);
96
-
97
- const prunedExtremasT = this._computeLocalization(prunedExtremasList, dogPyramidImagesT);
98
-
99
- // compute the orientation angle for each pruned extremas
100
- const extremaHistogramsT = this._computeOrientationHistograms(prunedExtremasT, pyramidImagesT);
101
-
102
- const smoothedHistogramsT = this._smoothHistograms(extremaHistogramsT);
103
- const extremaAnglesT = this._computeExtremaAngles(smoothedHistogramsT);
104
-
105
- // to compute freak descriptors, we first find the pixel value of 37 freak points for each extrema
106
- const extremaFreaksT = this._computeExtremaFreak(
107
- pyramidImagesT,
108
- prunedExtremasT,
109
- extremaAnglesT,
110
- );
111
-
112
- // compute the binary descriptors
113
- const freakDescriptorsT = this._computeFreakDescriptors(extremaFreaksT);
114
-
115
- const prunedExtremasArr = prunedExtremasT.arraySync();
116
- const extremaAnglesArr = extremaAnglesT.arraySync();
117
- const freakDescriptorsArr = freakDescriptorsT.arraySync();
118
-
119
- if (this.debugMode) {
120
- debugExtra = {
121
- pyramidImages: pyramidImagesT.map((ts) => ts.map((t) => t.arraySync())),
122
- dogPyramidImages: dogPyramidImagesT.map((t) => (t ? t.arraySync() : null)),
123
- extremasResults: extremasResultsT.map((t) => t.arraySync()),
124
- extremaAngles: extremaAnglesT.arraySync(),
125
- prunedExtremas: prunedExtremasList,
126
- localizedExtremas: prunedExtremasT.arraySync(),
127
- };
128
- }
129
-
130
- pyramidImagesT.forEach((ts) => ts.forEach((t) => t.dispose()));
131
- dogPyramidImagesT.forEach((t) => t && t.dispose());
132
- extremasResultsT.forEach((t) => t.dispose());
133
- prunedExtremasT.dispose();
134
- extremaHistogramsT.dispose();
135
- smoothedHistogramsT.dispose();
136
- extremaAnglesT.dispose();
137
- extremaFreaksT.dispose();
138
- freakDescriptorsT.dispose();
139
-
140
- const featurePoints = [];
141
-
142
- for (let i = 0; i < prunedExtremasArr.length; i++) {
143
- if (prunedExtremasArr[i][0] == 0) continue;
144
-
145
- const descriptors = [];
146
- for (let m = 0; m < freakDescriptorsArr[i].length; m += 4) {
147
- const v1 = freakDescriptorsArr[i][m];
148
- const v2 = freakDescriptorsArr[i][m + 1];
149
- const v3 = freakDescriptorsArr[i][m + 2];
150
- const v4 = freakDescriptorsArr[i][m + 3];
151
-
152
- let combined = v1 * 16777216 + v2 * 65536 + v3 * 256 + v4;
153
- //if (m === freakDescriptorsArr[i].length-4) { // last one, legacy reason
154
- // combined /= 32;
155
- //}
156
- descriptors.push(combined);
157
- }
158
-
159
- const octave = prunedExtremasArr[i][1];
160
- const y = prunedExtremasArr[i][2];
161
- const x = prunedExtremasArr[i][3];
162
- const originalX = x * Math.pow(2, octave) + Math.pow(2, octave - 1) - 0.5;
163
- const originalY = y * Math.pow(2, octave) + Math.pow(2, octave - 1) - 0.5;
164
- const scale = Math.pow(2, octave);
165
-
166
- featurePoints.push({
167
- maxima: prunedExtremasArr[i][0] > 0,
168
- x: originalX,
169
- y: originalY,
170
- scale: scale,
171
- angle: extremaAnglesArr[i],
172
- descriptors: descriptors,
173
- });
174
- }
175
- //console.log("feature points", featurePoints);
176
- //console.table(tf.memory());
177
- return { featurePoints, debugExtra };
178
- }
179
-
180
- _computeFreakDescriptors(extremaFreaks) {
181
- if (!this.tensorCaches.computeFreakDescriptors) {
182
- const in1Arr = [];
183
- const in2Arr = [];
184
- for (let k1 = 0; k1 < extremaFreaks.shape[1]; k1++) {
185
- for (let k2 = k1 + 1; k2 < extremaFreaks.shape[1]; k2++) {
186
- in1Arr.push(k1);
187
- in2Arr.push(k2);
188
- }
189
- }
190
- const in1 = tf.tensor(in1Arr, [in1Arr.length]).cast("int32");
191
- const in2 = tf.tensor(in2Arr, [in2Arr.length]).cast("int32");
192
-
193
- this.tensorCaches.computeFreakDescriptors = {
194
- positionT: tf.keep(tf.stack([in1, in2], 1)),
195
- };
196
- }
197
- const { positionT } = this.tensorCaches.computeFreakDescriptors;
198
-
199
- // encode 8 bits into one number
200
- // trying to encode 16 bits give wrong result in iOS. may integer precision issue
201
- /*
202
- if (!this.kernelCaches.computeFreakDescriptors) {
203
- const kernel = {
204
- variableNames: ['freak', 'p'],
205
- outputShape: [extremaFreaks.shape[0], descriptorCount],
206
- userCode: `
207
- void main() {
208
- ivec2 coords = getOutputCoords();
209
- int featureIndex = coords[0];
210
- int descIndex = coords[1] * 8;
211
-
212
- int sum = 0;
213
- for (int i = 0; i < 8; i++) {
214
- if (descIndex + i >= ${ FREAK_CONPARISON_COUNT }) {
215
- continue;
216
- }
217
-
218
- int p1 = int(getP(descIndex + i, 0));
219
- int p2 = int(getP(descIndex + i, 1));
220
-
221
- float v1 = getFreak(featureIndex, p1);
222
- float v2 = getFreak(featureIndex, p2);
223
-
224
- if (v1 < v2 + 0.01) {
225
- sum += int(pow(2.0, float(7 - i)));
226
- }
227
- }
228
- setOutput(float(sum));
229
- }
230
- `
231
- }
232
- this.kernelCaches.computeFreakDescriptors = [kernel];
233
- }
234
- */
235
- return tf.tidy(() => {
236
- //const [program] = this.kernelCaches.computeFreakDescriptors;
237
- //return this._runWebGLProgram(program, [extremaFreaks, positionT], 'int32');
238
- return tf.engine().runKernel("ComputeFreakDescriptors", { extremaFreaks, positionT });
239
- });
240
- }
241
-
242
- _computeExtremaFreak(pyramidImagesT, prunedExtremas, prunedExtremasAngles) {
243
- if (!this.tensorCaches._computeExtremaFreak) {
244
- tf.tidy(() => {
245
- const freakPoints = tf.tensor(FREAKPOINTS);
246
- this.tensorCaches._computeExtremaFreak = {
247
- freakPointsT: tf.keep(freakPoints),
248
- };
249
- });
250
- }
251
- const { freakPointsT } = this.tensorCaches._computeExtremaFreak;
252
-
253
- const gaussianImagesT = [];
254
- for (let i = 1; i < pyramidImagesT.length; i++) {
255
- //gaussianImagesT.push(pyramidImagesT[i][0]);
256
- gaussianImagesT.push(pyramidImagesT[i][1]); // better
257
- }
258
-
259
- /* if (!this.kernelCaches._computeExtremaFreak) {
260
- const imageVariableNames = [];
261
- for (let i = 1; i < pyramidImagesT.length; i++) {
262
- imageVariableNames.push('image' + i);
263
- }
264
-
265
- let pixelsSubCodes = `float getPixel(int octave, int y, int x) {
266
- `;
267
- for (let i = 1; i < pyramidImagesT.length; i++) {
268
- pixelsSubCodes += `
269
- if (octave == ${ i }) {
270
- return getImage${ i } (y, x);
271
- }
272
- `
273
- }
274
- pixelsSubCodes += `} `;
275
-
276
- const kernel = {
277
- variableNames: [...imageVariableNames, 'extrema', 'angles', 'freakPoints'],
278
- outputShape: [prunedExtremas.shape[0], FREAKPOINTS.length],
279
- userCode: `
280
- ${ pixelsSubCodes }
281
- void main() {
282
- ivec2 coords = getOutputCoords();
283
- int featureIndex = coords[0];
284
- int freakIndex = coords[1];
285
-
286
- float freakSigma = getFreakPoints(freakIndex, 0);
287
- float freakX = getFreakPoints(freakIndex, 1);
288
- float freakY = getFreakPoints(freakIndex, 2);
289
-
290
- int octave = int(getExtrema(featureIndex, 1));
291
- float inputY = getExtrema(featureIndex, 2);
292
- float inputX = getExtrema(featureIndex, 3);
293
- float inputAngle = getAngles(featureIndex);
294
- float cos = ${ FREAK_EXPANSION_FACTOR }. * cos(inputAngle);
295
- float sin = ${ FREAK_EXPANSION_FACTOR }. * sin(inputAngle);
296
-
297
- float yp = inputY + freakX * sin + freakY * cos;
298
- float xp = inputX + freakX * cos + freakY * -sin;
299
-
300
- int x0 = int(floor(xp));
301
- int x1 = x0 + 1;
302
- int y0 = int(floor(yp));
303
- int y1 = y0 + 1;
304
-
305
- float f1 = getPixel(octave, y0, x0);
306
- float f2 = getPixel(octave, y0, x1);
307
- float f3 = getPixel(octave, y1, x0);
308
- float f4 = getPixel(octave, y1, x1);
309
-
310
- float x1f = float(x1);
311
- float y1f = float(y1);
312
- float x0f = float(x0);
313
- float y0f = float(y0);
314
-
315
- // ratio for interpolation between four neighbouring points
316
- float value = (x1f - xp) * (y1f - yp) * f1
317
- + (xp - x0f) * (y1f - yp) * f2
318
- + (x1f - xp) * (yp - y0f) * f3
319
- + (xp - x0f) * (yp - y0f) * f4;
320
-
321
- setOutput(value);
322
- }
323
- `
324
- }
325
-
326
- this.kernelCaches._computeExtremaFreak = [kernel];
327
- } */
328
-
329
- return tf.tidy(() => {
330
- /* const [program] = this.kernelCaches._computeExtremaFreak;
331
- const result = this._compileAndRun(program, [...gaussianImagesT, prunedExtremas, prunedExtremasAngles, freakPointsT]);
332
- return result; */
333
- return tf.engine().runKernel("ComputeExtremaFreak", {
334
- gaussianImagesT,
335
- prunedExtremas,
336
- prunedExtremasAngles,
337
- freakPointsT,
338
- pyramidImagesLength: pyramidImagesT.length,
339
- });
340
- });
341
- }
342
- /**
343
- *
344
- * @param {tf.Tensor<tf.Rank>} histograms
345
- * @returns
346
- */
347
- _computeExtremaAngles(histograms) {
348
- /* if (!this.kernelCaches.computeExtremaAngles) {
349
- const kernel = {
350
- variableNames: ['histogram'],
351
- outputShape: [histograms.shape[0]],
352
- userCode: `
353
- void main() {
354
- int featureIndex = getOutputCoords();
355
-
356
- int maxIndex = 0;
357
- for (int i = 1; i < ${ ORIENTATION_NUM_BINS }; i++) {
358
- if (getHistogram(featureIndex, i) > getHistogram(featureIndex, maxIndex)) {
359
- maxIndex = i;
360
- }
361
- }
362
-
363
- int prev = imod(maxIndex - 1 + ${ ORIENTATION_NUM_BINS }, ${ ORIENTATION_NUM_BINS });
364
- int next = imod(maxIndex + 1, ${ ORIENTATION_NUM_BINS });
365
-
366
- **
367
- * Fit a quatratic to 3 points.The system of equations is:
368
- *
369
- * y0 = A * x0 ^ 2 + B * x0 + C
370
- * y1 = A * x1 ^ 2 + B * x1 + C
371
- * y2 = A * x2 ^ 2 + B * x2 + C
372
- *
373
- * This system of equations is solved for A, B, C.
374
- *
375
- float p10 = float(maxIndex - 1);
376
- float p11 = getHistogram(featureIndex, prev);
377
- float p20 = float(maxIndex);
378
- float p21 = getHistogram(featureIndex, maxIndex);
379
- float p30 = float(maxIndex + 1);
380
- float p31 = getHistogram(featureIndex, next);
381
-
382
- float d1 = (p30 - p20) * (p30 - p10);
383
- float d2 = (p10 - p20) * (p30 - p10);
384
- float d3 = p10 - p20;
385
-
386
- // If any of the denominators are zero then, just use maxIndex.
387
- float fbin = float(maxIndex);
388
- if (abs(d1) > 0.00001 && abs(d2) > 0.00001 && abs(d3) > 0.00001) {
389
- float a = p10 * p10;
390
- float b = p20 * p20;
391
-
392
- // Solve for the coefficients A,B,C
393
- float A = ((p31 - p21) / d1) - ((p11 - p21) / d2);
394
- float B = ((p11 - p21) + (A * (b - a))) / d3;
395
- float C = p11 - (A * a) - (B * p10);
396
- fbin = -B / (2. * A);
397
- }
398
-
399
- float an = 2.0 * ${ Math.PI } * (fbin + 0.5) / ${ ORIENTATION_NUM_BINS }.- ${ Math.PI };
400
- setOutput(an);
401
- }
402
- `
403
- }
404
- this.kernelCaches.computeExtremaAngles = kernel;
405
- } */
406
- return tf.tidy(() => {
407
- /* const program = this.kernelCaches.computeExtremaAngles;
408
- return this._compileAndRun(program, [histograms]); */
409
- return tf.engine().runKernel("ComputeExtremaAngles", { histograms });
410
- });
411
- }
412
-
413
- // TODO: maybe can try just using average momentum, instead of histogram method. histogram might be overcomplicated
414
- /**
415
- *
416
- * @param {tf.Tensor<tf.Rank>} prunedExtremasT
417
- * @param {tf.Tensor<tf.Rank>[]} pyramidImagesT
418
- * @returns
419
- */
420
- _computeOrientationHistograms(prunedExtremasT, pyramidImagesT) {
421
- const gaussianImagesT = [];
422
- for (let i = 1; i < pyramidImagesT.length; i++) {
423
- gaussianImagesT.push(pyramidImagesT[i][1]);
424
- }
425
-
426
- if (!this.tensorCaches.orientationHistograms) {
427
- tf.tidy(() => {
428
- const gwScale =
429
- -1.0 /
430
- (2 * ORIENTATION_GAUSSIAN_EXPANSION_FACTOR * ORIENTATION_GAUSSIAN_EXPANSION_FACTOR);
431
- const radius = ORIENTATION_GAUSSIAN_EXPANSION_FACTOR * ORIENTATION_REGION_EXPANSION_FACTOR;
432
- const radiusCeil = Math.ceil(radius);
433
-
434
- const radialProperties = [];
435
- for (let y = -radiusCeil; y <= radiusCeil; y++) {
436
- for (let x = -radiusCeil; x <= radiusCeil; x++) {
437
- const distanceSquare = x * x + y * y;
438
-
439
- // may just assign w = 1 will do, this could be over complicated.
440
- if (distanceSquare <= radius * radius) {
441
- const _x = distanceSquare * gwScale;
442
- // fast expontenial approx
443
- let w =
444
- (720 + _x * (720 + _x * (360 + _x * (120 + _x * (30 + _x * (6 + _x)))))) *
445
- 0.0013888888;
446
- radialProperties.push([y, x, w]);
447
- }
448
- }
449
- }
450
-
451
- this.tensorCaches.orientationHistograms = {
452
- radialPropertiesT: tf.keep(tf.tensor(radialProperties, [radialProperties.length, 3])),
453
- };
454
- });
455
- }
456
- const { radialPropertiesT } = this.tensorCaches.orientationHistograms;
457
-
458
- /* if (!this.kernelCaches.computeOrientationHistograms) {
459
- const imageVariableNames = [];
460
- for (let i = 1; i < pyramidImagesT.length; i++) {
461
- imageVariableNames.push('image' + i);
462
- }
463
-
464
- let kernel1SubCodes = `float getPixel(int octave, int y, int x) {
465
- `;
466
- for (let i = 1; i < pyramidImagesT.length; i++) {
467
- kernel1SubCodes += `
468
- if (octave == ${ i }) {
469
- return getImage${ i } (y, x);
470
- }
471
- `
472
- }
473
- kernel1SubCodes += `} `;
474
-
475
- const kernel1 = {
476
- variableNames: [...imageVariableNames, 'extrema', 'radial'],
477
- outputShape: [prunedExtremasT.shape[0], radialPropertiesT.shape[0], 2], // last dimension: [fbin, magnitude]
478
- userCode: `
479
- ${ kernel1SubCodes }
480
-
481
- void main() {
482
- ivec3 coords = getOutputCoords();
483
- int featureIndex = coords[0];
484
- int radialIndex = coords[1];
485
- int propertyIndex = coords[2];
486
-
487
- int radialY = int(getRadial(radialIndex, 0));
488
- int radialX = int(getRadial(radialIndex, 1));
489
- float radialW = getRadial(radialIndex, 2);
490
-
491
- int octave = int(getExtrema(featureIndex, 1));
492
- int y = int(getExtrema(featureIndex, 2));
493
- int x = int(getExtrema(featureIndex, 3));
494
-
495
- int xp = x + radialX;
496
- int yp = y + radialY;
497
-
498
- float dy = getPixel(octave, yp + 1, xp) - getPixel(octave, yp - 1, xp);
499
- float dx = getPixel(octave, yp, xp + 1) - getPixel(octave, yp, xp - 1);
500
-
501
- if (propertyIndex == 0) {
502
- // be careful that atan(0, 0) gives 1.57 instead of 0 (different from js), but doesn't matter here, coz magnitude is 0
503
-
504
- float angle = atan(dy, dx) + ${ Math.PI };
505
- float fbin = angle * ${ ORIENTATION_NUM_BINS }. * ${ oneOver2PI };
506
- setOutput(fbin);
507
- return;
508
- }
509
-
510
- if (propertyIndex == 1) {
511
- float mag = sqrt(dx * dx + dy * dy);
512
- float magnitude = radialW * mag;
513
- setOutput(magnitude);
514
- return;
515
- }
516
- }
517
-
518
- `
519
- }
520
-
521
- const kernel2 = {
522
- variableNames: ['fbinMag'],
523
- outputShape: [prunedExtremasT.shape[0], ORIENTATION_NUM_BINS],
524
- userCode: `
525
- void main() {
526
- ivec2 coords = getOutputCoords();
527
- int featureIndex = coords[0];
528
- int binIndex = coords[1];
529
-
530
- float sum = 0.;
531
- for (int i = 0; i < ${ radialPropertiesT.shape[0] }; i++) {
532
- float fbin = getFbinMag(featureIndex, i, 0);
533
- int bin = int(floor(fbin - 0.5));
534
- int b1 = imod(bin + ${ ORIENTATION_NUM_BINS }, ${ ORIENTATION_NUM_BINS });
535
- int b2 = imod(bin + 1 + ${ ORIENTATION_NUM_BINS }, ${ ORIENTATION_NUM_BINS });
536
-
537
- if (b1 == binIndex || b2 == binIndex) {
538
- float magnitude = getFbinMag(featureIndex, i, 1);
539
- float w2 = fbin - float(bin) - 0.5;
540
- float w1 = w2 * -1. + 1.;
541
-
542
- if (b1 == binIndex) {
543
- sum += w1 * magnitude;
544
- }
545
- if (b2 == binIndex) {
546
- sum += w2 * magnitude;
547
- }
548
- }
549
- }
550
- setOutput(sum);
551
- }
552
- `
553
- }
554
-
555
- this.kernelCaches.computeOrientationHistograms = [kernel1, kernel2];
556
- } */
557
-
558
- return tf.tidy(() => {
559
- /* const [program1, program2] = this.kernelCaches.computeOrientationHistograms;
560
- const result1 = this._compileAndRun(program1, [...gaussianImagesT, prunedExtremasT, radialPropertiesT]);
561
- const result2 = this._compileAndRun(program2, [result1]);
562
- return result2;*/
563
- return tf.engine().runKernel("ComputeOrientationHistograms", {
564
- gaussianImagesT,
565
- prunedExtremasT,
566
- radialPropertiesT,
567
- pyramidImagesLength: pyramidImagesT.length,
568
- });
569
- });
570
- }
571
-
572
- // The histogram is smoothed with a Gaussian, with sigma = 1
573
- _smoothHistograms(histograms) {
574
- /* if (!this.kernelCaches.smoothHistograms) {
575
- const kernel = {
576
- variableNames: ['histogram'],
577
- outputShape: [histograms.shape[0], ORIENTATION_NUM_BINS],
578
- userCode: `
579
- void main() {
580
- ivec2 coords = getOutputCoords();
581
-
582
- int featureIndex = coords[0];
583
- int binIndex = coords[1];
584
-
585
- int prevBin = imod(binIndex - 1 + ${ ORIENTATION_NUM_BINS }, ${ ORIENTATION_NUM_BINS });
586
- int nextBin = imod(binIndex + 1, ${ ORIENTATION_NUM_BINS });
587
-
588
- float result = 0.274068619061197 * getHistogram(featureIndex, prevBin) + 0.451862761877606 * getHistogram(featureIndex, binIndex) + 0.274068619061197 * getHistogram(featureIndex, nextBin);
589
-
590
- setOutput(result);
591
- }
592
- `
593
- }
594
- this.kernelCaches.smoothHistograms = kernel;
595
- } */
596
- return tf.tidy(() => {
597
- return tf.engine().runKernel("SmoothHistograms", { histograms }); //
598
- /* const program = this.kernelCaches.smoothHistograms;
599
- for (let i = 0; i < ORIENTATION_SMOOTHING_ITERATIONS; i++) {
600
- histograms = this._compileAndRun(program, [histograms]);
601
- }
602
- return histograms; */
603
- });
604
- }
605
- /**
606
- *
607
- * @param {number[][]} prunedExtremasList
608
- * @param {tf.Tensor<tf.Rank>[]} dogPyramidImagesT
609
- * @returns
610
- */
611
- _computeLocalization(prunedExtremasList, dogPyramidImagesT) {
612
- /* if (!this.kernelCaches.computeLocalization) {
613
- const dogVariableNames = [];
614
-
615
- let dogSubCodes = `float getPixel(int octave, int y, int x) {
616
- `;
617
- for (let i = 1; i < dogPyramidImagesT.length; i++) { // extrema starts from second octave
618
- dogVariableNames.push('image' + i);
619
- dogSubCodes += `
620
- if (octave == ${ i }) {
621
- return getImage${ i } (y, x);
622
- }
623
- `;
624
- }
625
- dogSubCodes += `} `;
626
-
627
- const kernel = {
628
- variableNames: [...dogVariableNames, 'extrema'],
629
- outputShape: [prunedExtremasList.length, 3, 3], // 3x3 pixels around the extrema
630
- userCode: `
631
- ${ dogSubCodes }
632
-
633
- void main() {
634
- ivec3 coords = getOutputCoords();
635
- int featureIndex = coords[0];
636
- float score = getExtrema(featureIndex, 0);
637
- if (score == 0.0) {
638
- return;
639
- }
640
-
641
- int dy = coords[1] - 1;
642
- int dx = coords[2] - 1;
643
- int octave = int(getExtrema(featureIndex, 1));
644
- int y = int(getExtrema(featureIndex, 2));
645
- int x = int(getExtrema(featureIndex, 3));
646
- setOutput(getPixel(octave, y + dy, x + dx));
647
- }
648
- `
649
- }
650
-
651
- this.kernelCaches.computeLocalization = [kernel];
652
- } */
653
-
654
- return tf.tidy(() => {
655
- //const program = this.kernelCaches.computeLocalization[0];
656
- //const prunedExtremasT = tf.tensor(prunedExtremasList, [prunedExtremasList.length, prunedExtremasList[0].length], 'int32');
657
-
658
- const pixelsT = tf
659
- .engine()
660
- .runKernel("ComputeLocalization", { prunedExtremasList, dogPyramidImagesT }); //this._compileAndRun(program, [...dogPyramidImagesT.slice(1), prunedExtremasT]);
661
- const pixels = pixelsT.arraySync();
662
-
663
- const result = [];
664
- for (let i = 0; i < pixels.length; i++) {
665
- result.push([]);
666
- for (let j = 0; j < pixels[i].length; j++) {
667
- result[i].push([]);
668
- }
669
- }
670
-
671
- const localizedExtremas = [];
672
- for (let i = 0; i < prunedExtremasList.length; i++) {
673
- localizedExtremas[i] = [
674
- prunedExtremasList[i][0],
675
- prunedExtremasList[i][1],
676
- prunedExtremasList[i][2],
677
- prunedExtremasList[i][3],
678
- ];
679
- }
680
-
681
- for (let i = 0; i < localizedExtremas.length; i++) {
682
- if (localizedExtremas[i][0] === 0) {
683
- continue;
684
- }
685
- const pixel = pixels[i];
686
- const dx = 0.5 * (pixel[1][2] - pixel[1][0]);
687
- const dy = 0.5 * (pixel[2][1] - pixel[0][1]);
688
- const dxx = pixel[1][2] + pixel[1][0] - 2 * pixel[1][1];
689
- const dyy = pixel[2][1] + pixel[0][1] - 2 * pixel[1][1];
690
- const dxy = 0.25 * (pixel[0][0] + pixel[2][2] - pixel[0][2] - pixel[2][0]);
691
-
692
- const det = dxx * dyy - dxy * dxy;
693
- const ux = (dyy * -dx + -dxy * -dy) / det;
694
- const uy = (-dxy * -dx + dxx * -dy) / det;
695
-
696
- const newY = localizedExtremas[i][2] + uy;
697
- const newX = localizedExtremas[i][3] + ux;
698
-
699
- if (Math.abs(det) < 0.0001) {
700
- continue;
701
- }
702
-
703
- localizedExtremas[i][2] = newY;
704
- localizedExtremas[i][3] = newX;
705
- }
706
- return tf.tensor(
707
- localizedExtremas,
708
- [localizedExtremas.length, localizedExtremas[0].length],
709
- "float32",
710
- );
711
- });
712
- }
713
-
714
- // faster to do it in CPU
715
- // if we do in gpu, we probably need to use tf.topk(), which seems to be run in CPU anyway (no gpu operation for that)
716
- // TODO: research adapative maximum supression method
717
- /**
718
- *
719
- * @param {tf.Tensor<tf.Rank>[]} extremasResultsT
720
- * @returns
721
- */
722
- _applyPrune(extremasResultsT) {
723
- const nBuckets = NUM_BUCKETS_PER_DIMENSION * NUM_BUCKETS_PER_DIMENSION;
724
- const nFeatures = MAX_FEATURES_PER_BUCKET;
725
- /*
726
- if (!this.kernelCaches.applyPrune) {
727
- const reductionKernels = [];
728
-
729
- // to reduce to amount of data that need to sync back to CPU by 4 times, we apply this trick:
730
- // the fact that there is not possible to have consecutive maximum/minimum, we can safe combine 4 pixels into 1
731
- for (let k = 0; k < extremasResultsT.length; k++) {
732
- const extremaHeight = extremasResultsT[k].shape[0];
733
- const extremaWidth = extremasResultsT[k].shape[1];
734
-
735
- const kernel = {
736
- variableNames: ['extrema'],
737
- outputShape: [Math.floor(extremaHeight/2), Math.floor(extremaWidth/2)],
738
- userCode: `
739
- void main() {
740
- ivec2 coords = getOutputCoords();
741
- int y = coords[0] * 2;
742
- int x = coords[1] * 2;
743
-
744
- float location = 0.0;
745
- float values = getExtrema(y, x);
746
-
747
- if (getExtrema(y + 1, x) != 0.0) {
748
- location = 1.0;
749
- values = getExtrema(y + 1, x);
750
- }
751
- else if (getExtrema(y, x + 1) != 0.0) {
752
- location = 2.0;
753
- values = getExtrema(y, x + 1);
754
- }
755
- else if (getExtrema(y + 1, x + 1) != 0.0) {
756
- location = 3.0;
757
- values = getExtrema(y + 1, x + 1);
758
- }
759
-
760
- if (values < 0.0) {
761
- setOutput(location * -1000.0 + values);
762
- } else {
763
- setOutput(location * 1000.0 + values);
764
- }
765
- }
766
- `
767
- }
768
- reductionKernels.push(kernel);
769
- }
770
- this.kernelCaches.applyPrune = {reductionKernels};
771
- }
772
- */
773
- // combine results into a tensor of:
774
- // nBuckets x nFeatures x [score, octave, y, x]
775
- const curAbsScores = [];
776
- /** @type {number[][][]} */
777
- const result = [];
778
- for (let i = 0; i < nBuckets; i++) {
779
- result.push([]);
780
- curAbsScores.push([]);
781
- for (let j = 0; j < nFeatures; j++) {
782
- result[i].push([0, 0, 0, 0]);
783
- curAbsScores[i].push(0);
784
- }
785
- }
786
-
787
- tf.tidy(() => {
788
- //const {reductionKernels} = this.kernelCaches.applyPrune;
789
-
790
- for (let k = 0; k < extremasResultsT.length; k++) {
791
- //const program = reductionKernels[k];
792
- //const reducedT = this._compileAndRun(program, [extremasResultsT[k]]);
793
- const reducedT = tf
794
- .engine()
795
- .runKernel("ExtremaReduction", { extremasResultT: extremasResultsT[k] });
796
- const octave = k + 1; // extrema starts from second octave
797
-
798
- const reduced = reducedT.arraySync();
799
- const height = reducedT.shape[0];
800
- const width = reducedT.shape[1];
801
-
802
- const bucketWidth = (width * 2) / NUM_BUCKETS_PER_DIMENSION;
803
- const bucketHeight = (height * 2) / NUM_BUCKETS_PER_DIMENSION;
804
-
805
- for (let j = 0; j < height; j++) {
806
- for (let i = 0; i < width; i++) {
807
- const encoded = reduced[j][i];
808
- if (encoded == 0) continue;
809
-
810
- const score = encoded % 1000;
811
- const loc = Math.floor(Math.abs(encoded) / 1000);
812
- const x = i * 2 + (loc === 2 || loc === 3 ? 1 : 0);
813
- const y = j * 2 + (loc === 1 || loc === 3 ? 1 : 0);
814
-
815
- const bucketX = Math.floor(x / bucketWidth);
816
- const bucketY = Math.floor(y / bucketHeight);
817
- const bucket = bucketY * NUM_BUCKETS_PER_DIMENSION + bucketX;
818
-
819
- const absScore = Math.abs(score);
820
-
821
- let tIndex = nFeatures;
822
- while (tIndex >= 1 && absScore > curAbsScores[bucket][tIndex - 1]) {
823
- tIndex -= 1;
824
- }
825
-
826
- if (tIndex < nFeatures) {
827
- for (let t = nFeatures - 1; t >= tIndex + 1; t--) {
828
- curAbsScores[bucket][t] = curAbsScores[bucket][t - 1];
829
- result[bucket][t][0] = result[bucket][t - 1][0];
830
- result[bucket][t][1] = result[bucket][t - 1][1];
831
- result[bucket][t][2] = result[bucket][t - 1][2];
832
- result[bucket][t][3] = result[bucket][t - 1][3];
833
- }
834
- curAbsScores[bucket][tIndex] = absScore;
835
- result[bucket][tIndex][0] = score;
836
- result[bucket][tIndex][1] = octave;
837
- result[bucket][tIndex][2] = y;
838
- result[bucket][tIndex][3] = x;
839
- }
840
- } //for j<height
841
- } //for i<width
842
- }
843
- });
844
-
845
- // combine all buckets into a single list
846
- const list = [];
847
- for (let i = 0; i < nBuckets; i++) {
848
- for (let j = 0; j < nFeatures; j++) {
849
- list.push(result[i][j]);
850
- }
851
- }
852
- return list;
853
- }
854
-
855
- _buildExtremas(image0, image1, image2) {
856
- /* const imageHeight = image1.shape[0];
857
- const imageWidth = image1.shape[1];
858
-
859
- const kernelKey = 'w' + imageWidth;
860
-
861
- if (!this.kernelCaches.buildExtremas) {
862
- this.kernelCaches.buildExtremas = {};
863
- }
864
- if (!this.kernelCaches.buildExtremas[kernelKey]) {
865
- const kernel = {
866
- variableNames: ['image0', 'image1', 'image2'],
867
- outputShape: [imageHeight, imageWidth],
868
- userCode: `
869
- void main() {
870
- ivec2 coords = getOutputCoords();
871
-
872
- int y = coords[0];
873
- int x = coords[1];
874
-
875
- float value = getImage1(y, x);
876
-
877
- // Step 1: find local maxima/minima
878
- if (value * value < ${ LAPLACIAN_SQR_THRESHOLD }.) {
879
- setOutput(0.);
880
- return;
881
- }
882
- if (y < ${ FREAK_EXPANSION_FACTOR } || y > ${ imageHeight - 1 - FREAK_EXPANSION_FACTOR }) {
883
- setOutput(0.);
884
- return;
885
- }
886
- if (x < ${ FREAK_EXPANSION_FACTOR } || x > ${ imageWidth - 1 - FREAK_EXPANSION_FACTOR }) {
887
- setOutput(0.);
888
- return;
889
- }
890
-
891
- bool isMax = true;
892
- bool isMin = true;
893
- for (int dy = -1; dy <= 1; dy++) {
894
- for (int dx = -1; dx <= 1; dx++) {
895
- float value0 = getImage0(y + dy, x + dx);
896
- float value1 = getImage1(y + dy, x + dx);
897
- float value2 = getImage2(y + dy, x + dx);
898
-
899
- if (value < value0 || value < value1 || value < value2) {
900
- isMax = false;
901
- }
902
- if (value > value0 || value > value1 || value > value2) {
903
- isMin = false;
904
- }
905
- }
906
- }
907
-
908
- if (!isMax && !isMin) {
909
- setOutput(0.);
910
- return;
911
- }
912
-
913
- // compute edge score and reject based on threshold
914
- float dxx = getImage1(y, x + 1) + getImage1(y, x - 1) - 2. * getImage1(y, x);
915
- float dyy = getImage1(y + 1, x) + getImage1(y - 1, x) - 2. * getImage1(y, x);
916
- float dxy = 0.25 * (getImage1(y - 1, x - 1) + getImage1(y + 1, x + 1) - getImage1(y - 1, x + 1) - getImage1(y + 1, x - 1));
917
-
918
- float det = (dxx * dyy) - (dxy * dxy);
919
-
920
- if (abs(det) < 0.0001) { // determinant undefined. no solution
921
- setOutput(0.);
922
- return;
923
- }
924
-
925
- float edgeScore = (dxx + dyy) * (dxx + dyy) / det;
926
-
927
- if (abs(edgeScore) >= ${ EDGE_HESSIAN_THRESHOLD } ) {
928
- setOutput(0.);
929
- return;
930
- }
931
- setOutput(getImage1(y, x));
932
- }
933
- `
934
- };
935
- this.kernelCaches.buildExtremas[kernelKey] = kernel;
936
- } */
937
-
938
- return tf.tidy(() => {
939
- return tf.engine().runKernel("BuildExtremas", { image0, image1, image2 });
940
- /* const program = this.kernelCaches.buildExtremas[kernelKey];
941
- image0 = this._downsampleBilinear(image0);
942
- image2 = this._upsampleBilinear(image2, image1); */
943
- //this._compileAndRun(program, [image0, image1, image2]);
944
- //return this._runWebGLProgram(program, [image0, image1, image2], 'float32');
945
- });
946
- }
947
- /**
948
- *
949
- * @param {tf.Tensor<tf.Rank>} image1
950
- * @param {tf.Tensor<tf.Rank>} image2
951
- * @returns
952
- */
953
- _differenceImageBinomial(image1, image2) {
954
- return tf.tidy(() => {
955
- return image1.sub(image2);
956
- });
957
- }
958
-
959
- // 4th order binomail filter [1,4,6,4,1] X [1,4,6,4,1]
960
- _applyFilter(image) {
961
- /* const imageHeight = image.shape[0];
962
- const imageWidth = image.shape[1];
963
-
964
- const kernelKey = 'w' + imageWidth;
965
- if (!this.kernelCaches.applyFilter) {
966
- this.kernelCaches.applyFilter = {};
967
- }
968
-
969
- if (!this.kernelCaches.applyFilter[kernelKey]) {
970
- const kernel1 = {
971
- variableNames: ['p'],
972
- outputShape: [imageHeight, imageWidth],
973
- userCode: `
974
- void main() {
975
- ivec2 coords = getOutputCoords();
976
-
977
- float sum = getP(coords[0], coords[1] - 2);
978
- sum += getP(coords[0], coords[1] - 1) * 4.;
979
- sum += getP(coords[0], coords[1]) * 6.;
980
- sum += getP(coords[0], coords[1] + 1) * 4.;
981
- sum += getP(coords[0], coords[1] + 2);
982
- setOutput(sum);
983
- }
984
- `
985
- };
986
-
987
- const kernel2 = {
988
- variableNames: ['p'],
989
- outputShape: [imageHeight, imageWidth],
990
- userCode: `
991
- void main() {
992
- ivec2 coords = getOutputCoords();
993
-
994
- float sum = getP(coords[0] - 2, coords[1]);
995
- sum += getP(coords[0] - 1, coords[1]) * 4.;
996
- sum += getP(coords[0], coords[1]) * 6.;
997
- sum += getP(coords[0] + 1, coords[1]) * 4.;
998
- sum += getP(coords[0] + 2, coords[1]);
999
- sum /= 256.;
1000
- setOutput(sum);
1001
- }
1002
- `
1003
- };
1004
- this.kernelCaches.applyFilter[kernelKey] = [kernel1, kernel2];
1005
- }
1006
- */
1007
- return tf.tidy(() => {
1008
- /* const [program1, program2] = this.kernelCaches.applyFilter[kernelKey];
1009
-
1010
- const result1 = this._compileAndRun(program1, [image]);
1011
- const result2 = this._compileAndRun(program2, [result1]);
1012
- return result2; */
1013
- return tf.engine().runKernel("BinomialFilter", { image });
1014
- });
1015
- }
1016
-
1017
- /* _upsampleBilinear(image, targetImage) {
1018
- const imageHeight = image.shape[0];
1019
- const imageWidth = image.shape[1];
1020
-
1021
- const kernelKey = 'w' + imageWidth;
1022
- if (!this.kernelCaches.upsampleBilinear) {
1023
- this.kernelCaches.upsampleBilinear = {};
1024
- }
1025
-
1026
- if (!this.kernelCaches.upsampleBilinear[kernelKey]) {
1027
- const kernel = {
1028
- variableNames: ['p'],
1029
- outputShape: [targetImage.shape[0], targetImage.shape[1]],
1030
- userCode: `
1031
- void main() {
1032
- ivec2 coords = getOutputCoords();
1033
- int j = coords[0];
1034
- int i = coords[1];
1035
-
1036
- float sj = 0.5 * float(j) - 0.25;
1037
- float si = 0.5 * float(i) - 0.25;
1038
-
1039
- float sj0 = floor(sj);
1040
- float sj1 = ceil(sj);
1041
- float si0 = floor(si);
1042
- float si1 = ceil(si);
1043
-
1044
- int sj0I = int(sj0);
1045
- int sj1I = int(sj1);
1046
- int si0I = int(si0);
1047
- int si1I = int(si1);
1048
-
1049
- float sum = 0.0;
1050
- sum += getP(sj0I, si0I) * (si1 - si) * (sj1 - sj);
1051
- sum += getP(sj1I, si0I) * (si1 - si) * (sj - sj0);
1052
- sum += getP(sj0I, si1I) * (si - si0) * (sj1 - sj);
1053
- sum += getP(sj1I, si1I) * (si - si0) * (sj - sj0);
1054
- setOutput(sum);
1055
- }
1056
- `
1057
- };
1058
- this.kernelCaches.upsampleBilinear[kernelKey] = kernel;
1059
- }
1060
-
1061
- return tf.tidy(() => {
1062
- const program = this.kernelCaches.upsampleBilinear[kernelKey];
1063
- return tf.engine().runKernel("UpsampleBilinear", { x: image, width: image.shape[1], height: image.shape[0] });//this._compileAndRun(program, [image]);
1064
- });
1065
- } */
1066
-
1067
- _downsampleBilinear(image) {
1068
- /* const imageHeight = image.shape[0];
1069
- const imageWidth = image.shape[1];
1070
-
1071
- const kernelKey = 'w' + imageWidth;
1072
- if (!this.kernelCaches.downsampleBilinear) {
1073
- this.kernelCaches.downsampleBilinear = {};
1074
- }
1075
-
1076
- if (!this.kernelCaches.downsampleBilinear[kernelKey]) {
1077
- const kernel = {
1078
- variableNames: ['p'],
1079
- outputShape: [Math.floor(imageHeight / 2), Math.floor(imageWidth / 2)],
1080
- userCode: `
1081
- void main() {
1082
- ivec2 coords = getOutputCoords();
1083
- int y = coords[0] * 2;
1084
- int x = coords[1] * 2;
1085
-
1086
- float sum = getP(y, x) * 0.25;
1087
- sum += getP(y + 1, x) * 0.25;
1088
- sum += getP(y, x + 1) * 0.25;
1089
- sum += getP(y + 1, x + 1) * 0.25;
1090
- setOutput(sum);
1091
- }
1092
- `
1093
- };
1094
- this.kernelCaches.downsampleBilinear[kernelKey] = kernel;
1095
- } */
1096
-
1097
- return tf.tidy(() => {
1098
- //const program = this.kernelCaches.downsampleBilinear[kernelKey];
1099
- return tf.engine().runKernel("DownsampleBilinear", { image }); //this._compileAndRun(program, [image]);
1100
- });
1101
- }
1102
- /**
1103
- *
1104
- * @param {tf.MathBackendWebGL.GPGPUProgram} program
1105
- * @param {*} inputs
1106
- * @returns
1107
- */
1108
- _compileAndRun(program, inputs) {
1109
- const outInfo = tf.backend().compileAndRun(program, inputs);
1110
- return tf.engine().makeTensor(outInfo.dataId, outInfo.shape, outInfo.dtype);
1111
- }
1112
-
1113
- _runWebGLProgram(program, inputs, outputType) {
1114
- const outInfo = tf.backend().runWebGLProgram(program, inputs, outputType);
1115
- return tf.engine().makeTensor(outInfo.dataId, outInfo.shape, outInfo.dtype);
1116
- }
1117
- }
1118
-
1119
- export { Detector };