HyperGAN
Search…
Pygame inference

Adding an AI character generator to pygame

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

Download the tflite generator

1
wget https://hypergan.s3-us-west-1.amazonaws.com/0.10/tutorial1.tflite
Copied!

Load the tflite model

1
import numpy as np
2
import tensorflow as tf
3
4
# Load TFLite model and allocate tensors.
5
interpreter = tf.lite.Interpreter(model_path="tutorial1.tflite")
6
interpreter.allocate_tensors()
Copied!

Sample the tflite model to a surface

1
def sample():
2
input_details = interpreter.get_input_details()
3
output_details = interpreter.get_output_details()
4
5
# Set the 'latent' input tensor.
6
input_shape = input_details[0]['shape']
7
latent = (np.random.random_sample(input_shape) - 0.5) * 2.0
8
input_data = np.array(latent, dtype=np.float32)
9
interpreter.set_tensor(input_details[0]['index'], input_data)
10
11
interpreter.invoke()
12
13
# Get the output image and transform it for display
14
result = interpreter.get_tensor(output_details[0]['index'])
15
result = np.reshape(result, [256,256,3])
16
result = (result + 1.0) * 127.5
17
result = pygame.surfarray.make_surface(result)
18
result = pygame.transform.rotate(result, -90)
19
return result
Copied!

Init pygame

1
import pygame
2
pygame.init()
3
display = pygame.display.set_mode((300, 300))
Copied!

Display the surface

1
surface = sample()
2
running = True
3
4
while running:
5
for event in pygame.event.get():
6
if event.type == pygame.QUIT:
7
running = False
8
display.blit(surface, (0, 0))
9
pygame.display.update()
10
pygame.quit()
Copied!

Randomize the latent variable

In the event loop:
1
if event.type == pygame.KEYDOWN:
2
if event.key == pygame.K_SPACE:
3
surface = sample()
Copied!
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

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

1
hypergan train [dataset]
Copied!
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

1
hypergan build
Copied!
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:
1
hypergan new . -l
Copied!
More information and help can be found in the discord.

References

Last modified 1yr ago