Skip to content

googlecreativelab/teachable-machine-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Teachable Machine Boilerplate

Try this demo

This is a small boilerplate project that demonstrates how to use deeplearn.js to create projects like Teachable Machine. The code shows how you can create setup a KNN classifier that can be trained live in the browser on a webcam image. It is intentionally kept very simple so it can provide a starting point for new projects.

Behind the scenes the image from the webcam is being processed by a small neural network called Squeezenet. This network is trained to recognize all sorts of classes from the imagenet dataset, and is optimized to be really small, making is useable in the browser. Instead of reading the prediction values from the SqueezeNet network, we instead take the second to last layer in the neural network and feed it into a KNN (k-nearest neighbors) classifier that allows you to train your own classes.

The benefit of using the SqueezeNet model instead of feeding the pixel values directly into the KNN classifier is that we use the high level abstractions that the neural network has learned in order to recognize the Imagenet classes. This allows us with very few samples to train a classifier that can recognize things like smiles vs frown, or small movements in your body. This technique is called Transfer Learning.

Deeplearn.js has a built in model for doing this. Its called KNN Image Classifier Model, and this boilerplate code shows how to easily use it.

If you are insterested in using this with p5.js, ITP has created a similar example that you can find here.

Use code

To use the code, first install the javascript dependencies by running

npm install

Then start the local budo webserver by running

npm start

This will start a webserver on localhost:9966. Try and allow permission to your webcam, and add some examples by holding down the buttons.

Quick Reference

A quick overview of the most important function calls in the deeplearn.js KNN Image Classifier

  • KNNImageClassifier(numClasses, k): The constructor takes an argument of how many classes you want to train and recoginize, and a k value that is the number of neighbors looked at when doing the classification. A value of 10 can be a good starting point.

  • .load(): Downloads the SqueezeNet model from the internet, and setups the model.

  • .addImage(image, classIndex): Adds an image to the specific class training set

  • .clearClass(classIndex): Clears a specific class for training data

  • .predictClass(image): Runs the prediction on the image, and returns (as a Promise) the class index and confidence score.

See the full implementation here