-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
183 lines (154 loc) · 6.76 KB
/
main.js
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
// Original source https://hackernoon.com/how-to-run-machine-learning-models-in-the-browser-using-onnx
const ort = require('onnxruntime-web');
const canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
document.getElementById("file-in").onchange = function (evt) {
let target = evt.target || window.event.src,
files = target.files;
if (FileReader && files && files.length) {
var fileReader = new FileReader();
fileReader.onload = () => onLoadImage(fileReader);
fileReader.readAsDataURL(files[0]);
}
}
function onLoadImage(fileReader) {
var img = document.getElementById("input-image");
img.onload = () => handleImage(img);
img.src = fileReader.result;
}
function handleImage(img) {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
var inputTensor = imageDataToTensor(ctx.getImageData(0, 0, img.width, img.height).data, [1, 3, img.height, img.width]);
run(inputTensor);
}
function imageDataToTensor(data, dims) {
// 1a. Extract the R, G, and B channels from the data
const [R, G, B] = [[], [], []]
for (let i = 0; i < data.length; i += 4) {
R.push(data[i]);
G.push(data[i + 1]);
B.push(data[i + 2]);
// 2. skip data[i + 3] thus filtering out the alpha channel
}
// 1b. concatenate RGB ~= transpose [224, 224, 3] -> [3, 224, 224]
const transposedData = R.concat(G).concat(B);
// 3. convert to float32
let i, l = transposedData.length; // length, we need this for the loop
const float32Data = new Float32Array(dims[0]*dims[1]*dims[2]*dims[3]); // create the Float32Array for output
for (i = 0; i < l; i++) {
float32Data[i] = transposedData[i] / 255.0; // convert to float
}
const inputTensor = new ort.Tensor("float32", float32Data, dims);
return inputTensor;
}
async function tileProc(inputTensor){
const sessionOption1 = { executionProviders: ['webgl'], logSeverityLevel: 0 };
const session1 = await ort.InferenceSession.create('./esrgan-small-pre.onnx', sessionOption1);
const sessionOption2 = { executionProviders: ['wasm'], logSeverityLevel: 0 };
const session2 = await ort.InferenceSession.create('./esrgan-small-end.onnx', sessionOption2);
const inputDims = inputTensor.dims;
const imageW = inputDims[3];
const imageH = inputDims[2];
const rOffset = 0;
const gOffset = imageW*imageH;
const bOffset = imageW*imageH*2;
const outputDims = [inputDims[0], inputDims[1], inputDims[2]*4, inputDims[3]*4];
const outputTensor = new ort.Tensor("float32", new Float32Array(outputDims[0]*outputDims[1]*outputDims[2]*outputDims[3]), outputDims);
const outImageW = outputDims[3];
const outImageH = outputDims[2];
const outROffset = 0;
const outGOffset = outImageW*outImageH;
const outBOffset = outImageW*outImageH*2;
const tileSize = 128;
const tilePadding = 12;
const tileSizePre = tileSize - tilePadding*2;
const tilesx = Math.ceil( inputDims[3] / tileSizePre );
const tilesy = Math.ceil( inputDims[2] / tileSizePre );
const data = inputTensor.data;
console.log(inputTensor);
const numTiles = tilesx*tilesy;
var currentTile = 0;
for (let i = 0; i < tilesx; i++) {
for (let j = 0; j < tilesy; j++) {
const ti = Date.now();
const tileW = Math.min(tileSizePre, imageW - i * tileSizePre);
const tileH = Math.min(tileSizePre, imageH - j * tileSizePre);
console.log("tileW: " + tileW + " tileH: " + tileH);
const tileROffset = 0;
const tileGOffset = tileSize*tileSize;
const tileBOffset = tileSize*tileSize*2;
const tileData = new Float32Array(tileSize*tileSize*3);
for (let xp = -tilePadding; xp < (tileSizePre+tilePadding); xp++) {
for (let yp = -tilePadding; yp < (tileSizePre+tilePadding); yp++) {
var xim = i * tileSizePre + xp;
if (xim < 0) xim = 0;
else if (xim >= imageW) xim = imageW - 1;
var yim = j * tileSizePre + yp;
if (yim < 0) yim = 0;
else if (yim >= imageH) yim = imageH - 1;
const idx = (xim + yim * imageW);
const xt = xp + tilePadding;
const yt = yp + tilePadding;
//const idx = (i * tileSize + x) + (j * tileSize + y) * imageW;
tileData[xt + yt * tileSize + tileROffset] = data[idx + rOffset];
tileData[xt + yt * tileSize + tileGOffset] = data[idx + gOffset];
tileData[xt + yt * tileSize + tileBOffset] = data[idx + bOffset];
}
}
const tile = new ort.Tensor("float32", tileData, [1, 3, tileSize, tileSize]);
const resultspre = await session1.run({input: tile});
console.log("pre dims:" + resultspre.output.dims);
const feed2 = { input: tile, input_pre: resultspre.output };
const results = await session2.run(feed2);
console.log("proc tile dims:" + results.output.dims);
const outTileW = tileW*4;
const outTileH = tileH*4;
const outTileSize = tileSize*4;
const outTileSizePre = tileSizePre*4;
const outTileROffset = 0;
const outTileGOffset = outTileSize*outTileSize;
const outTileBOffset = outTileSize*outTileSize*2;
// add tile to output
for (let x = 0; x < outTileW ; x++) {
for (let y = 0; y < outTileH; y++) {
const xim = i * outTileSizePre + x;
const yim = j * outTileSizePre + y;
const idx = (xim + yim * outImageW);
const xt = x + tilePadding*4;
const yt = y + tilePadding*4;
outputTensor.data[idx + outROffset] = results.output.data[xt + yt * outTileSize + outTileROffset];
outputTensor.data[idx + outGOffset] = results.output.data[xt + yt * outTileSize + outTileGOffset];
outputTensor.data[idx + outBOffset] = results.output.data[xt + yt * outTileSize + outTileBOffset];
}
}
currentTile++;
const dt = Date.now() - ti;
const remTime = (numTiles - currentTile) * dt;
console.log("tile " + currentTile + " of " + numTiles + " took " + dt + " ms, remaining time: " + remTime + " ms");
}
}
console.log("output dims:" + outputTensor.dims);
return outputTensor;
}
async function run(inputTensor) {
try {
const start = Date.now();
const result = await tileProc(inputTensor);
console.log("output dims:" + result.dims);
//display result
const canvas = document.createElement("canvas");
const context = canvas.getContext('2d');
canvas.height = result.dims[2];
canvas.width = result.dims[3];
const img = result.toImageData({ format: 'RGB', tensorLayout: 'NCHW'});
context.putImageData(img, 0, 0);
document.getElementById("canvas-image").src = canvas.toDataURL();
const end = Date.now();
console.log(`Execution time: ${end - start} ms`);
document.getElementById("executeTime").innerHTML = `Execution time: ${end - start} ms`;
} catch (e) {
console.log(e);
}
}