How to implement the ProGAN using tensorflow hub

Key takeaways:

  • Progressive Growing GAN models grow from low to high resolution, improving training stability and image quality.

  • TensorFlow hub offers pre-trained models for quick deployment, reducing the need for extensive training resources.

  • Implementation steps:

    • Import tensorflow and tensorflow Hub,

    • load the pre-trained ProGAN model

    • Access its default inference signature.

    • Set a random seed for reproducibility

    • Generate random input vectors, and create multiple diverse images.

Progressive growing GAN, also known as ProGAN, was introduced by Tero Karras, Timo Aila, Samuli Laine, and Jaakko Lehtinen from NVIDIA.

It enhances the traditional GAN training approach by progressively developing both the generator and discriminator. Starting from a low resolution, additional layers are incorporated to capture finer details as training advances.

This method accelerates the training process and significantly improves stability, enabling the generation of large, high quality images. One effective way to implement the ProGAN model is through tensorflow Hub.

Why use tensorflow hub?

TensorFlow Hub is a library designed for the publication, discovery, and utilization of reusable models in tensorflow. It offers an easy way to access pre-trained models for various tasks, including image classification, text analysis, and more.

Pro-GANs require huge amounts of computing to generate quality results.

The official implementation on GitHub mentions a training time of two weeks on a single GPU for the CelebA-HQ dataset. This is beyond the time and effort available for most people. Hence, we will focus on the pretrained Pro-GAN model available through tensorflow Hub.

Tensorflow Hub also provides a training mechanism to train such models from scratch.

ProGAN using tensorflow Hub

Let’s see how we can use the pre-trained ProGAN model on tensorflow Hub to generate images.

import tensorflow_hub as hub
import tensorflow as tf
pro_gan = hub.load("https://tfhub.dev/google/progan-128/1").signatures['default']
  • Line 1: We import the tensorflow hub library, which provides a way to use pre-trained models and modules in tensorflow.

  • Line 2: This imports tensorflow, the deep learning framework used for building and training neural networks.

  • Line 4: We load a pre-trained Progressive GAN model from tensorflow hub.

    • hub.load("https://tfhub.dev/google/progan-128/1"): This loads the pre-trained Progressive GAN model from the specified URL. This particular model is designed to generate images with a resolution of 128 x 128 pixels.

    • .signatures['default']: This accesses the default signature of the loaded model. In tensorflow Hub, models often have multiple signatures, which define how the model can be used (e.g., for inference, training, etc.). By specifying 'default', we’re accessing the default signature, which typically corresponds to the inference mode of the model.

After executing this code, pro_gan will be a tensorflow callable object representing the loaded Progressive GAN model, ready to generate images of size 128 x 128 pixels.

Generating images

import tensorflow_hub as hub
import tensorflow as tf
tf.random.set_seed(12)
pro_gan = hub.load("https://tfhub.dev/google/progan-128/1").signatures['default']
num_faces = 25
generated_faces = []
for _ in range(num_faces):
vector = tf.random.normal([1, 512])
sample_image = pro_gan(vector)['default'][0]
generated_faces.append(sample_image.numpy())
fig, axes = plt.subplots(5, 5, figsize=(10, 10))
fig.subplots_adjust(hspace=0.5)
for i in range(5):
for j in range(5):
axes[i, j].imshow(generated_faces[i * 5 + j].reshape(128, 128, 3).astype(np.float32))
axes[i, j].axis('off')
plt.savefig('output/generated_faces.png')
plt.show()
  • Line 4: We can set the random seed for tensorflow's random number generator to ensure reproducibility. By setting the seed to 12, the random numbers generated by tensorflow will be the same every time the code is run, which can be helpful for debugging and experimentation. This step is optional.

  • Line 10: We iterate the generated_faces list num_faces times.

  • Line 11: We generate a random vector of shape [1, 512] from a normal distribution using tensorflow's tf.random.normal function. This vector will be used as input to the GAN to generate a face.

  • Line 12: We generate a sample image of a face using the pre-trained Progressive GAN model (pro_gan) loaded earlier. It passes the random vector as input to the model and retrieves the generated image. The output is accessed via ['default'], retrieving the generated image tensor.

  • Line 13: The generated image tensor is converted to a numPy array using .numpy() and appended to the generated_faces list.

By commenting out tf.random.set_seed(12) and executing the code multiple times you can observe a broader variety of faces that this model is capable of generating.

There have been other works that focused on generating higher-resolution output samples but they lacked perceived output quality and presented a larger set of challenges for training. Progresstive GANs are a a highly effective method for generating high-quality samples that mitigates many of the challenges present in earlier works.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Can I use my own dataset with ProGAN?

Yes, we can train a ProGAN on your dataset, but this requires more computational resources and time.


What is a cGAN?

Conditional generative adversarial network, or cGAN for short, is a type of GAN that involves the conditional generation of images by a generator model.


What is the role of the discriminator?

The discriminator functions as a classifier that aims to differentiate between real data and the data generated by the generator.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved