> For the complete documentation index, see [llms.txt](https://hypergan.gitbook.io/hypergan/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hypergan.gitbook.io/hypergan/tutorials/pygame.md).

# Pygame inference

## Adding an AI character generator to pygame

![](https://github.com/HyperGAN/HyperGAN/tree/91d0ea7885e2ccb443dddfe012eeed29bfce9487/docs/.gitbook/assets/pygame-tutorial-1.png)

For this tutorial we'll use a pre-trained [HyperGAN](https://www.github.com/HyperGAN/HyperGAN) model.

#### Download the tflite generator

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

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

#### Load the tflite model

```python
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

```python
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

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

#### Display the surface

```python
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:

```python
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.

![Pressing space will change the image](https://github.com/HyperGAN/HyperGAN/tree/91d0ea7885e2ccb443dddfe012eeed29bfce9487/docs/.gitbook/assets/pygame-tutorial-2.png)

#### 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](https://github.com/HyperGAN/HyperGAN/tree/91d0ea7885e2ccb443dddfe012eeed29bfce9487/docs/tutorials/pygame-tutorial.py)

### Create your own model

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

* a GPU
* a [HyperGAN](https://www.github.com/HyperGAN/HyperGAN) training environment
* a dataset directory of images to train against

#### Train your model

```bash
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

```bash
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:

```bash
hypergan new . -l
```

More information and help can be found in the [discord](https://discord.gg/t4WWBPF).

#### References

* <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>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hypergan.gitbook.io/hypergan/tutorials/pygame.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
