Generate Realistic Images using StyleGAN3 and Bacalhau
In this example tutorial, we will show you how to generate realistic images with StyleGAN3 and Bacalhau. StyleGAN is based on Generative Adversarial Networks (GANs), which include a generator and discriminator network that has been trained to differentiate images generated by the generator from real images. However, during the training, the generator tries to fool the discriminator, which results in the generation of realistic-looking images. With StyleGAN3 we can generate realistic-looking images or videos. It can generate not only human faces but also animals, cars, and landscapes.
TD;LR
Generative images with Bacalhau
Prerequisite
To get started, you need to install the Bacalhau client, see more information here
Running StyleGAN3 locally
To run StyleGAN3 locally, you'll need to clone the repo, install dependencies and download the model weights.
Generate an image using a pre-trained AFHQv2
model
Viewing the output image
Containerize Script with Docker
To build your own docker container, create a Dockerfile
, which contains instructions to build your image.
:::info See more information on how to containerize your script/apphere :::
Build the container
We will run docker build
command to build the container;
Before running the command replace;
hub-user with your docker hub username, If you don’t have a docker hub account follow these instructions to create docker account, and use the username of the account you created
repo-name with the name of the container, you can name it anything you want
tag this is not required but you can use the latest tag
In our case
Push the container
Next, upload the image to the registry. This can be done by using the Docker hub username, repo name or tag.
In our case
Running a Bacalhau Job
To submit a job, run the following Bacalhau command:
Structure of the command
Let's look closely at the command above:
bacalhau docker run
: call to bacalhau--gpu 1
: No of GPUsjsacex/stylegan3
: the name and the tag of the docker image we are using../outputs
: path to the outputpython gen_images.py
: execute the script--trunc=1 --seeds=2 --network=stylegan3-r-afhqv2-512x512.pkl
: The animation length is either determined based on the --seeds value or explicitly specified using the --num-keyframes option. When num keyframes are specified with --num-keyframes, the output video length will be 'num_keyframes*w_frames' frames.
Render a latent vector interpolation video
You can also run variations of this command to generate videos and other things. In the following command below, we will render a latent vector interpolation video. This will render a 4x2 grid of interpolations for seeds 0 through 31.
Structure of the command
Let's look closely at the command above:
bacalhau docker run
: call to bacalhau--gpu 1
: No of GPUsjsacex/stylegan3
: the name and the tag of the docker image we are using../outputs
: path to the outputpython gen_images.py
: execute the script--trunc=1 --seeds=2 --network=stylegan3-r-afhqv2-512x512.pkl
: The animation length is either determined based on the --seeds value or explicitly specified using the --num-keyframes option. When num keyframes is specified with --num-keyframes, the output video length will be 'num_keyframes_w_frames' frames. If --num-keyframes is not specified, the number of seeds given with --seeds must be divisible by grid size W_H (--grid). In this case, the output video length will be '# seeds/(w*h)*w_frames' frames.
When a job is submitted, Bacalhau prints out the related job_id
. We store that in an environment variable so that we can reuse it later on.
Checking the State of your Jobs
Job status: You can check the status of the job using
bacalhau list
.
When it says Completed
, that means the job is done, and we can get the results.
Job information: You can find out more information about your job by using
bacalhau describe
.
Job download: You can download your job results directly by using
bacalhau get
. Alternatively, you can choose to create a directory to store your results. In the command below, we created a directory and downloaded our job output to be stored in that directory.
After the download has finished you should see the following contents in the results directory
Viewing your Job Output
To view the file, run the following command: