Home

Writing

Dec 2017

Monster GANs

In this blog, we are going to generate monsters. To do this, we are going to use something called a Generative Adversarial Network (GAN). This is a special type of generative model that can generate novel outputs given some set of inputs. GANs learn features/traits from the input data and produce outputs with similar features. In this blog post, I'm going to generate monsters you could use in your indie game. This could save you many hours of work. Generated monsters are also a great starting point for further artwork.

We will turn random colored noise (64x64 pixels):

Into monsters like these (64x64 pixels):

Here are the steps we'll take:

1 — Collect and prepare the data.

2 — Create a GAN.

3 — Train the GAN.

4 — Generate monsters!

1 — Collect and prepare the data

We need data that look like monsters. I started going scrounging up pixel art on pinterest and other places, but the problem with most of that is that it's not at the same resolution, and it's not at the same style. You can deal with both these issues I wanted to keep things simple. Eventually, I settled on using some open source pokemon art found on two different pokemon community forums. Overall in my dataset, there are 14,774 JPG images of varying sizes. I'm calling this dataset the mgan, 'monster gan,' data set.

The mgan dataset.

2 — Setup and GAN Creation

Now we'll start laying down some code. We're going to use pytorch to create our model and train it. I do not recommend that you run this without a powerful GPU. If you need a GPU, check out Floydhub. You can pay by the hour there, and it's pretty affordable. In a GAN, our generator model will learn how to make monsters by making some, then asking another model, 'does my monster look like a real monster?' This other model will be called the discriminator. The discriminator will tell the generator how bad its attempt to create a monster was. The generator will then take this information and improve its parameters to generate better monsters hopefully. We repeat this process 100–200 times and eventually get a generator that can create novel monsters.

Let’s load some data:

This code should output a pokemon. Mine looks like this:

I just wanted to check that we could read and output one image from the mgan dataset. Now we'll get to the good stuff. The GAN, the stuff that actually generates new monsters:

This looks long, but if you read the comments, it's not too bad. It just describes a generator model and a discriminator model and then creates one of each.

3 — Training the GAN

During training, this should output lines like so:

They show the epoch (the training round) and how bad the generator is at generating images, and the discriminator losses show how good the discriminator is at its job. These values are not necessarily interpretable. Each line shows an image of 64 sample monsters created by the generator from the fixed_noise value. The quality of this image should be increasing steadily. Every epoch's generator is saved with the epoch tacked onto the end.

If you see one that generates particularly realistic monsters or a style you prefer, you can use that generator from that epoch.

4 — Generating Monsters!

I trained 125 generator epochs, and in the end, I decided that the generator from epoch 90 was the best one (choosing is more art than science). After generating the monsters, you may notice most of the monsters generated look like blobs or cronenberg monsters. We can generate an infinite number of monsters by passing in new random noise. As long as a few are decent, we can keep generating monsters until we get enough good ones.

This could output something like this:

Most of these just look like blobs, but some are cool. These two are my favorite from this batch:

If you re-run the noise = line you'll keep getting new monsters. You may notice some similar monsters generated each time. If you keep generating, you should build up a pretty decent collection of unique monsters.

Conclusion and GIF

You can see the full notebook code for this blog post here. In this post, we walked through how to use a GAN to generate monsters. You can use them in your games/game prototypes. You can use them as a starting point for more developed artwork. You can use them just to get ideas. GANs are a really great creative tool and a lot of fun. One warning, though: it is possible to spend a week messing with it and have it not work. Stick to the numbers here, and you should be ok.

As promised, below is an animation that illustrates the process of training the generator. Each frame is a snapshot of its progress as it goes from grainy to crisp and detailed:

If you enjoyed this post, be sure to signup for more.

Signup For More