DCGAN: Deep Convolutional Generative Adversarial Networks

generated map images

Description

You can use neural networks to generate new content. A Generative Adversarial Network (GAN) is a machine learning architecture where two neural networks are adversaries competing. One neural network is a “generator”, it makes new images. The other is a “discriminator” and tries to guess if the image is “fake” (made by the generator) or “real” (from the training data). Once the discriminator can no longer guess correctly, the model is trained! A DCGAN is a Deep Convolutional Generative Adversarial Network.

ml5.js provides a few default pre-trained models for DCGAN, but you may consider training your own DCGAN to generate images of things you’re interested in.

Quickstart

  1. const dcgan = ml5.DCGAN('model/geo/manifest.json', modelReady);
  2. // When the model is loaded
  3. function modelReady() {
  4. // Generate a new image
  5. dcgan.generate(gotImage);
  6. }
  7. function gotImage(err, result) {
  8. if (err) {
  9. console.log(err);
  10. return;
  11. }
  12. // The generated image is in the result
  13. console.log(result);
  14. }

Usage

Initialize

  1. const dcgan = ml5.DCGAN(modelPath, callback);

Parameters

  • modelPath: REQUIRED. This will be a JSON object called manifest.json that contains information about your pre-trained GAN and the url to the model.json file that contains your pre-trained model. The model property can also point to an absolute URL e.g. "https://raw.githubusercontent.com/ml5js/ml5-data-and-models/master/models/dcgan/face/model.json"

    1. {
    2. "description": "Aerial Images of Santiago, Chile 64x64 (16 MB)",
    3. "model": "model/geo/model.json",
    4. "modelSize": 64,
    5. "modelLatentDim": 128
    6. }
  • callback: Required. A function to run once the model has been loaded.

Properties


.modelReady

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



.model

Object. An object that specifies the model properties



.modelPath

String. The name of the model being used to generate images


Methods


.generate()

Given a number, will make magicSparkles

  1. dcgan.generate(callback, ?latentVector);

📥 Inputs

  • callback: REQUIRED. Function. A function to handle the results of .generate(). Likely a function to do something with the generated image data.
  • latentVector: OPTIONAL. An array. A vector to explore the latent space of the model. If no latentVector is given, then a random “location” in the latent space is returned.

📤 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 demos yet - contribute one today!

Acknowledgements

Contributors:

  • YG Zhang & Rui An
  • Additional contributions by Joey Lee

Credits:

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

Source Code