GigaGAN-PyTorch

repository·main·Indexed 23 days ago

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

An implementation of the GigaGAN architecture for high-resolution image synthesis. It supports unconditional training, Unet upsamplers (1k-4k), and various auxiliary losses for stability and convergence. The library includes the GigaGAN and ImageDataset classes and supports multi-GPU training via 🤗 accelerate.

Tokens
1.5K
Snippets
4
Records
5
Agent score
34%

What's inside gigagan-pytorch

  1. Perform Multi-GPU training with Accelerate

    main

    The GigaGAN class supports 🤗 accelerate. To perform multi-GPU training, use the accelerate CLI in your project root:

    1. Configure your environment: accelerate config
    2. Launch your training script: accelerate launch train.py
    $ accelerate config
    
    $ accelerate launch train.py
  2. Use GigaGAN for unconditional GAN training

    main

    To train a simple unconditional GAN, initialize the GigaGAN class with generator and discriminator configuration dictionaries. You must also use ImageDataset to load your data and call gan.set_dataloader(dataloader) before starting the training loop with gan(steps=..., grad_accum_every=...).

    import torch
    
    from gigagan_pytorch import (
        GigaGAN,
        ImageDataset
    )
    
    gan = GigaGAN(
        generator = dict(
            dim_capacity = 8,
            style_network = dict(
                dim = 64,
                depth = 4
            ),
            image_size = 256,
            dim_max = 512,
            num_skip_layers_excite = 4,
            unconditional = True
        ),
        discriminator = dict(
            dim_capacity = 16,
            dim_max = 512,
            image_size = 256,
            num_skip_layers_excite = 4,
            unconditional = True
        ),
        amp = True
    ).cuda()
    
    # dataset
    
    dataset = ImageDataset(
        folder = '/path/to/your/data',
        image_size = 256
    )
    
    dataloader = dataset.get_dataloader(batch_size = 1)
    
    # you must then set the dataloader for the GAN before training
    
    gan.set_dataloader(dataloader)
    
    # training the discriminator and generator alternating
    # for 100 steps in this example, batch size 1, gradient accumulated 8 times
    
    gan(
        steps = 100,
        grad_accum_every = 8
    )
    
    # after much training
    
    images = gan.generate(batch_size = 4) # (4, 3, 256, 256)
  3. Use GigaGAN for Unet Upsampler training

    main

    To train a Unet Upsampler, set train_upsampler = True in the GigaGAN constructor. The generator configuration should include input_image_size (the resolution of the low-res input). After training, you can generate high-resolution images by passing a low-resolution tensor to gan.generate(lowres).

    import torch
    from gigagan_pytorch import (
        GigaGAN,
        ImageDataset
    )
    
    gan = GigaGAN(
        train_upsampler = True,     # set this to True
        generator = dict(
            style_network = dict(
                dim = 64,
                depth = 4
            ),
            dim = 32,
            image_size = 256,
            input_image_size = 64,
            unconditional = True
        ),
        discriminator = dict(
            dim_capacity = 16,
            dim_max = 512,
            image_size = 256,
            num_skip_layers_excite = 4,
            multiscale_input_resolutions = (128,),
            unconditional = True
        ),
        amp = True
    ).cuda()
    
    dataset = ImageDataset(
        folder = '/path/to/your/data',
        image_size = 256
    )
    
    dataloader = dataset.get_dataloader(batch_size = 1)
    gan.set_dataloader(dataloader)
    
    # training the discriminator and generator alternating
    # for 100 steps in this example, batch size 1, gradient accumulated 8 times
    
    gan(
        steps = 100,
        grad_accum_every = 8
    )
    
    # after much training
    
    lowres = torch.randn(1, 3, 64, 64).cuda()
    
    images = gan.generate(lowres) # (1, 3, 256, 256)
  4. Monitor GigaGAN training losses

    main

    When monitoring training, look for the following loss keys. A healthy run typically has G, MSG, D, and MSD values hovering between 0 and 10. If these values stay in the triple digits after 1k steps, there may be an issue. GP and SSL should be pushed towards 0.

    Loss Key Reference:

    • G: Generator
    • MSG: Multiscale Generator
    • D: Discriminator
    • MSD: Multiscale Discriminator
    • GP: Gradient Penalty
    • SSL: Auxiliary Reconstruction in Discriminator
    • VD: Vision-aided Discriminator
    • VG: Vision-aided Generator
    • CL: Generator Contrastive Loss
    • MAL: Matching Aware Loss