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.

An example. Start drawing to replace it.

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().

Filter, per digit–
JavaScript, per digit–
Filter is slower by–
Same answer–
Filter got right–
JavaScript got right–
Largest gap in a score–
Same, if JavaScript rounds to 8 bits too–

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.

  1. 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.

  2. Filter
    Copy.

    feTile repeats 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.

  3. Filter
    Convolve.

    Eight feConvolveMatrix primitives, each limited to its own copy, each with its own 5×5 kernel and bias. 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.

  4. Filter
    Max.

    feMorphology with dilate is 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.

  5. 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 feConvolveMatrix whose output area is 1×1. An feOffset then moves that pixel into a row with the other 47.

  6. 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.

  7. JS
    Read back.

    One getImageData on 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 drawImage with ctx.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 bias attribute)
  • 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

Gotchas found on the way