How Sudoku Challenger 2.0 reads a printed sudoku with a small neural network, and the bug that for a while made it see ones in the empty squares.
Sudoku Challenger started as an exercise to learn Dancing Links, Knuth's technique for exact cover problems. Then I wanted to learn how to scan objects with a known geometry, and a printed sudoku is a good case: a square of 9×9 squares, each one empty or holding a digit. The early versions could already scan with Vision, Apple's computer vision framework, but they needed the corners and the contrast adjusted by hand. In 2.0 I wanted a single step: you take the photo, check the result and play.
Vision finds the largest, squarest rectangle in the photo, a perspective correction straightens it, and the board is split into 81 squares. Each one has ten possible answers, a digit from 1 to 9 or nothing. Vision's OCR is designed for lines of text and relies on the baseline and the direction of writing, which an isolated square doesn't have. A small neural network that sorts tiny images into ten classes is a better fit.
Sudoku Challenger's network takes the square at 28×28 pixels in greyscale. It has two convolutional layers, with 32 and 64 filters, a dense layer of 64 neurons and the output. That's about 122,000 parameters, under half a megabyte, and it runs on the phone with Core ML. The photo never leaves the device. It's trained in Python with PyTorch and converted with coremltools. The app inverts each square before passing it in, because MNIST, the classic collection of handwritten digits, has white digits on black.
The first version of the model, in March, was trained only on MNIST digits 1 to 9 and scored 98.98 % on its test set. It worked as plan B: if Vision's OCR wasn't sure, the network was asked. It had two gaps. MNIST is handwriting, and the sudokus people scan are printed. And the model had nine classes, none for "empty", so faced with a blank square it picked whichever digit looked most like a blank page.
On 17 October, the day before submitting 2.0 for review, I automated the App Store screenshots. One of them runs a sudoku through the real scanner, and it came out with a 1 in almost every empty square.
I suspected the border: a sliver of grid line in the crop looks a lot like a 1. The crops were fine; Tesseract, another OCR, read them correctly. The bug was in the threshold. The network's last layer produces logits, unscaled scores, and turning them into probabilities takes softmax. The code compared the logit directly with 0.5. For a blank square, the logit for "1" was 0.79. That didn't mean "79 % sure"; it was just the highest score from a model that had no way of saying "nothing". A classifier always answers, and if "nothing" isn't among its possible answers, it turns every empty input into something else. 0.79 is greater than 0.5, so the square was filled with a 1.
Applying softmax before the threshold solved part of it. The other part was retraining the model with ten classes, with 0 for "empty", and with data closer to what actually gets scanned:
The data generator had a bug of its own: it added the noise before cropping the digit, the crop took in the whole image and the digit ended up tiny. Now the noise goes in last.
Vision's OCR mixed up printed 6s and 9s with a confidence of 1.0, because an isolated glyph doesn't show which way up it is. Since March there had been a patch that always swapped the two digits. I removed it. Swapping every 6 and 9 in a valid sudoku gives another valid sudoku with a unique solution, so no solver can detect the error, and the only way to avoid it is to read each digit correctly. Now the network, trained on printed digits the right way up, goes first, and Vision is only consulted if the network doesn't reach 50 % probability.
An automated test scans a synthetic image of the screenshot sudoku and requires the exact board, with its 30 clues and 51 empty squares. And the app always goes through a review screen before you play. If there are repeated numbers it won't let you continue, if there's no solution it doesn't load the puzzle, and if there are several it warns you. The scanner saves you typing in some 30 clues, and the person holding the paper has the final say.
There's a third thing still to learn, if I ever get to it: augmented reality. The idea is to point the camera at an empty sudoku and see the solution overlaid on the paper, each number in its square. The app can already find the board and read it; what's left is tracking it in video and drawing on top.