Skip to content

inevitable-team/tfjs-mnist

Repository files navigation

TensorFlowJS Minst

An implementation of the MNIST neural network for use in backend ML systems.

Example Usages

Basic

To use this package and run it on the test data provided, the only code that needs to be wrote is:

    let config = {};
    let mnistTester: new mnist(config);
    await mnistTester.setup();
    await mnistTester.train();
    await mnistTester.evaluate();
    console.table(mnistTester.prettyConfusionMatrix());
    console.log("Accuracy: ", mnistTester.accuracy() + "%");

Which will then display a result of:

┌────────────┬────────────────┬────────────────┬────────────────┬────────────────┬────────────────┬────────────────┬────────────────┬────────────────┬────────────────┬────────────────┐
│  (index)   │ "0" Prediction │ "1" Prediction │ "2" Prediction │ "3" Prediction │ "4" Prediction │ "5" Prediction │ "6" Prediction │ "7" Prediction │ "8" Prediction │ "9" Prediction │
├────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┼────────────────┤
│ "0" Actual │       24       │       0        │       0        │       0        │       0        │       0        │       0        │       0        │       0        │       0        │
│ "1" Actual │       0        │       26       │       0        │       0        │       0        │       0        │       0        │       0        │       0        │       0        │
│ "2" Actual │       0        │       0        │       23       │       0        │       0        │       0        │       0        │       1        │       0        │       0        │
│ "3" Actual │       0        │       0        │       0        │       28       │       0        │       0        │       0        │       1        │       0        │       0        │
│ "4" Actual │       0        │       0        │       0        │       0        │       24       │       0        │       0        │       0        │       0        │       1        │
│ "5" Actual │       0        │       0        │       0        │       0        │       0        │       26       │       0        │       0        │       0        │       0        │
│ "6" Actual │       0        │       0        │       0        │       0        │       0        │       1        │       17       │       0        │       0        │       0        │
│ "7" Actual │       0        │       0        │       1        │       0        │       0        │       0        │       0        │       25       │       2        │       1        │
│ "8" Actual │       1        │       0        │       1        │       0        │       0        │       2        │       1        │       0        │       18       │       0        │
│ "9" Actual │       0        │       0        │       0        │       0        │       0        │       1        │       0        │       0        │       0        │       25       │
└────────────┴────────────────┴────────────────┴────────────────┴────────────────┴────────────────┴────────────────┴────────────────┴────────────────┴────────────────┴────────────────┘
Accuracy:  94.4%

Use Model to Predict a new Image

Using the trained model from above, the following code can be added to see the prediction of a given image.

    let onePrediction = await mnistTester.predictOne(`${__dirname}/example_dataset/0/img_5.jpg`);
    console.log("Predict One Prediction: ", onePrediction);

Which will then log the following result:

    Predict One Prediction:  0

Source Folder Structure

In the imagesUrl parameter you specify for the source folder, there are two acceptable structure, one where you can manually split the training and testing data, then another where it is randomly determined by the split parameter.

Manual Split

example_dataset
+-- training
|   +-- 0
|   |   +-- img_0.jpg
|   |   +-- img_2.jpg
|   |   +-- img_43.jpg
|   +-- 1
|   |   +-- img_23.jpg
|   |   +-- img_76.jpg
|   |   +-- img_245.jpg
+-- testing
|   +-- 0
|   |   +-- img_69.jpg
|   |   +-- img_66.jpg
|   |   +-- img_56.jpg
|   +-- 1
|   |   +-- img_68.jpg
|   |   +-- img_58.jpg

Auto Split

example_dataset
+-- 0
|   +-- img_67.jpg
|   +-- img_57.jpg
|   +-- img_33.jpg
|   +-- img_13.jpg
+-- 1
|   +-- img_34.jpg
|   +-- img_56.jpg
|   +-- img_23.jpg

Parameters

The imagesUrl has been mentioned to specify the source folder, but below are all of the possible parameters that can be used when instantiating a new mnist Object.

    let config: {
        this.tf = config.tf || require("@tensorflow/tfjs-node"); // Optional: TF, enables the gpu package to be passed in
        this.onlyTesting = config.onlyTesting || false; // Optional: Boolean, true if you want to test via other means and use the "predictOne" function
        this.imageLimiter = config.imageLimiter || false; // Optional: Number, % of images to use, 0.9 turns 100 images to 90 images to use (then being split into training and testing data)
        this.split = config.split || 0.75; // Optional: Float, vary the difference in training and testing data, 0.75 = 75% of the images will be used for training
        this.oldModelImageSize = config.modelImageSize || 28;  // Optional: Number, specify the input width/height of the old model
        this.modelImageShape = [this.oldModelImageSize, this.oldModelImageSize, 1];  // Using the input size to get the shape
        this.imagesUrl = config.imagesUrl || `${__dirname}/example_dataset`;  // Optional: String, specify the location of where the source folder is of the images
        this.lossFunction = config.lossFunction || 'categoricalCrossentropy';  // Optional: String, loss function for the models training phase
        this.optimizer = config.optimizer || 'rmsprop';  // Optional: tf.train / String, optimizer for the models training phase
        this.epochs = config.epochs || 5;  // Optional: Number, specify the amount of epoches to be run during the training phase
        this.batchSize = config.batchSize || 8; // Optional: Number, specify the size of batchs to be run during the training phase
    }