GFPGAN: Generative Facial Prior GAN

repository·master·Indexed 12 days ago

https://github.com/tencentarc/gfpgan

A generative facial prior model for real-world blind face restoration. It restores high-quality facial details from degraded images using pre-trained models like StyleGAN2 and ArcFace. Supports multiple model versions (V1, V1.2, V1.3) to balance naturalness and sharpness, and provides a CLI for inference via inference_gfpgan.py.

Tokens
2.7K
Snippets
6
Records
10
Agent score
96%

What's inside GFPGAN

  1. Compare GFPGAN Model Versions

    master

    GFPGAN provides different model versions depending on your needs for naturalness vs. sharpness:

    VersionModel NameDescription
    V1.3GFPGANv1.3.pthMore natural restoration results. Better for very low-quality or high-quality inputs. Can have repeated restorations. Weakness: Not very sharp, slight identity change.
    V1.2GFPGANCleanv1-NoCE-C2.pthNo colorization; no CUDA extensions required. Sharper output with beauty makeup. Weakness: Some outputs can be unnatural.
    V1GFPGANv1.pthThe original paper model, includes colorization.

    Note: V1.3 is not always better than V1.2; choose based on whether you prioritize naturalness or sharpness.

  2. Compare GFP-GAN model versions (V1.2 vs V1.3)

    master

    When choosing between GFP-GAN model versions, consider your specific input quality and desired output characteristics. V1.3 is not universally better than V1.2; the optimal choice depends on your use case.

    V1.3

    Best for:

    • Very low-quality inputs.
    • Relatively high-quality inputs.
    • Achieving natural-looking outputs.

    Trade-offs:

    • Outputs may not be as sharp.
    • May cause a slight change in facial identity.
    • Can occasionally result in repeated (twice) restorations.

    V1.2

    Best for:

    • Producing sharper outputs.
    • Applying a 'beauty makeup' effect.

    Trade-offs:

    • Some outputs may appear unnatural.
  3. Run Quick Inference with GFPGAN

    master

    To perform inference, first download the pre-trained model GFPGANv1.pth into the experiments/pretrained_models directory.

    Download Model

    wget https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/GFPGANv1.pth -P experiments/pretrained_models

    Run Inference

    Depending on your installation method, use the following commands:

    If using JIT installation: You must prefix your command with BASICSR_JIT=True.

    BASICSR_JIT=True python inference_gfpgan.py --input inputs/whole_imgs --output results --version 1

    If you compiled extensions during installation:

    python inference_gfpgan.py --input inputs/whole_imgs --output results --version 1

    For Aligned Images: Add the --aligned flag to either method if your input images are already aligned.

    # Example for JIT
    BASICSR_JIT=True python inference_gfpgan.py --input inputs/whole_imgs --output results --version 1 --aligned
    wget https://github.com/TencentARC/GFPGAN/releases/download/v0.1.0/GFPGANv1.pth -P experiments/pretrained_models
    
    # JIT Inference
    BASICSR_JIT=True python inference_gfpgan.py --input inputs/whole_imgs --output results --version 1
    
    # Compiled Inference
    python inference_gfpgan.py --input inputs/whole_imgs --output results --version 1
  4. Download and organize pre-trained models

    master

    To use GFP-GAN, you must download the required pre-trained models and place them in the experiments/pretrained_models/ directory. The following files are necessary for the model to function:

    • StyleGAN2 model: StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth
    • FFHQ component locations: FFHQ_eye_mouth_landmarks_512.pth
    • ArcFace model: arcface_resnet18.pth
    # Example directory structure
    gfpgan/
    └── experiments/
        └── pretrained_models/
            ├── StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth
            ├── FFHQ_eye_mouth_landmarks_512.pth
            └── arcface_resnet18.pth
  5. Train GFPGAN

    master

    To train GFPGAN, follow these procedures:

    1. Dataset Preparation: Use the FFHQ dataset.
    2. Download Pre-trained Data: Place the following in experiments/pretrained_models:
      • StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth
      • FFHQ_eye_mouth_landmarks_512.pth
      • arcface_resnet18.pth
    3. Configure: Modify options/train_gfpgan_v1.yml.
    4. Run Training: Use torch.distributed.launch to start training.

    Tip: Using higher quality faces and performing pre-processing (like beauty makeup) can improve restoration quality.

    python -m torch.distributed.launch --nproc_per_node=4 --master_port=22021 gfpgan/train.py -opt options/train_gfpgan_v1.yml --launcher pytorch
  6. Install the original GFPGAN model (Paper Version)

    master

    To use the original model described in the paper, you must compile or load customized PyTorch C++ extensions (StyleGAN2). There are two installation methods depending on whether you want to compile extensions upfront or load them just-in-time (JIT).

    Prerequisites

    1. Clone the repository:
    git clone https://github.com/xinntao/GFPGAN.git
    cd GFPGAN

    Option 1: Just-In-Time (JIT) Loading

    Best for simple inference tasks with fewer installation issues. You must set the BASICSR_JIT=True environment variable before running your commands.

    pip install basicsr
    pip install facexlib
    pip install -r requirements.txt
    python setup.py develop

    Option 2: Compile Extensions During Installation

    Recommended for frequent training or inference. This method compiles CUDA extensions in BasicSR during the pip installation process and may take several minutes.

    BASICSR_EXT=True pip install basicsr -vvv
    pip install facexlib
    pip install -r requirements.txt
    python setup.py develop
    # Option 1: JIT
    pip install basicsr
    pip install facexlib
    pip install -r requirements.txt
    python setup.py develop
    
    # Option 2: Compile during install
    BASICSR_EXT=True pip install basicsr -vvv
    pip install facexlib
    pip install -r requirements.txt
    python setup.py develop
  7. Install GFPGAN

    master

    To install the clean version of GFPGAN (which does not require customized CUDA extensions and can run on Windows or CPU), follow these steps:

    1. Clone the repository:

      git clone https://github.com/TencentARC/GFPGAN.git
      cd GFPGAN
    2. Install dependencies: You will need Python >= 3.7 and PyTorch >= 1.7. Use the following commands to install the required packages:

      pip install basicsr
      pip install facexlib
      pip install -r requirements.txt
      python setup.py develop
    3. Optional: Background Enhancement: If you want to enhance non-face regions (the background) using Real-ESRGAN, install the realesrgan package:

      pip install realesrgan
    git clone https://github.com/TencentARC/GFPGAN.git
    cd GFPGAN
    
    # Install dependencies
    pip install basicsr
    pip install facexlib
    pip install -r requirements.txt
    python setup.py develop
    
    # Optional: for background enhancement
    pip install realesrgan
  8. Reference: inference_gfpgan.py CLI flags

    master

    The inference_gfpgan.py script accepts the following command-line arguments:

    FlagDescription
    -hShow this help
    -i inputInput image or folder. Default: inputs/whole_imgs
    -o outputOutput folder. Default: results
    -v versionGFPGAN model version. Options: 1
    -s upscaleThe final upsampling scale of the image. Default: 2
    -bg_upsamplerBackground upsampler. Default: realesrgan
    -bg_tileTile size for background sampler, 0 for no tile during testing. Default: 400
    -suffixSuffix of the restored faces
    -only_center_faceOnly restore the center face
    -alignedInput are aligned faces
    -extImage extension. Options: auto
    Usage: python inference_gfpgan.py -i inputs/whole_imgs -o results -v 1.3 -s 2 [options]...
    
      -h                   show this help
      -i input             Input image or folder. Default: inputs/whole_imgs
      -o output            Output folder. Default: results
      -v version           GFPGAN model version. Option: 1 | 1.2 | 1.3. Default: 1.3
      -s upscale           The final upsampling scale of the image. Default: 2
      -bg_upsampler        background upsampler. Default: realesrgan
      -bg_tile             Tile size for background sampler, 0 for no tile during testing. Default: 400
      -suffix              Suffix of the restored faces
      -only_center_face    Only restore the center face
      -aligned             Input are aligned faces
      -ext                 Image extension. Options: auto | jpg | png, auto means using the same extension as inputs. Default: auto
  9. Inference CLI Arguments

    master

    The inference_gfpgan.py script accepts the following arguments for controlling the restoration process:

    • --input: Path to the input images (e.g., inputs/whole_imgs).
    • --output: Path where the restored images will be saved (e.g., results).
    • --version: Specifies the model version (e.g., 1).
    • --aligned: A flag used when the input images are already face-aligned.