A Pytorch implementation of the model presented in "A Style-Based Generator Architecture for Generative Adversarial Networks". No training videos this time - I've run this enough to be pretty sure it's working, but I can't justify pulling my machine off COVID19 folding long enough to get a convincing video to upload.
This is a sequel to ProGAN For Humans. The goal here is for the code to be easily interpretable, with a lot of comments explaining why we're doing things, under the assumption the reader isn't already an expert. A large amount of code here is directly reused; the major updates have been made to:
train.py
if pretrained:
condition and anywhere you see style_mixer
and style_mixes
- we use a new data loader to generate tensors that tell the model when to swap between mapping network outputs for style mixing.optimizer_G
has had a separate learning rate set for the mapping network.stylegan_models.py
forward
method has been revised to support the new loss - as a side effect, the old gradient_penalty
method is gone.stylegan_layers.py
EqualizedLinear
layer to support the mapping network, and new AdaIN
and NoiseInput
layers.stylegan_dataloader.py
StyleMixer
dataloader, which produces the tensors mentioned in train.py
's extra logic.The recommended reading order is train.py
, stylegan_dataloader.py
, stylegan_models.py
, stylegan_layers.py
. You'll want to understand what the style mixer is doing for the style mixing logic in the model itself to make sense. This doesn't feel particularly intuitive or readable, so suggestions welcome.
Compared to the code in the original project, there are 4 major differences:
As before, readability of the code was the highest priority, so some features are lacking and I would not expect a production-ready code base. In particular:
In general, I try to assume you have the paper on hand - comments don't generally explain "why"s that are covered in the paper, but instead try to give enough information that you know what part of the paper is relevant. Likewise, the code assumes a basic level of familiarity with convolutional neural networks. If you don't have that yet, I strongly recommend the following resources:
For an introduction to deep learning in general:
For an introduction to convolutional neural networks and modern training techniques:
The following Python modules are required. I recommend installing the Python requirement with venv
or something similar.
Pillow
numpy
torch
torchvision
You will also need a dataset of 1024x1024 images. In principle, you should use the FFHQ dataset. In practice, I've been trying to download this thing for a solid month but Google Drive has a quota that has kept me from succeeding.
You may also find it helpful to set the environment variable CUDA_VISIBLE_DEVICES=1,0
- your OS may be using some VRAM on your 0th GPU, so reversing them for Pytorch can give you just a bit more room on the GPU used for unparallelized scratch space.