ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / mnist / web / index.html
| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta charset="utf-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> |
| 6 | <title>MNIST with GGML</title> |
| 7 | <script src="mnist.js"></script> |
| 8 | </head> |
| 9 | <body> |
| 10 | <h2>MNIST digit recognizer with <a href="https://github.com/ggerganov/ggml">GGML</a></h2> |
| 11 | <p id="msg">Loading model and data set, please wait ...</p> |
| 12 | <canvas id="ggCanvas" width="364" height="364" style="border:2px solid #d3d3d3;"> |
| 13 | Your browser does not support the HTML canvas tag. |
| 14 | </canvas> |
| 15 | <div> |
| 16 | <button id="clear" onclick="onClear()">Clear</button> |
| 17 | <button id="random" onclick="onRandom()" disabled>Random</button> |
| 18 | <button id="download" onclick="onDownload()">Download</button> |
| 19 | </div> |
| 20 | <div> |
| 21 | <p id="prediction"></p> |
| 22 | </div> |
| 23 | <script> |
| 24 | "use strict"; |
| 25 | const DIGIT_SIZE = 28; // digits are 28x28 pixels |
| 26 | var canvas = document.getElementById("ggCanvas"); |
| 27 | var ctx = canvas.getContext("2d", { alpha: false, willReadFrequently: true }); |
| 28 | ctx.fillStyle = "white"; |
| 29 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 30 | var dragging = false; |
| 31 | var lastX, lastY; |
| 32 | |
| 33 | function onClear(event) { |
| 34 | ctx.fillStyle = "white"; |
| 35 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 36 | document.getElementById("prediction").innerHTML = ""; |
| 37 | } |
| 38 | |
| 39 | function predict(digit) { |
| 40 | let buf = Module._malloc(digit.length); |
| 41 | if (buf == 0) { |
| 42 | console.log("failed to allocate memory"); |
| 43 | return; |
| 44 | } |
| 45 | Module.HEAPU8.set(digit, buf); |
| 46 | let prediction = Module.ccall('wasm_eval', null, ['number'], [buf]); |
| 47 | Module._free(buf); |
| 48 | if (prediction >= 0) { |
| 49 | document.getElementById("prediction").innerHTML = "Predicted digit is <b>" + prediction + "</b>"; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function onRandom(event) { |
| 54 | onClear(); |
| 55 | const bufLength = DIGIT_SIZE*DIGIT_SIZE; |
| 56 | var buf = Module._malloc(bufLength); |
| 57 | if (buf == 0) { |
| 58 | console.log("failed to allocate memory"); |
| 59 | return; |
| 60 | } |
| 61 | let ret = Module.ccall('wasm_random_digit', null, ['number'], [buf]); |
| 62 | let digit = new Uint8Array(Module.HEAPU8.buffer, buf, bufLength); |
| 63 | for (let i = 0; i < digit.length; i++) { |
| 64 | let x = i % DIGIT_SIZE; |
| 65 | let y = Math.floor(i / DIGIT_SIZE); |
| 66 | setPixel(x, y, digit[i]); |
| 67 | } |
| 68 | Module._free(buf); |
| 69 | predict(digit); |
| 70 | } |
| 71 | |
| 72 | function onDownload(event) { |
| 73 | let digit = scaleCanvas(); |
| 74 | let digitBlob = new Blob([new Uint8Array(digit)], {type: "application/octet-stream"}); |
| 75 | let url = URL.createObjectURL(digitBlob); |
| 76 | let link = document.createElement('a'); |
| 77 | link.href = url; |
| 78 | link.download = "image.raw"; |
| 79 | document.body.appendChild(link); |
| 80 | link.click(); |
| 81 | document.body.removeChild(link); |
| 82 | } |
| 83 | |
| 84 | // Get the position of the mouse relative to the canvas |
| 85 | function getMousePos(event) { |
| 86 | if (event.touches !== undefined && event.touches.length > 0) { |
| 87 | event = event.touches[0]; |
| 88 | } |
| 89 | var rect = canvas.getBoundingClientRect(); |
| 90 | return [Math.floor(event.clientX) - rect.left, Math.floor(event.clientY) - rect.top]; |
| 91 | } |
| 92 | |
| 93 | function setPixel(x, y, val) { |
| 94 | let canvasX = x * 13; |
| 95 | let canvasY = y * 13; |
| 96 | let color = 255 - val; |
| 97 | ctx.fillStyle = "#" + color.toString(16) + color.toString(16) + color.toString(16); |
| 98 | ctx.fillRect(canvasX, canvasY, 13, 13); |
| 99 | } |
| 100 | |
| 101 | function onMouseDown(e) { |
| 102 | dragging = true; |
| 103 | [lastX, lastY] = getMousePos(e); |
| 104 | } |
| 105 | |
| 106 | // scale the canvas to 28x28 pixels and return the pixel values as an array |
| 107 | function scaleCanvas() { |
| 108 | let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 109 | let tempCanvas = document.createElement('canvas'); |
| 110 | tempCanvas.width = DIGIT_SIZE; |
| 111 | tempCanvas.height = DIGIT_SIZE; |
| 112 | let tempCtx = tempCanvas.getContext("2d"); |
| 113 | tempCtx.drawImage(canvas, 0, 0, DIGIT_SIZE, DIGIT_SIZE); |
| 114 | let tempImgData = tempCtx.getImageData(0, 0, DIGIT_SIZE, DIGIT_SIZE); |
| 115 | let tempData = tempImgData.data; |
| 116 | let digit = new Array(DIGIT_SIZE*DIGIT_SIZE).fill(0); |
| 117 | for (let i = 0; i < tempData.length; i += 4) { |
| 118 | let val = 255 - tempData[i]; |
| 119 | digit[i / 4] = val; |
| 120 | } |
| 121 | return digit; |
| 122 | } |
| 123 | |
| 124 | function onMouseUp(e) { |
| 125 | dragging = false; |
| 126 | let digit = scaleCanvas(); |
| 127 | predict(digit); |
| 128 | } |
| 129 | |
| 130 | function onMouseMove(e) { |
| 131 | if (dragging) { |
| 132 | let [mouseX, mouseY] = getMousePos(e); |
| 133 | ctx.beginPath(); |
| 134 | ctx.moveTo(lastX, lastY); |
| 135 | ctx.lineTo(mouseX, mouseY); |
| 136 | ctx.lineWidth = 20; |
| 137 | ctx.lineJoin = ctx.lineCap = 'round'; |
| 138 | ctx.strokeStyle = "#000000"; |
| 139 | ctx.stroke(); |
| 140 | ctx.closePath(); |
| 141 | lastX = mouseX; |
| 142 | lastY = mouseY; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Prevent scrolling when touching the canvas |
| 147 | document.body.addEventListener("touchstart", function (e) { |
| 148 | if (e.target == canvas) { |
| 149 | e.preventDefault(); |
| 150 | } |
| 151 | }, {passive: false}); |
| 152 | document.body.addEventListener("touchend", function (e) { |
| 153 | if (e.target == canvas) { |
| 154 | e.preventDefault(); |
| 155 | } |
| 156 | }, {passive: false}); |
| 157 | document.body.addEventListener("touchmove", function (e) { |
| 158 | if (e.target == canvas) { |
| 159 | e.preventDefault(); |
| 160 | } |
| 161 | }, {passive: false}); |
| 162 | |
| 163 | function onRuntimeInitialized() { |
| 164 | // Use the same handlers for mouse and touch events |
| 165 | canvas.onmousedown = onMouseDown; |
| 166 | canvas.onmouseup = onMouseUp; |
| 167 | canvas.onmousemove = onMouseMove; |
| 168 | canvas.ontouchstart = onMouseDown; |
| 169 | canvas.ontouchend = onMouseUp; |
| 170 | canvas.ontouchmove = onMouseMove; |
| 171 | document.getElementById("msg").innerHTML = "Draw a single digit on the canvas below:" |
| 172 | document.getElementById("random").disabled = false; |
| 173 | } |
| 174 | |
| 175 | Module['onRuntimeInitialized'] = onRuntimeInitialized; |
| 176 | </script> |
| 177 | </body> |
| 178 | </html> |