Skip to content

Training and web visualization of digit recognition feedforward neural network using brain.js and three.js

License

Notifications You must be signed in to change notification settings

g-otn/number-guesser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repo holds the code to train a simple digit recognizing feed foward neural network with the MNIST database digits and also a website which allows the user to draw digits and visualize the neural network prediction of the drawings.

Usage

  • Draw digit on the "Digit" canvas
  • Click on node to highlight it's weights
  • Check table for camera controls in the 3D visualizer
  • Space to reset weights and camera

Installation

First, clone the repository:

git clone https://github.com/g-otn/number-guesser.git
cd number-guesser

Client

If you do not wish to train a new neural network, you can simply serve the client/ folder:

  1. npm i -g serve
  2. serve client or cd train && npm run-script serve

Training

If you wish to run the training script to generate a new model, follow these steps. Steps 1-3 are optional, but if you wish to skip them, please read Note 2 below:

  1. Navigate to the train/ folder: cd train
  2. BEFORE creating node_modules/ (i.e npm i) in the train/ folder, download these two files from the MNIST database:
  3. Extract the two files into the data/ folder:
data
├───train-images-idx3-ubyte
└───train-labels-idx1-ubyte

Note: Notice the - character in the file names. You may need to rename them (replacing the . for -) if you get an file not found error in the next step.

  1. Install the packages: npm i. A post-install npm script should do all the work with the files.

  2. Run npm start or node train.js

  3. After training ends the model file will already be in the right path to be loaded by the browser.

Note 2: If something goes wrong during steps 1-3 you can skip it, however you will need to edit the train.js file and lower the mnist set to a sum <= 10000. (i.e mnist.set(8000, 2000). Otherwise the training might log NaNs and save an invalid model.

Using your own model

If you already have an exported brain.js model, you can replace the client/model.json file so the client can use your model instead. Because of hard-coded code, and since it's using a poorly modified version of brain-browser.js, the client requires the model to have this structure for the visualization to work:

  • Type: feed forward (brain.NeuralNetwork)
  • Layers: 784 nodes input layer (28*28 image pixels), at least one hidden layer, 10 nodes output layer (digits 0-9)
  • Activation function: sigmoid

Built with

Acknowledgements

This repo was highly motivated by 3Blue1Brown's videos on Neural Networks.

Most of the training code was based on Ralph's Blog article on "Machine Learning with brain.js and Tensorflow.js".

Thanks to MSc Adriana Natividad Lopez Valverde for assigning me the project related to this repo.

License

MIT

This repo uses a modified version of a BrainJS/brain.js file, licensed under MIT.