πŸ‘οΈπŸ”’ Digit recognition using neural nets and JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
raw_data
src
test
.gitignore
LICENSE
README.md
package.json
tsconfig.json
tslint.json
typings.json
webpack.config.js

README.md

JS Digit Recognition

My attempt to write a digit recognition engine in JavaScript (TypeScript to be precise). This tool utilises some very basic concepts behind neural networks.

Data set

As my data set I'm using USPS Handwritten Digits (also found as jpeg in the raw_data folder). I do appreciate the fact that it is a very small data set but in my opinion it will suffice for this simple exercise.

Demo output

Click here for a short video of the classifier in action.

Here's an excerpt of the console output after running dist/test/digit-classification-test.js:

[TEST] Output:  Expected > 7  Actual > 7
[TEST]
[TEST] CORRECT GUESS
[TEST]
[TEST] Neuron outputs:
[TEST]
[TEST]    0  β—†         0
[TEST]    1  β—†         1
[TEST]    2  β—†         2
[TEST]    3 β—†          3
[TEST]    4 β—†          4
[TEST]    5  β—†         5
[TEST]    6 β—†          6
[TEST]  > 7          β—† 7 <
[TEST]    8 β—†          8
[TEST]    9  β—†         9
[TEST]
[TEST] Image of the digit:
[TEST]
[TEST] β–ˆ
[TEST]       β–ˆβ–ˆ  β–ˆ β–ˆβ–ˆβ–ˆβ–ˆ
[TEST]   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
[TEST]  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
[TEST] β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ      β–ˆβ–ˆβ–ˆ
[TEST] β–ˆβ–ˆβ–ˆ        β–ˆβ–ˆβ–ˆ
[TEST]           β–ˆβ–ˆβ–ˆ
[TEST]          β–ˆβ–ˆβ–ˆβ–ˆ
[TEST]          β–ˆβ–ˆβ–ˆ
[TEST]         β–ˆβ–ˆβ–ˆ
[TEST]        β–ˆβ–ˆβ–ˆβ–ˆ
[TEST]        β–ˆβ–ˆβ–ˆ
[TEST]       β–ˆβ–ˆβ–ˆ
[TEST]      β–ˆβ–ˆβ–ˆ
[TEST]     β–ˆβ–ˆβ–ˆ
[TEST]     β–ˆβ–ˆ
[TEST]    β–ˆβ–ˆ

Accuracy after 243 iterations: 0.789 [---------------->

Running the classifier

The TypeScript source code for the neural network abstraction itself can be found in the src folder. Technically, it can be used as a standalone solution so feel free to reuse any of the code found there.

The TypeScript source code for the tests can be found in the test folder. All of the files there are Mocha tests except for the digit classification test itself, found in digit-classification-test.ts.

The easiest way to run the tests is to compile all of the TypeScript into JavaScript, I have provided the tsconfig.json I used during the development. You can find the precompiled JavaScript source code for the neural network in the dist/src folder and the source code for tests in dist/test folder.

One can run the classifier by executing node dist/test/digit-classification-test.js in the terminal after having installed all of the dependencies (run npm install and typings install for that).

Using the source code

I tried to document all of the TypeScript source code for the neural network implementation itself, although the tests are quite poorly documented. I suggest you use the TypeScript tests to understand how a neural network is initialised and then consult the code in the src folder to understand how the actual implementation works.

Suggestions and feedback

If you feel like something has to be changed or find an error, feel free to create a new issue thread.