For this tutorial we'll use a pre-trained HyperGAN model.
Download the generator https://hypergan.s3-us-west-1.amazonaws.com/0.10/tutorial1.tflite (13.9 MB)
wget https://hypergan.s3-us-west-1.amazonaws.com/0.10/tutorial1.tflite
import numpy as npimport tensorflow as tf​# Load TFLite model and allocate tensors.interpreter = tf.lite.Interpreter(model_path="tutorial1.tflite")interpreter.allocate_tensors()
def sample():input_details = interpreter.get_input_details()output_details = interpreter.get_output_details()​# Set the 'latent' input tensor.input_shape = input_details[0]['shape']latent = (np.random.random_sample(input_shape) - 0.5) * 2.0input_data = np.array(latent, dtype=np.float32)interpreter.set_tensor(input_details[0]['index'], input_data)​interpreter.invoke()​# Get the output image and transform it for displayresult = interpreter.get_tensor(output_details[0]['index'])result = np.reshape(result, [256,256,3])result = (result + 1.0) * 127.5result = pygame.surfarray.make_surface(result)result = pygame.transform.rotate(result, -90)return result
import pygamepygame.init()display = pygame.display.set_mode((300, 300))
surface = sample()running = True​while running:for event in pygame.event.get():if event.type == pygame.QUIT:running = Falsedisplay.blit(surface, (0, 0))pygame.display.update()pygame.quit()
In the event loop:
if event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:surface = sample()
This runs the generator for a new random sample with each press of the space key.
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​
See pygame-tutorial.py​
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
hypergan train [dataset]
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.
hypergan build
This will generate a tflite
file in your build directory.
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:
hypergan new . -l
More information and help can be found in the discord.