Trains a multi-layer perceptron (MLP) neural network to perform optical character recognition (OCR).
The training set is automatically generated using a heavily modified version of the captcha-generator node-captcha.
The network takes a one-dimensional binary array (default 20x20 = 400-bit) as input and outputs an 8-bit binary array, which can then be converted into a character code. Initial performance measurements show promising success rates.
After training, the network is saved as a standalone module to ./network.js
, which can then be used in your project with
var network = require('./network.js');
var output = network.activate(input);
- MLP specification
- Neurons
400
input40
hidden4
output
- Learning rate
0.1
- Training set
- Size
59999
digits
- Size
- Testing set
- Size
9999
digits
- Size
- Neurons
- Measured success rate
82.08820882088209%
- Fonts
- sans-serif
- serif
- MLP specification
- Measured success rate
96.32307692307693%
- Fonts
- sans-serif
- serif
- MLP specification
- Measured success rate
99.22%
Tweak the network for your needs by editing the config.json
file located in the main folder. Pasted below is the default config file.
{
"mnist": false,
"text": "abcdefghijklmnopqrstuvwxyz",
"fonts": [
"sans-serif",
"serif"
],
"training_set": 2000,
"testing_set": 500,
"image_size": 20,
"threshold": 400,
"network": {
"hidden": 40,
"learning_rate": 0.1
}
}
mnist
- If set to true, the MNIST handwritten digit dataset will be used for training and testing the network.
text
- A string containing the glyphs with which to train/test the network.
fonts
- An array of fonts to be used when generating images.
training_set
- Number of images to be generated and used as the network training set.
testing_set
- Same as above, but these images are used for testing the network.
image_size
- The size of the square chunk (in pixels) containing a glyph. The resulting network input size is
image_size
^2.
- The size of the square chunk (in pixels) containing a glyph. The resulting network input size is
threshold
- When analyzing the pixels of a glyph, the algorithm reduces each pixel
(r, g, b)
to(r + g + b)
and everything belowthreshold
is marked as 1 in the resulting binary array used as network input.
- When analyzing the pixels of a glyph, the algorithm reduces each pixel
network
hidden
- The size (number of neurons) of the hidden layer of the network.
learning_rate
- The learning rate of the network.
Clone this repository. The script is using canvas, so you'll need to install the Cairo rendering engine. On OSX, this can be done with the following one-liner (copied from canvas README):
$ wget https://raw.githubusercontent.com/LearnBoost/node-canvas/master/install -O - | sh
Then install npm dependencies and test it:
$ npm install
$ node main.js
Here is an example run of the script:
$ node main.js
reading config file ...
... done
generating images ...
... done
neural network specs:
layers:
input: 400 neurons.
hidden: 40 neurons.
output: 8 neurons.
learning rate: 0.1
training set: 20000 characters.
testing set: 5000 characters.
learning ...
progress: 10%
progress: 20%
progress: 30%
progress: 40%
progress: 50%
progress: 60%
progress: 70%
progress: 80%
progress: 90%
... done
network saved to ./network.js
testing on 5000 samples ...
progress: 10%
progress: 20%
progress: 30%
progress: 40%
progress: 50%
progress: 60%
progress: 70%
progress: 80%
progress: 90%
... done
success rate: 99.58 %
Feel free to fork and submit pull requests.