CVAE: Condtional Variational Autoencoder

generated images from google quickdraw

Description

An autoencoder is an neural network that learns how to encode data (like the pixels of an image) into a smaller representation. This is akin to image compression (although classic image compression algorithms are better!) A Variational Autoencoder (VAE) takes this idea one step further and is trained to generate new images in the style of training data by sprinkling in a little bit of randomness. Conditional Variational Autoencoder (CVAE) is an extension of this idea, with the ability to be more specific about what is generated. From Two Minute Papers, the author explains that:

“Autoencoders are neural networks that are capable of creating sparse representations of the input data and can therefore be used for image compression. There are denoising autoencoders that after learning these sparse representations, can be presented with noisy images. What is even better is a variant that is called the variational autoencoder that not only learns these sparse representations, but can also draw new images as well. We can, for instance, ask it to create new handwritten digits and we can actually expect the results to make sense!”

Quickstart

  1. const cvae = ml5.CVAE('model/quick_draw/manifest.json', modelReady);
  2. function modelReady() {
  3. // generate an image of an airplane
  4. cvae.generate('airplane', gotImage);
  5. }
  6. function gotImage(error, result) {
  7. if (error) {
  8. console.log(error);
  9. return;
  10. }
  11. // log the result
  12. console.log(result);
  13. }

Usage

Initialize

  1. const magic = ml5.CVAE(?model, ?callback);

Parameters

  • model: REQUIRED. The url path to your model. Can be an absolute or relative path.
  • callback: REQUIRED. A function to run once the model has been loaded.

Properties


.ready

BOOLEAN. Boolean value that specifies if the model has loaded.


Methods


.generate(label, callback);

Given a label, will generate an image.

  1. cvae.generate(label, callback);

📥 Inputs

  • label: REQUIRED. String. A label of the feature your want to generate.
  • callback: REQUIRED. Function. A function to handle the results of “.generate()”. Likely a function to do something with the generated image data.

📤 Outputs

  • Object: Returns “raw”, “blob”, and “tensor”. If p5.js is available, a “p5Image” will be returned as well.

Examples

p5.js

p5 web editor

plain javascript

Demo

No demos yet - contribute one today!

Tutorials

No tutorials yet - contribute one today!

Acknowledgements

Contributors:

  • Wenhe Li & Dingsu (Derek) Wang

Credits:

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

Source Code

/src/CVAE/