Pygame inference

Adding an AI character generator to pygame

For this tutorial we'll use a pre-trained HyperGAN model.

Download the tflite generator

Download the generator https://hypergan.s3-us-west-1.amazonaws.com/0.10/tutorial1.tflite (13.9 MB)

Load the tflite model

Sample the tflite model to a surface

Init pygame

Display the surface

Randomize the latent variable

In the event loop:

This runs the generator for a new random sample with each press of the space key.

Pressing space will change the image

An issue: this uses the CPU not the GPU.

This technique uses the tflite interpreter which was created for mobile devices.

On desktop, it is not GPU accelerated. Unanswered question about this here: https://stackoverflow.com/questions/56184013/tensorflow-lite-gpu-support-for-python

Putting it all together

See pygame-tutorial.py

Create your own model

If you want to train a model from scratch, you will need:

  • a GPU

  • a HyperGAN training environment

  • a dataset directory of images to train against

Train your model

This will take several hours. A view will display the training progress.

You will need to save and quit the model when you are satisfied with the results.

Build your model

This will generate a tflite file in your build directory.

Fine tune your results

There are many differing configurations you can use to train your GAN and each decision will effect the final output.

You can see all the prepacked configurations with:

More information and help can be found in the discord.

References

Last updated

Was this helpful?