DCGAN - Deep Convolutional Generative Adversarial Networks - allow for the creation of generative images

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.


// Create a new Sentiment method
const dcgan = ml5.DCGAN('model/geo/manifest.json', modelReady);

// When the model is loaded
function modelReady() {
  // Generate a new image

function gotImage(err, result) {
  if (err) {
  // The generated image is in the result


ml5.DCGAN(?modelPath, ?callback)


  • 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"
    "description": "Aerial Images of Santiago, Chile 64x64 (16 MB)",
    "model": "model/geo/model.json",
    "modelSize": 64,
    "modelLatentDim": 128
  • callback - Required. A function to run once the model has been loaded.



Boolean value that specifies if the model has loaded.


An object that specifies the model properties


The name of the model being used to generate images



Returns "raw", "blob", and "tensor". If p5.js is available, a "p5Image" will be returned as well.

  • callback - A function to handle the results of ".generate()". Likely a function to do something with the generated image data.