GANsplaining: What generative models are and how to tame them

A plain-language introduction to the trendy method in generative art.

This post was automatically translated from the using Claude Fable 5.

Ever since the little Chilean flag made by an Artificial Intelligence (AI) became popular—the one with a red background and a dog as its emblem—where in reality its description was generated by an AI, not the famous image—it has become more and more common to run into someone on social media sharing an "I typed X into an AI and it generated this".

The goal of this post is to explain in plain language precisely how we've managed to create images from text. What matters most to me with this publication is that people who don't know how to code and aren't close to mathematics understand the general concepts behind it. So rest assured: you won't need to have taken a calculus course to follow along.

That said, this is also why I owe a warning to the people who do know the field: I will commit some aberrations for the sake of simplifying the subject for the general public—it seems like a fair tradeoff to me.

What are generative models?

To explain what a generative model is, it's convenient to first explain what a classifier model is.

Hey, but what the hell is a model.

When we talk about models in the context of AI, we mean a piece of software that was trained to solve some problem.

A typical example is building a model that tells photos of dogs and cats apart. To achieve this, we need beforehand a collection of photos of both animals (which we call a dataset), and then design a series of steps that allow the software, through trial and error, to gradually learn visual patterns that distinguish dogs from cats.

Humanizing this a bit, it would be like showing this model the photos one by one and pressing it with the question: What animal is this?

If it answers correctly, all good. If it gets it wrong, we let it know and the model adjusts itself a little bit, hopefully to not repeat that mistake again.

Classifier models (🍐 vs 🍎)

In the previous example, we built what we call a classifier model, since it learned to classify things. But since we're talking about science, we should be a bit more rigorous and define what classifying means.

Here comes the part that might trigger a reaction along the lines of "I'm out, this thing is in Korean". I promise that if you give me a chance and keep reading, you'll get it.

Classifying, in our situation, is the act of indicating whether something belongs to a distribution or not.

In plain Chilean, this means knowing how to tell pears from apples. To pull that off, we need to somehow understand what makes an apple an apple, and in contrast, a pear a pear.

One way to do it would be to look at every example of apples and pears that ever was and ever will be. But nature is beautiful and gave us the ability to intuitively infer, upon seeing a new apple or pear, which family it belongs to.

We could say that, in such cases, you are assigning a probability that a specimen is a pear, or else an apple. And to do this, we hold an unconscious idea of what these fruits are; that is, we infer the distribution of pears and apples.

In summary:

  • Model: Software that learns to solve something.
  • Distribution: Family of probabilities of things (e.g.: red apples belong to the distribution of apples).
  • Classifier model: Model built to recognize whether something belongs to a distribution (e.g.: that apple belongs to the distribution of apples).

Generative Models

The difference between a classifier model and a generative one is that, while the former seeks to learn to recognize whether something belongs to a distribution, the latter wants to learn to represent that distribution.

Let's go back to the pears and apples example. Remember I mentioned that one possible way to recognize these fruits is to have seen beforehand every pear and apple that exists and will ever exist, but that in practice we don't do this since we infer their differences, or more precisely, their distribution?

What representing that distribution implies is precisely simulating having seen every pear and apple that ever was and ever will be. That is, by observing some pears and apples, we seek to understand what all the other pears and apples that exist—or that could even exist—will look like.

There are many ways to create generative models. But all of these methods require real photos of the distribution to be learned (the dataset). It is with this ingredient that, through trial and error, they gradually learn to recreate elements not present in the dataset, or in other words, to generate things.

Generative Adversarial Networks

Even though, as I just mentioned, many different generative models exist, I'll take a moment here to talk about the one that fascinates me the most and marked my life, pulling me into this AI niche: Generative Adversarial Networks (GANs).

As their name indicates, GANs are generative models that manage to learn to represent a distribution through an adversarial game.

What does adversarial mean here?

That it involves models competing against each other.

GANs operate under a game scheme with two participants: a Discriminator and a Generator. The discriminator is basically a classifier model, while the generator, as you might imagine, is a generative model.

The rules of the game are quite simple:

  • We have a dataset of images.
  • The generator will generate images, while the discriminator will classify images both from the dataset and those created by the generator.
  • If the discriminator correctly recognizes that an image is fake or real, it gets rewarded.
  • If the generator fools the discriminator—that is, the discriminator classifies a generated image as real—it gets rewarded.

Under these rules—and quite a few more technical details—what ends up happening is that as the discriminator and generator learn to catch each other out, each one improves at its role, which finally lands in a technical draw. The discriminator will manage to recognize whether a photo is real or not with only 50% success, because the generator will produce photos indistinguishable from real ones—for readers familiar with game theory, this technical draw happens because we force the optimal equilibrium of a two-player Minimax competition.

And that's GANs!

I personally find it beautiful how Ian Goodfellow (the creator of GANs) managed, back in 2014, to solve the problem of how to create a generative model using a competition scheme against another classifier. This is a case where healthy competition can bring good results.

Latent Space

There's one last very important detail to understand about generative models before we can explain how we manage to steer them with text: the latent space.

This concept—whose name might sound like it was pulled from a sci-fi cosmic horror curse—refers to the distribution learned by the model. Because as awesome as these models are, we must accept that they learn a distribution that isn't necessarily equal to the real one. That is, they may learn to generate infinite apples, but not necessarily every apple.

But the latent space is really more than this—new "this dude started speaking Korean again" alert, give me a few paragraphs to explain myself better—it also refers to the fact that we represent the distribution within a metric space.

What do I mean by this?

Generated images don't come out of nowhere; we need a way to control what the model generates. Otherwise, how could we make it generate the same image on different occasions?

To achieve this control, generative models receive a number—actually a list of numbers, but let's keep things simple—from which they generate an image. That is, every time I feed my model a 0, it will generate the same image. So we could say the 0 corresponds to that image. Likewise, no matter how many times we give it a 1, the model will generate the same image (different, though, from the 0's one).

Here comes an interesting consequence of the above. Between 0 and 1 there are more numbers—in fact, infinitely many: 0.1, 0.15, 0.2, 0.32, 0.453654765, etc. What happens then if we move continuously between 0 and 1?

As you can see, the image changes continuously, starting from the one corresponding to 0 until reaching the one for 1. This happens because, in their need to learn to represent a distribution while doing so by associating numbers with the generated images, generative models are forced to learn to develop a space where placing every element of the distribution will make sense.

Let me try to explain this with an example:

Imagine you're asked to organize thousands of post-its of different colors on a very large whiteboard. What would be the most intuitive way to do it? If you've worked with any software that requires heavy color handling, you'll know the best approach would probably be to replicate the color palettes they use. That is, similar colors close to one another, and sorted along different axes according to some parameter like intensity, brightness, or whatever.

Example of how colors are organized in Photoshop

A generative model tries to solve this very same problem, but instead of a whiteboard it has a latent space, and instead of thousands of post-its, it has infinite images.

CLIP

Now it's time to explain the second leg of the matter: CLIP.

CLIP (Contrastive Language-Image Pre-Training) is a model published at the beginning of this year that was trained with the goal of pairing text and images. That is, upon receiving a text and an image, it tells you whether the two are related.

In other words, if I hand it a photo of an apple along with the text "photo of an apple", the idea would be for CLIP to tell me "yes, this text and image are related".

Note, though, that the way CLIP confirms this correlation is through a number between 0 and 1, where 0 is an "absolutely unrelated" and 1 is "they are exactly the same thing, but expressed in different mediums".

It's important to stress the in different mediums. CLIP's stroke of genius was that it managed to understand, to a certain degree, when things are the same but expressed through different mediums. We call this multimodality, and it's a property that even occurs in our own brain.

It was first observed during the 2000s, when researchers noticed that groups of our neurons specialize in understanding concepts, even when those concepts are expressed through different mediums.

Using the same example given by the authors who discovered this: if we compare a person's neural activity when seeing a photo of Jennifer Aniston versus when hearing the name Jennifer Aniston, we'll observe that almost the same neurons fire. And it goes further: if this person hears the famous Friends intro, they'll show similar activations too.

CLIP in a nutshell

It's for this last reason that achieving multimodality is something revolutionary. It allows understanding relationships beyond a mere "this photo and this text represent the same thing". For example, CLIP outputs a high relatedness number when given the text "Daniel Radcliffe" and a photo of a Harry Potter book cover.

You could say we're approaching a world where AI will understand cultural relationships between things, even when they're represented in different mediums.

Generative Models + CLIP

Let's recap what we've seen so far:

  1. Generative models learn distributions (e.g.: learning to generate photos of apples).
  2. By learning this distribution, they produce a latent space, a space where nearby places correspond to generating similar things (e.g.: the apple generated with the number 0.00000001 will look a lot like the one that comes out with 0.00000002).
  3. CLIP is a model that learned to confirm whether a text and an image are about similar things, even when represented in different mediums (e.g.: a photo of an apple and the text "an apple" are about the same thing).
  4. CLIP outputs a number between 0 and 1 to report the intensity of the relationship between a text and an image.

With all these ingredients, we can finally understand how we can generate an image from text.

At the beginning of this year, very shortly after CLIP went public, the generative art community developed a way to take advantage of its multimodal properties.

We already know CLIP gives feedback on whether a text and an image are related. Therefore, it doesn't matter if the image it receives was generated by another model—it will be just as capable of telling us whether it relates to a text.

And this is where the latent space, and the fact that CLIP outputs a number to tell us how strong the relationship is, come into play.

Thanks to the fact that if we move continuously through the latent space the generated images will also change continuously, and that, on the other hand, with CLIP we can evaluate how much an image relates to a text, we can then design a way to guide ourselves within this latent space to find a spot where the relationship between text and image is stronger.

Here's an example of how this is done:

Picture a person hunting for treasure on the beach with a metal detector. The way the detector tells its user how close they are to metal is by firing off sequences of beeps. The closer it is to metal, the more beeps it emits, which helps guide the treasure hunter toward where they should move on the sand.

When we couple CLIP with a generative model we're doing exactly this. Our metal detector is CLIP, where instead of detecting metal we detect how related a text and an image are, and instead of beeps we're informed by a number between 0 and 1. Meanwhile, instead of moving along a beach, we move through the latent space. With each step we take, we receive CLIP's beep, giving us a sense of where to move to find the precise point that generates an image matching the text we provided.

CLIP guiding a StyleGAN to generate an image related to "A portrait of Nicanor Parra"

This way, we could summarize the algorithm for generating images from text in the following steps:

  1. We choose a text (e.g.: "a red apple").
  2. We stand at some point in the latent space of our favorite generative model, thereby generating an image.
  3. CLIP compares the image generated in the previous step against the text from 1), outputting a number between 0 and 1.
  4. We iteratively move through the latent space, generating an image at each step and repeating 3), thus trying to get closer, little by little, to an image that yields a higher number.

Note that this process implies a couple of things:

First, the point of the latent space we choose to start from will determine what ends up being generated. Going back to the treasure hunter analogy, starting the search at one end of a beach is not the same as starting at the other. They will probably end up finding different things depending on the starting point, given that the metal detector will guide the user down different paths. This becomes more evident the larger the beach is.

Second, the generated images are limited both by the generative model and by CLIP. That is, it may happen that a generative model does have the capacity to generate an image strongly related to a text, but CLIP lacks the capacity or sufficient training to competently understand the relationship. Likewise, it's possible that a generative model lacks the capacity to generate CLIP's ideal image.


Generative Art and where to find it

I'm not quite sure how to end my first post, so I'd simply like to close by sharing people and projects you can follow if this little world interests you.

It would be a crime not to start by mentioning Katherine Crowson. She is without a doubt one of the leaders of this growing niche, having developed—from the comfort of her home and without the backing of a mega corporation—many of the standard techniques of the generative art world.

Other somewhat-famous people in this scene are (in no particular order):

I should also mention the existence of EleutherAI, a decentralized, anonymous group of researchers and developers organized through Discord, where many of these innovations have been created and discovered (most of the people I'm sharing here inhabit those chats). This community has managed to stand up to giants of the field like Google, OpenAI, and the rest.

Finally, I'll share platforms where you can play with these models:

  • Notebooks created by Katherine Crowson:
  • wombo.art: this is probably the app that almost everyone outside the field uses when they say "I typed x into an AI and it generated this". You can tell it runs a VQGAN under the hood, and honestly there are far better options if you want to generate things from text with this model (like, for instance, the first notebook I shared from Katherine).
  • Hugging Face Spaces: Here you'll find tons of different models ready to be tried out. It's a platform designed so researchers can share their creations behind a friendly interface.
  • StyleGAN3+CLIP. I'm proud to say this is the world's most popular notebook for steering a StyleGAN3 with CLIP, and it was created by me. 😊