HyperGAN
  • About
  • Getting started
  • CLI guide
  • Configurations
    • Configurable Parameters
  • Showcase
    • AI Explorer for Android
    • Youtube, Twitter, Discord +
  • Examples
    • 2D
    • Text
    • Classification
    • Colorizer
    • Next Frame (video)
  • Tutorials
    • Training a GAN
    • Pygame inference
    • Creating an image dataset
    • Searching for hyperparameters
  • Components
    • GAN
      • Aligned GAN
      • Aligned Interpolated GAN
      • Standard GAN
    • Generator
      • Configurable Generator
      • DCGAN Generator
      • Resizable Generator
    • Discriminator
      • DCGAN Discriminator
      • Configurable Discriminator
    • Layers
      • add
      • cat
      • channel_attention
      • ez_norm
      • layer
      • mul
      • multi_head_attention
      • operation
      • pixel_shuffle
      • residual
      • resizable_stack
      • segment_softmax
      • upsample
    • Loss
      • ALI Loss
      • F Divergence Loss
      • Least Squares Loss
      • Logistic Loss
      • QP Loss
      • RAGAN Loss
      • Realness Loss
      • Softmax Loss
      • Standard Loss
      • Wasserstein Loss
    • Latent
      • Uniform Distribution
    • Trainer
      • Alternating Trainer
      • Simultaneous Trainer
      • Balanced Trainer
      • Accumulate Gradient Trainer
    • Optimizer
    • Train Hook
      • Adversarial Norm
      • Weight Constraint
      • Stabilizing Training
      • JARE
      • Learning Rate Dropout
      • Gradient Penalty
      • Rolling Memory
    • Other GAN implementations
Powered by GitBook
On this page
  • Adding an AI character generator to pygame
  • Create your own model

Was this helpful?

  1. Tutorials

Pygame inference

PreviousTraining a GANNextCreating an image dataset

Last updated 4 years ago

Was this helpful?

Adding an AI character generator to pygame

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

Download the tflite generator

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

Load the tflite model

import numpy as np
import tensorflow as tf

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="tutorial1.tflite")
interpreter.allocate_tensors()

Sample the tflite model to a surface

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.0
  input_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 display
  result = interpreter.get_tensor(output_details[0]['index'])
  result = np.reshape(result, [256,256,3])
  result = (result + 1.0) * 127.5
  result = pygame.surfarray.make_surface(result)
  result = pygame.transform.rotate(result, -90)
  return result

Init pygame

import pygame
pygame.init()
display = pygame.display.set_mode((300, 300))

Display the surface

surface = sample()
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    display.blit(surface, (0, 0))
    pygame.display.update()
pygame.quit()

Randomize the latent variable

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.

An issue: this uses the CPU not the GPU.

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

Putting it all together

Create your own model

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

  • a GPU

  • a dataset directory of images to train against

Train your model

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.

Build your model

hypergan build

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:

hypergan new . -l

References

Download the generator (13.9 MB)

Pressing space will change the image

On desktop, it is not GPU accelerated. Unanswered question about this here:

See

a training environment

More information and help can be found in the .

https://hypergan.s3-us-west-1.amazonaws.com/0.10/tutorial1.tflite
https://stackoverflow.com/questions/56184013/tensorflow-lite-gpu-support-for-python
pygame-tutorial.py
HyperGAN
discord
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/guide/inference.md#load-and-run-a-model-in-python
https://www.github.com/HyperGAN/HyperGAN
HyperGAN