How to Use StyleGAN to Generate Faces

StyleGAN, or Style Generative Adversarial Network, is a revolutionary tool used to generate the faces of non-existent people. Nvidia researchers developed StyleGAN as an extension to the GAN architecture and made changes that greatly enhanced the outputs of this model. StyleGAN quickly became popular for being able to generate faces that are almost true to life.

AI face generators that work from prompts can also be used to generate images of animals, cars, and landscapes. What sets StyleGAN apart as one of the top random face generators, however, is not just its ability to generate photo-realistic high-quality image faces, but the ability to make adjustments to the expressions of the faces generated.

How to Use StyleGAN

Nvidia researchers, after releasing the initial version of StyleGAN in December 2018, made the source codes available in February 2019. This means there are now different ways of generating faces using StyleGAN—you can create faces with the original model released by StyleGAN or use other models that have adopted Nvidia’s codes.

Using StyleGAN

To use StyleGAN’s original codes to generate faces, you must first train the codes with a database of images. Use the following steps to train your StyleGAN code:

  • Install TFA TensorFlow and get your dataset. You can train StyleGAN using the CelebA dataset, which has over 202,000 faces.
  • For the Nadam Optimizer, set the learning rate, batch size, code size, and loss function for both the discriminator and the generator. These settings should be based on your GPU performance and memory available.
  • Regularise the image using one of three techniques available. You can choose to perform a horizontal flip with a probability of 0.5 when loading the image, prevent the model from learning the correlation between feature levels, or add random noise during training to each channel during training. These techniques result in different outputs after StyleGAN has been trained.

Depending on the features and quality of images, training StyleGAN can take between four and forty-one days and must be done on a GPU, as StyleGAN won’t train on CPUs. After training your model, you can begin generating your images with StyleGAN. After training is complete, you can move on to the next steps.

  • Compute the mean feature vector by using the generator to synthesise several images. You set the number of images to compute.
  • The feature vector used during face generation is v_mean + ψ(v — v_mean)--v is the output of the feature mapping network, v_mean is the previously computed mean feature vector, and ψ is a constant that controls the strength of the mean feature vector.
  • You can also use style mixing after the training process for the final output of images. Style mixing allows you to generate a new face from a chosen number of generated images.

Programs like StyleGAN are how AI is used to create faces. Since launching the original StyleGAN, Nvidia has gone on to improve the generator models of StyleGAN, releasing StyleGAN2 in February 2020. StyleGAN2 removes some of the characteristic artefacts of the original model and greatly improves the image quality. In October 2021, Nvidia published StyleGAN3, described as an "alias-free" version. You can also check out these newer versions for even better results.

Applications that Use StyleGAN

Since the codes of StyleGAN are open source, people train StyleGAN with their own datasets to achieve different results. There are now numerous software programs that utilise StyleGAN. Programs that have been trained with StyleGAN are often easier to use. After installing such programs or mobile apps, all users need to do to generate images is click “Generate Random Image.”

In a Nutshell

StyleGAN is an open-source, hyper realistic human face generator with easy-to-use tools and models. There are several applications of StyleGAN, further increasing its possibilities and use cases. This has made many researchers consider StyleGAN a foundation for the future of AI face generator models.