FeatureExtractor

Illustration of a brain being sent through a funnel

Description

You can use neural networks to recognize the content of images. Most of the time you will be using a “pre-trained” model trained on a large dataset to classify an image into a fixed set of categories. However you can also use a part of the pre-trained model: the features. Those features allow you to ‘retrain’ or ‘reuse’ the model for a new custom task. This is known as Transfer Learning.

This class allows you to extract features of an image via a pre-trained model and re-train that model with new data.

Quickstart

  1. // Extract the already learned features from MobileNet
  2. const featureExtractor = ml5.featureExtractor('MobileNet', modelLoaded);
  3. // When the model is loaded
  4. function modelLoaded() {
  5. console.log('Model Loaded!');
  6. }
  7. // Create a new classifier using those features and with a video element
  8. const classifier = featureExtractor.classification(video, videoReady);
  9. // Triggers when the video is ready
  10. function videoReady() {
  11. console.log('The video is ready!');
  12. }
  13. // Add a new image with a label
  14. classifier.addImage(document.getElementById('dogA'), 'dog');
  15. // Retrain the network
  16. classifier.train((lossValue) => {
  17. console.log('Loss is', lossValue);
  18. });
  19. // Get a prediction for that image
  20. classifier.classify(document.getElementById('dogB'), (err, result) => {
  21. console.log(result); // Should output 'dog'
  22. });

Usage

Initialize

  1. // initial without options
  2. const featureExtractor = ml5.featureExtractor(model, ?callback);
  3. // OR with options included
  4. const featureExtractor = ml5.featureExtractor(model, ?options, ?callback);

Parameters

  • model: REQUIRED. The model from which extract the learned features. Case-insensitive

  • callback: OPTIONAL. A function to be executed once the model has been loaded. If no callback is provided, it will return a promise that will be resolved once the model has loaded.

  • options: OPTIONAL. An object containing custom options. For the MobileNet model these are the custom options you can reset.

    1. {
    2. version: 1,
    3. alpha: 1.0,
    4. topk: 3,
    5. learningRate: 0.0001,
    6. hiddenUnits: 100,
    7. epochs: 20,
    8. numLabels: 2,
    9. batchSize: 0.4,
    10. };

Properties


.modelLoaded

Boolean value that specifies if the model has loaded.



.hasAnyTrainedClass

Boolean value that specifies if new data has been added to the model



.usageType

String that specifies how is the Extractor being used. Possible values are ‘regressor’ and ‘classifier’



.isPredicting

Boolean value to check if the model is predicting.


Methods


.classification(?video, ?callback)

Use the features of MobileNet as a classifier

  1. featureExtractor.classification(?video, ?callback);

📥 Inputs

  • video: Optional. An HTML video element or a p5.js video element.
  • callback: Optional. A function to be called once the video is ready. If no callback is provided, it will return a promise that will be resolved once the video element has loaded.

📤 Outputs

  • n/a


.regression()

Use the features of MobileNet as a regressor

  1. featureExtractor.regression(?video, ?callback);

📥 Inputs

  • video: Optional. An HTML video element or a p5.js video element.
  • callback: Optional. A function to be called once the video is ready. If no callback is provided, it will return a promise that will be resolved once the video element has loaded.

📤 Outputs

  • n/a


.addImage()

Adds a new image element to the featureExtractor for training

  1. featureExtractor.addImage(label, ?callback);
  2. // OR
  3. featureExtractor.addImage(input, label, ?callback);

📥 Inputs

  • input - Optional. An HTML image or video element or a p5 image or video element. If not input is provided, the video element provided in the method-type will be used.
  • label - The label to associate the new image with. When using the classifier this can be strings or numbers. For a regressor, this needs to be a number.
  • callback - Optional. A function to be called once the new image has been added to the model. If no callback is provided, it will return a promise that will be resolved once the image has been added.

📤 Outputs

  • n/a


.train()

Retrain the model with the provided images and labels using the models original features as starting point.

  1. featureExtractor.train(?callback);

📥 Inputs

  • callback - Optional. A function to be called to follow the progress of the training.

📤 Outputs

  • n/a


.classify()

Classifies an an image based on a new retrained model. .classification() needs to be used with this.

  1. featureExtractor.classify(?callback);
  2. // OR
  3. featureExtractor.classify(input, ?callback);

📥 Inputs

  • input - Optional. An HTML image or video element or a p5 image or video element. If not input is provided, the video element provided in the method-type will be used.
  • callback - Optional. A function to be called once the input has been classified. If no callback is provided, it will return a promise that will be resolved once the model has classified the image.

📤 Outputs

  • Object - returns the {label, confidence}


.predict()

Predicts a continuous value based on a new retrained model. .regression() needs to be used with this.

  1. featureExtractor.predict(?callback);
  2. // OR
  3. featureExtractor.predict(input, ?callback);

📥 Inputs

  • input - Optional. An HTML image or video element or a p5 image or video element. If not input is provided, the video element provided when creating the regression will be used.
  • callback - Optional. A function to be called once the input has been predicted. If no callback is provided, it will return a promise that will be resolved once the model has predicted the image.

📤 Outputs

  • Object - an object with {value}.


.save()

Saves your model to the downloads folder of your machine.

  1. featureExtractor.save(?callback, ?name);

📥 Inputs

  • callback - Optional. A function to be called once the input has been predicted. If no callback is provided, it will return a promise that will be resolved once the model has predicted the image.
  • name - Optional. A name that you’d like to give to your saved model. This should be a text string. The default is

📤 Outputs

  • Downloads a model.json and model.weights.bin file to your downloads directory.


.load()

Allows you to load your model from a URL path or via an HTML input file reader.

  1. featureExtractor.load(filesOrPath = null, callback);

📥 Inputs

  • filesOrPath - A path to your model.json if you are using a string path. If you are using the HTML file input, then select BOTH the model.json and the model.weights.bin` files.
  • callback - Optional. A function to do after your model has been loaded

📤 Outputs

  • Returns the loaded model.

Examples

p5.js

p5 web editor

plain javascript

Demo

No demos yet - contribute one today!

Tutorials

ml5.js Feature Extractor Classification via CodingTrain

ml5.js Transfer Learning with Feature Extractor via CodingTrain

ml5.js Feature Extractor Regression via CodingTrain

Acknowledgements

Contributors:

  • Yining Shi & Cristobal Valenzuela

Credits:

  • Paper Reference | Website URL | Github Repo | Book reference | etc

Source Code

src/FeatureExtractor