-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.htm
170 lines (164 loc) · 7.83 KB
/
index.htm
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
<!DOCTYPE>
<html>
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Cousine:ital,wght@0,400;0,700;1,400;1,700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="SWOps.js"></script>
<script src="model_reordered.js"></script>
<script>
function TensorFromImage(img)
{
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
const padded_width = img.width + (img.width % 2);
const padded_height = img.height + (img.height % 2);
canvas.width = padded_width;
canvas.height = padded_height;
context.drawImage(img, 0, 0 );
var imageData = context.getImageData(0, 0, padded_width, padded_height);
var tensorData = new Float32Array(padded_width * padded_height * 3);
var tensorIndex = 0;
var tensorPlane1Offset = padded_width * padded_height;
var tensorPlane2Offset = 2 * tensorPlane1Offset;
for (y = 0; y < padded_height; y++)
{
var row = y * padded_width * 4;
for (x = 0; x < padded_width; x++)
{
pixel = row + x * 4;
tensorData[tensorIndex] = imageData.data[pixel] / 255.0;
tensorData[tensorIndex + tensorPlane1Offset] = imageData.data[pixel+1] / 255.0;
tensorData[tensorIndex + tensorPlane2Offset] = imageData.data[pixel+2] / 255.0;
tensorIndex++;
}
}
tensorData.width = padded_width;
tensorData.height = padded_height;
return tensorData;
}
function ImageFromTensor(tensorData, width, height)
{
var imageData = new Uint8ClampedArray(width * height * 4);
var tensorIndex = 0;
var tensorPlane1Offset = width * height;
var tensorPlane2Offset = 2 * tensorPlane1Offset;
for (y = 0; y < height; y++)
{
var row = y * width * 4;
for (x = 0; x < width; x++)
{
pixel = row + x * 4;
imageData[pixel] = tensorData[tensorIndex] * 255.0;
imageData[pixel+1] = tensorData[tensorIndex + tensorPlane1Offset] * 255.0;
imageData[pixel+2] = tensorData[tensorIndex + tensorPlane2Offset] * 255.0;
imageData[pixel+3] = 255;
tensorIndex++;
}
}
return new ImageData(imageData, width, height);
}
function mulReduce(arr) {
return arr.reduce((product, value) => product * value, 1);
}
async function main() {
const weights_file = 'weights.bin';
let cache = await caches.open("weights")
let weights_response = await cache.match(weights_file);
if (!weights_response)
{
await cache.add(weights_file);
weights_response = await cache.match(weights_file);
}
const weights_buffer = await weights_response.arrayBuffer();
var startTime = performance.now();
clearCanvas();
image = document.querySelector("img");
const tensor = TensorFromImage(image);
// Build WebNN Graph and execute it
let result = null;
try {
const context = await navigator.ml.createContext(
{'deviceType' : 'gpu'});
const builder = new MLGraphBuilder(context);
InstallCpuOps(builder);
const operandType = {type: 'float32',
dataType: 'float32', shape: [1, 3, tensor.height, tensor.width]};
const input_operand = builder.input('input', operandType);
const output_operand = loadModelGraph(input_operand, weights_buffer, builder);
const graph = await builder.build({'output': output_operand});
const outputBuffer = new Float32Array(mulReduce(output_operand.shape()));
const inputs = {'input': tensor};
const outputs = {'output': outputBuffer};
result = await context.compute(graph, inputs, outputs);
} catch (webnn_exception) {
document.querySelector("b").innerText = "Error: " + webnn_exception;
return;
}
// Convert tensor back to image
const imageData = ImageFromTensor(result.outputs.output, tensor.width * 2, tensor.height * 2);
var canvasHTML = document.querySelector("canvas");
let canvas_context = canvasHTML.getContext('2d');
canvasHTML.width = image.width*2;
canvasHTML.height = image.height*2;
canvas_context.putImageData(imageData, 0, 0);
var endTime = performance.now();
document.querySelector("b").innerText = Math.round(endTime - startTime) + "ms " + tensor.width + " x " + tensor.height + " | " + Math.round((tensor.width*tensor.height)/Math.round(endTime - startTime)) + "K pixels/second";
}
function clearCanvas() {
var canvasHTML = document.querySelector("canvas");
let context = canvasHTML.getContext('2d');
context.clearRect(0, 0, canvasHTML.width, canvasHTML.height);
}
function dropHandler(ev)
{
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
if (ev.dataTransfer.items) {
// Use DataTransferItemList interface to access the file(s)
[...ev.dataTransfer.items].forEach((item, i) => {
// If dropped items aren't files, reject them
if (item.kind === "file") {
const file = item.getAsFile();
console.log(`A file[${i}].name = ${file.name}`);
var reader=new FileReader();
reader.onload= function (event) {
var image = document.querySelector("img");
image.src = reader.result;
}
reader.readAsDataURL(file);
}
});
} else {
// Use DataTransfer interface to access the file(s)
[...ev.dataTransfer.files].forEach((file, i) => {
console.log(`B file[${i}].name = ${file.name}`);
});
}
}
function dragOverHandler(ev) {
ev.preventDefault();
}
</script>
</head>
<body>
<div>
<h1>Rapid ESRGAN</h1>
<div>
<div>
An ESRGAN implementation using WebNN, experience Super Resolution in your browser.<br>
<span class="light">
To enable experimental WebNN visit
<u>chrome://flags/#web-machine-learning-neural-network</u>
<br><span>Metrics: Inference Time <b>∞</b></span><br>
</span>
</div>
<button onclick="main()">SuperRes</button> <button onclick="clearCanvas()">Clear</button><br>
<center>👇 Drop your images here 👇</center>
<img src="0567x2.png" ondrop="dropHandler(event)" ondragover="dragOverHandler(event);" onload="main()"></img><br><canvas style="zoom:0.5"></canvas>
</div>
</div>
</body>
</html>