Gradient Decent / filter
Can a photo filter read handwriting?
An SVG filter, the thing meant for blurs and drop shadows, as a calculator. A handwritten-digit reader whose
whole forward pass is one <filter>: the layers are feConvolveMatrix, the max
pooling is feMorphology, and the activation function is the clamp every colour gets for free. No
WebGPU, no WebGL, no WASM.
Yes. On the 10,000 MNIST test digits the filter gets 98.22% right and a plain JavaScript run of the same weights gets 98.24%. They give the same answer on 9,996 of them. The small gap is rounding: every picture inside a filter is 8 bits per channel, so every number in the network is rounded to one of 256 levels on the way through, and the network was trained expecting that. It's also slow: about 7 ms a digit, where a plain JavaScript loop is about 40 times faster in headless Chrome on a CPU. The race below measures your machine on 1,000 of those digits.
Run it
Draw one digit, 0 to 9, with the mouse or a finger. The filter runs on every stroke.
What the network sees: your digit at 14×14, copied 8 times
After the 8 kernels: 8 feature maps
After taking the largest value in every 3×3 window
The 48 hidden units, one pixel each, 6 per feature map
This is the filter's whole output at actual size, 64 by 98 pixels, from one drawImage. The
four pictures above are parts of it, enlarged, and the ten bars are the red values of the ten pixels in
its last row, divided by 255. Brighter means a bigger number; black is zero.
Race it in your browser
Runs 1,000 MNIST test digits the network never trained on through both engines: the filter, and a plain
JavaScript loop over the same weights from model.json. Same as window.selfTest().
Scores run from 0 to 1, in steps of 1/255, which is 0.0039. The last number is the check that the filter is doing exactly this network and nothing else: when the JavaScript version rounds every layer to 8 bits the way the filter has to, the two should never be more than one step apart.
How one digit gets read
Seven steps. JavaScript does the first and the last, and none of the arithmetic. The five in the middle are filter primitives holding weights.
-
JS
Shrink the drawing.
The pad is 280 pixels wide and the network takes 14×14. Your strokes are kept as lines and drawn again small: scaled so the digit's longer side is 10 pixels, centred, with a line about 1.2 pixels thick. That's how MNIST digits sit in their frame. The small picture gets a 1-pixel black border, making a 16×16 tile, and is drawn onto a canvas through the filter.
-
Filter
Copy.
feTilerepeats the tile 8 times in a 4×2 grid, one copy per kernel. The black borders keep the copies 2 pixels apart, which is exactly the zero padding a 5×5 kernel needs. -
Filter
Convolve.
Eight
feConvolveMatrixprimitives, each limited to its own copy, each with its own 5×5 kernel andbias. A filter's result is a colour, so anything below 0 becomes 0 and anything above 1 becomes 1. That clamp is the activation function, and it costs nothing. -
Filter
Max.
feMorphologywithdilateis meant for thickening text. What it does is replace each pixel with the largest value in the 3×3 around it, which is max pooling. One primitive does all 8 maps at once. -
Filter
Hidden layer.
48 units, 6 per map. A unit that looks at a whole 14×14 map is a 14×14 kernel that only needs working out at one pixel, so each is a
feConvolveMatrixwhose output area is 1×1. AnfeOffsetthen moves that pixel into a row with the other 47. -
Filter
Scores.
Ten more
feConvolveMatrix, each a 48×1 kernel laid over the row of hidden units and worked out at one pixel. The result is ten grey pixels, one per digit. -
JS
Read back.
One
getImageDataon those ten pixels. The brightest one is the answer. There's no softmax: the network was trained to make the right pixel white and the rest black.
What the filter looks like
<feTile in="digit" x="0" y="0" width="64" height="32" result="copies"/> <!-- one of the 8 first-layer kernels, whole --> <feConvolveMatrix in="copies" x="1" y="1" width="14" height="14" order="5" kernelMatrix="0.1227618 0.194187 0.166251 0.1888995 0.1797812 -0.0499268 -0.0450399 0.0474566 -0.1540529 -0.1313138 0.0323561 -0.1948786 -0.3117438 0.04431 -0.0588307 0.1197731 -0.6937593 0.1275399 0.260922 -0.0438024 -0.285367 -0.4159704 0.0473294 0.2386246 -0.1628726" bias="-0.064782" divisor="1" preserveAlpha="true" edgeMode="none" result="c0"/> <feMorphology in="maps" operator="dilate" radius="1" result="pooled"/> <!-- one of the 48 hidden units: a 14x14 kernel worked out at a single pixel, then moved into row 96 --> <feConvolveMatrix in="pooled" x="1" y="1" width="1" height="1" order="14" targetX="0" targetY="0" kernelMatrix="0.018094 0.0486829 -0.1287327 -0.1837926 -0.0614371 -0.0308482 … 190 more" bias="0.0885406" divisor="1" preserveAlpha="true" edgeMode="none" result="u0"/> <feOffset in="u0" x="0" y="96" width="1" height="1" dx="-1" dy="95" result="h0"/> <!-- the score for "0": a 48x1 kernel over row 96 --> <feConvolveMatrix in="hidden" x="0" y="96" width="1" height="1" order="48 1" targetX="0" targetY="0" kernelMatrix="-0.0676359 -0.0125812 0.1558215 -0.313763 0.1266749 -0.1226906 … 42 more" bias="0.1085112" divisor="1" preserveAlpha="true" edgeMode="none" result="s0"/>
Real lines from this page's filter, with the long kernels cut short. The whole thing is
KB; view the page source to read it. The kernels are written back to front because
feConvolveMatrix turns them around before using them.
JS the bookkeeping
- Recording your strokes and redrawing them at 14×14
- One
drawImagewithctx.filter = 'url(#net)' - Reading ten pixels back and picking the brightest
- Copying parts of the output into the pictures on this page
Filter all of the maths
- Every multiply and add in all three layers (
feConvolveMatrix) - The biases (its
biasattribute) - The activation function (colours clamp to 0..1)
- Max pooling (
feMorphology) -
Moving numbers to where the next layer reads them (
feTile,feOffset,feMerge)
What it says
- A filter is a small convolutional network already. Convolution, bias, a clamp and a max are all there as primitives. Nothing here is a trick on the scale of the CSS page; the primitives are used for what they do, just not for what they're for.
- Everything is 8 bits, and that's fine if the network knows. Every primitive's result is rounded to one of 256 levels and clamped to 0..1. Training with the same rounding and clamping in place means the filter is the network, not an approximation of it: 98.22% against 98.24% without the rounding, on 10,000 test digits, and no score moved by more than 0.02.
- The filter matches its own arithmetic exactly. A JavaScript version that rounds each layer to 8 bits gives the same answer as the filter on all 10,000 test digits, and no score differs by more than one step of 1/255. So the filter isn't roughly a neural network. It is this one.
-
It isn't fast. About 6 ms a digit in headless Chrome on a CPU, against about 0.16 ms for a
plain JavaScript loop over the same weights. Merging the 48
feOffsetprimitives into 8 made no difference to the time. - The model is small and so is its idea of a digit. weights, trained on MNIST shrunk to 14×14. Apart from MNIST it has only been tried on strokes drawn by a script, not on many people's handwriting, so expect it to be wrong sometimes.
Gotchas found on the way
- Pictures between primitives are 8 bits per channel, even when the canvas being drawn to is float16. Multiply by 0.01 in one primitive and by 100 in the next and 255 levels come back as 4.
-
Except when they aren't. Two
feColorMatrixin a row are merged into one step and keep full precision between them, so the first precision test said everything was fine. Put any other primitive between them and it's 8 bits again. -
feConvolveMatrixstops at 256 kernel entries. A 17×16 kernel doesn't raise an error. The primitive is skipped and the picture passes through untouched. So no single kernel can see all 8 feature maps, which is why the hidden layer has 6 units per map and not 48 units that each see everything. - Kernels of 28 entries or more are stored as an 8-bit picture, stretched between the kernel's smallest and largest entry. Up to 27 they're exact. The big kernels here were trained on that 256-level grid, which makes the filter exact again.
- The kernel is applied upside down and back to front, as the specification says (it's a true convolution). Every kernel in the filter is written in reverse.
-
A primitive only writes where its input was. An
feOffsetthat moves one pixel somewhere else produced nothing at all until it was given its ownx,y,widthandheight, because its output area defaults to its input's. -
A kernel can only write inside itself.
targetXhas to be smaller than the kernel's width, so a hidden unit's pixel lands on the map it's looking at and needs anfeOffsetto get anywhere useful. -
biasis added everywhere, including the empty space between tiles, so every convolution is fenced in withx,y,widthandheight. -
Filters work in linear light by default. Without
color-interpolation-filters="sRGB"every number is bent through a gamma curve on the way in and back out: half of 200 came out as 147. -
Only tried in headless Chrome 153 with no GPU, where filters run in software. The canvas
asks for software drawing (
willReadFrequently) to keep it that way, but a different browser, or a GPU path with different rounding, could give different numbers. The race above will show it if so.