-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maya Man
committed
Oct 1, 2018
1 parent
fe10dcb
commit c0586a6
Showing
5 changed files
with
53,283 additions
and
17,364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,45 @@ | ||
# Teachable Machine Boilerplate | ||
**[Try this demo](https://googlecreativelab.github.io/teachable-machine-boilerplate/)** | ||
This is a small boilerplate project that demonstrates how to use [tensorflow.js](https://github.com/tensorflow/tfjs-models) to create projects like [Teachable Machine](https://teachablemachine.withgoogle.com/). 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. | ||
|
||
This is a small boilerplate project that demonstrates how to use [tensorflow.js](https://js.tensorflow.org/) to create projects like [Teachable Machine](https://teachablemachine.withgoogle.com/). 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 an activation of [MobileNet](https://github.com/tensorflow/tfjs-examples/tree/master/mobilenet). 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 MobileNet network, we instead take the second to last layer in the neural network and feed it into a KNN ([k-nearest neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm)) classifier that allows you to train your own classes. | ||
|
||
Behind the scenes the image from the webcam is being processed by a small neural network called [SqueezeNet](https://github.com/DeepScale/SqueezeNet). This network is trained to recognize all sorts of classes from the ImageNet dataset, and is optimized to be really small, making it useable in the browser. Instead of reading the prediction values from the SqueezeNet network, we take the second to last layer in the neural network and feed it into a KNN ([k-nearest neighbors](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm)) classifier that allows you to train your own classes. | ||
The benefit of using the MobileNet 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](https://en.wikipedia.org/wiki/Transfer_learning). | ||
|
||
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](https://en.wikipedia.org/wiki/Transfer_learning). | ||
|
||
Deeplearn.js has a built in model for doing this. It's called [KNN Classifier Model](https://github.com/tensorflow/tfjs-models/tree/master/knn-classifier), and this boilerplate code shows how to easily use it. | ||
|
||
If you are interested in using this with [p5.js](https://p5js.org/), ITP has created a similar example that you can find [here](https://ml5js.org/demos/teachableMachine/). | ||
Tensorflow has a built in model for doing this. Its called the [KNN Classifier](https://github.com/tensorflow/tfjs-models/tree/master/knn-classifier), and this boilerplate code shows how to easily use it. | ||
|
||
## Use code | ||
To use the code, first install the JavaScript dependencies by running | ||
To use the code, first install the javascript dependencies by running | ||
|
||
``` | ||
npm install | ||
``` | ||
|
||
Then start the local budo web server by running | ||
Then start the local budo webserver by running | ||
|
||
``` | ||
npm start | ||
``` | ||
|
||
This will start a web server on [`localhost:9966`](http://localhost:9966). Try and allow permission to your webcam, and add some examples by holding down the buttons. | ||
This will start a webserver on [`localhost:9966`](http://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 Classifier](https://github.com/tensorflow/tfjs-models/tree/master/knn-classifier) | ||
## Quick Reference - KNN Classifier | ||
A quick overview of the most important function calls in the tensorflow.js [KNN Classifier](https://github.com/tensorflow/tfjs-models/tree/master/knn-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. | ||
- `knnClassifier.create()`: Returns a KNNImageClassifier. | ||
|
||
- `.load()`: Downloads the SqueezeNet model from the internet, and setups the model. | ||
- `.addExample(example, classIndex)`: Adds an example to the specific class training set. | ||
|
||
- `.addImage(image, classIndex)`: Adds an image to the specific class training set | ||
- `.clearClass(classIndex)`: Clears a specific class for training data. | ||
|
||
- `.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. | ||
- `.predictClass(image)`: Runs the prediction on the image, and returns an object with a top classIndex, and confidences mapping all class indices to their confidence. | ||
|
||
See the full implementation [here](https://github.com/tensorflow/tfjs-models/blob/master/knn-classifier/src/index.ts) | ||
|
||
## Quick Reference - MobileNet | ||
A quick overview of the most important function calls we use from the tensorflow.js model [MobileNet](https://github.com/tensorflow/tfjs-models/tree/master/mobilenet). | ||
|
||
- `.load()`: Loads and returns a model object. | ||
|
||
- `.infer(image, endpoint)`: Get an intermediate activation or logit as Tensorflow.js tensors. Takes an image and the optional endpoint to predict through. | ||
|
||
See the full implementation [here](https://github.com/tensorflow/tfjs-models/blob/master/mobilenet/src/index.ts) |
Oops, something went wrong.