stylegan2-pytorch

repository·master·Indexed 26 days ago

https://github.com/lucidrains/stylegan2-pytorch

A PyTorch implementation of StyleGAN2 featuring command-line training, multi-GPU support, and differentiable augmentation for low-data regimes. It includes tools for image generation, latent space interpolation, and FID score calculation. Advanced features include Top-k training, Feature Quantization in the discriminator, contrastive loss regularization, and support for the Aim experiment tracker.

Tokens
1.9K
Snippets
10
Records
19
Agent score
37%

What's inside stylegan2-pytorch

  1. Train using multiple GPUs

    master

    To utilize all available GPUs on a machine, add the --multi-gpus flag. The batch size will be divided evenly across the GPUs. Use the CUDA_VISIBLE_DEVICES environment variable to restrict training to specific GPUs.

    $ stylegan2_pytorch --data ./data --multi-gpus --batch-size 32 --gradient-accumulate-every 1
  2. Generate images and interpolations

    master

    After training, use the --generate flag to create images from the latest checkpoint.

    Options:

    • --generate-interpolation --interpolation-num-steps <int>: Generate a video of interpolation through two random points in latent space.
    • --generate-interpolation --save-frames: Save each individual frame of the interpolation.
    • --generate --load-from <checkpoint_number>: Load a specific previous checkpoint (useful if a previous generator was better).
    • --generate --trunc-psi <float>: Control truncation (typically 0.5 to 1.0, default is 0.75). Lower values increase sample quality/fidelity but decrease variety.
  3. Use differentiable augmentation for low data regimes

    master

    When training with limited data (e.g., 1k-2k images), use differentiable augmentation to prevent the discriminator from overfitting.

    Configuration:

    • --aug-prob <float>: Set the augmentation probability (recommended between 0.0 and 0.7).
    • --aug-types [type1,type2,...]: Specify augmentation types. Available types are translation, cutout, and color. Note: Do not include spaces between items in the list.
  4. Calculate FID scores during training

    master

    To monitor training quality using FID scores, first install pytorch-fid, then use the --calculate-fid-every <int> flag. Results are logged to ./results/{name}/fid_scores.txt.

    $ pip install pytorch-fid
    $ stylegan2_pytorch --data ./data --calculate-fid-every 5000
  5. Configure training project settings

    master

    Use the following flags to customize your training run:

    • --name <name>: Specify a project name (defaults to default).
    • --results_dir <path>: Specify the directory for intermediate results.
    • --models_dir <path>: Specify the directory for model checkpoints.
    • --network-capacity <int>: Increase network capacity (default is 16) to improve generation at the cost of more memory.
    • --image-size <int>: Set the image resolution.
    • --batch-size <int>: Set the training batch size.
    • --gradient-accumulate-every <int>: Number of steps to accumulate gradients before updating weights.
    • --num-train-steps <int>: Total number of training steps.
    • --new: Restart training with new settings instead of resuming from the last checkpoint.
  6. Generate images and interpolations via CLI

    master

    You can use the CLI to perform inference without running a full training loop by setting the generate or generate_interpolation flags.

    • To generate static samples: Set --generate True and provide a path to a checkpoint via --load_from.
    • To generate interpolations: Set --generate_interpolation True and provide a path to a checkpoint via --load_from.

    Results will be saved to the directory specified by --results_dir using a timestamped filename.

  7. Sample images programmatically with ModelLoader

    master

    Use the ModelLoader class to load checkpoints and generate images in Python code.

    import torch
    from torchvision.utils import save_image
    from stylegan2_pytorch import ModelLoader
    
    loader = ModelLoader(
        base_dir = '/path/to/directory',   # path where you invoked the CLI
        name = 'default'                   # project name
    )
    
    noise   = torch.randn(1, 512).cuda()
    styles  = loader.noise_to_styles(noise, trunc_psi = 0.7)  # pass through mapping network
    images  = loader.styles_to_images(styles) # call generator on style vectors
    
    save_image(images, './sample.jpg')