InterFaceGAN Documentation

repository·master·Indexed 23 days ago

https://github.com/genforce/interfacegan

A framework for semantic face editing that interprets the latent spaces of GANs, such as ProgressiveGAN and StyleGAN, to control facial attributes like age, gender, and expression. The documentation covers system requirements, training and importing pre-trained networks, dataset preparation using dataset_tool.py, and utilizing generator components for image generation and style mixing.

Tokens
6K
Snippets
14
Records
27
Agent score
82%

What's inside InterFaceGAN

  1. Understand StyleGAN network snapshots (_G, _D, Gs)

    master

    When loading pre-trained networks, you may encounter different types of snapshots:

    • _G: An instantaneous snapshot of the generator. Primarily used for resuming a previous training run.
    • _D: An instantaneous snapshot of the discriminator. Primarily used for resuming a previous training run.
    • Gs: The long-term average of the generator. This version yields higher-quality results than the instantaneous snapshot and is preferred for generation tasks.
  2. Quickstart: Verify pre-trained network import

    master

    Follow these steps to verify that you can correctly import and run a pre-trained CelebA-HQ network:

    1. Add the Progressive GAN repository to your PYTHONPATH.
    2. Install dependencies: pip install -r requirements-pip.txt.
    3. Download import_example.py from the official Google Drive resources.
    4. Download karras2018iclr-celebahq-1024x1024.pkl and place it in the same directory as the script.
    5. Run the script: python import_example.py.

    If successful, the script will generate 10 PNG images (img0.png through img9.png) matching the reference images.

    pip install -r requirements-pip.txt
    python import_example.py
  3. Train StyleGAN networks

    master

    To train a StyleGAN network:

    1. Edit train.py to specify your dataset and training configuration.
    2. Run the training script: python train.py.
    3. Results are saved to results/<ID>-<DESCRIPTION>.

    Note: Training is computationally intensive and may take days or weeks. The default configuration is optimized for 8 GPUs. Training with fewer GPUs may produce different results compared to official benchmarks.

    python train.py
  4. System requirements for Progressive GAN (TensorFlow version)

    master

    To run the TensorFlow implementation of Progressive GAN, ensure your environment meets the following requirements:

    • OS: Linux (strongly recommended) or Windows.
    • Python: 64-bit Python 3.6 with numpy 1.13.3 or newer (Anaconda3 recommended).
    • GPU: One or more high-end NVIDIA Pascal or Volta GPUs with 16GB of DRAM (e.g., NVIDIA DGX-1 with 8 Tesla V100 GPUs).
    • Drivers/Toolkits: NVIDIA driver 391.25 or newer, CUDA toolkit 9.0 or newer, and cuDNN 7.1.2 or newer.
    • Dependencies: Install additional packages via pip install -r requirements-pip.txt.
  5. Evaluate quality and disentanglement with run_metrics.py

    master

    Use run_metrics.py to evaluate metrics such as Fréchet Inception Distance (fid50k) and Perceptual Path Length (ppl_zfull, ppl_wfull, etc.).

    By default, the script evaluates the pre-trained FFHQ generator and writes results to a new directory under results. You can customize the evaluation by editing run_metrics.py.

  6. Prepare datasets for training using dataset_tool.py

    master

    StyleGAN training requires datasets stored as multi-resolution TFRecords. Each dataset directory should contain the same image data in several resolutions.

    By default, scripts look for datasets at datasets/<NAME>/<NAME>-<RESOLUTION>.tfrecords. You can modify these paths in config.py using data_dir.

    Use dataset_tool.py to convert various image formats/libraries into the required TFRecord format.

  7. Train ProgressiveGAN networks

    master

    To train a new network, follow these steps:

    1. Configure: Edit config.py to specify your dataset and training configuration. You must uncomment and edit the specific lines for your desired setup.
    2. Execute: Run the training script using python train.py.
    3. Results: Outputs are saved in a new subdirectory under the directory specified by config.result_dir.

    Configuration Presets for CelebA-HQ

    Training a 1024x1024 CelebA-HQ network is computationally intensive. Use these presets in config.py to manage training time based on your hardware:

    PresetHardwareEstimated Time (V100)
    preset-v1-1gpu1x GPU~1 month
    preset-v2-1gpu1x GPU~2 weeks
    preset-v2-2gpus2x GPUs~1 week
    preset-v2-4gpus4x GPUs~3 days
    preset-v2-8gpus8x GPUs~2 days

    Key Configuration Options

    • fp16: Enables FP16 mixed-precision training to reduce training time (speedup depends on GPU architecture/cuDNN).
    • BENCHMARK / BENCHMARK0: Used to quickly iterate through resolutions to measure raw performance.
    • syn1024rgb: A synthetic 1024x1024 dataset of black images for benchmarking.
    • VERBOSE: Saves image and network snapshots frequently for debugging.
    • GRAPH and HIST: Includes additional data in TensorBoard reports.
  8. System requirements for StyleGAN

    master

    To run the official TensorFlow implementation of StyleGAN, ensure your environment meets the following specifications:

    • OS: Linux (strongly recommended) or Windows.
    • Python: 64-bit Python 3.6 (Anaconda3 with numpy 1.14.3+ is recommended).
    • TensorFlow: 1.10.0 or newer with GPU support.
    • GPU: One or more high-end NVIDIA GPUs with at least 11GB of DRAM (e.g., NVIDIA Tesla V100).
    • Drivers/Toolkits: NVIDIA driver 391.35+, CUDA toolkit 9.0+, and cuDNN 7.3.1+.
  9. Complete workflow: Prepare data, predict scores, and search boundaries

    master

    Follow these steps to discover a new semantic boundary for a ProgressiveGAN model:

    1. Prepare Data: Generate latent codes and synthesized images.

      NUM=10000
      python generate_data.py -m pggan_celebahq -o data/pggan_celebahq -n "$NUM"
    2. Predict Attribute Score: Use an external attribute predictor to evaluate the generated images. Save the results as a .npy file with shape ($NUM, 1) at data/pggan_celebahq/"$ATTRIBUTE_NAME"_scores.npy.

    3. Search Semantic Boundary: Train the boundary using the latent codes and scores.

      python train_boundary.py \
          -o boundaries/pggan_celebahq_"$ATTRIBUTE_NAME" \
          -c data/pggan_celebahq/z.npy \
          -s data/pggan_celebahq/"$ATTRIBUTE_NAME"_scores.npy
    4. Compute Conditional Boundary (Optional): If you need conditional manipulation, use project_boundary() in utils/manipulator.py to compute the projected direction.

  10. Load pre-trained StyleGAN networks

    master

    Pre-trained networks are stored as pickle files. When loading, the file yields three instances of dnnlib.tflib.Network:

    • _G: Instantaneous snapshot of the generator (useful for resuming training).
    • _D: Instantaneous snapshot of the discriminator (useful for resuming training).
    • Gs: Long-term average of the generator (yields higher-quality results; typically used for image generation).

    Requirements:

    • The dnnlib source directory must be in your PYTHONPATH.
    • A tf.Session must be set as default, initialized via dnnlib.tflib.init_tf().
    import pickle
    import dnnlib.tflib
    
    url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl
    with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f:
        _G, _D, Gs = pickle.load(f)
  11. Perform semantic face editing with edit.py

    master

    To edit facial attributes using InterFaceGAN, you need a pre-trained model, a boundary file (representing the semantic direction), and a latent code.

    Prerequisite: Download the pre-trained ProgressiveGAN model on CelebA-HQ dataset and place it in the .models/pretrain/ folder.

    Run the edit.py script with the following arguments:

    • -m: The model name (e.g., pggan_celebahq).
    • -b: Path to the boundary .npy file.
    • -n: Number of latent codes to use for editing.
    • -o: Output directory for the results.
    LATENT_CODE_NUM=10
    python edit.py \
        -m pggan_celebahq \
        -b boundaries/pggan_celebahq_smile_boundary.npy \
        -n "$LATENT_CODE_NUM" \
        -o results/pggan_celebahq_smile_editing
  12. Download pre-trained StyleGAN networks

    master

    Pre-trained networks are available as pickled instances of dnnlib.tflib.Network. You can download specific models for different datasets via the following links: