Need help with PSFRGAN?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

265 Stars 53 Forks Other 33 Commits 1 Opened issues


PyTorch codes for "Progressive Semantic-Aware Style Transformation for Blind Face Restoration", CVPR2021

Services available


Need anything else?

Contributors list

# 179,364
1 commit

PSFR-GAN in PyTorch

Progressive Semantic-Aware Style Transformation for Blind Face Restoration
Chaofeng Chen, Xiaoming Li, Lingbo Yang, Xianhui Lin, Lei Zhang, Kwan-Yee K. Wong


  • 2021.04.26: Add pytorch vgg19 model to GoogleDrive and remove
    option which causes training error.
  • 2021.03.22: Update new model at 15 epoch (52.5k iterations).
  • 2021.03.19: Add train codes for PSFRGAN and FPN.

Prerequisites and Installation

  • Ubuntu 18.04
  • CUDA 10.1
  • Clone this repository
    git clone
    cd PSFR-GAN
  • Python 3.7, install required packages by
    pip3 install -r requirements.txt

Quick Test

Download Pretrain Models and Dataset

Download the pretrained models from the following link and put them to


- GoogleDrive - BaiduNetDisk, extract code:

Test single image

Run the following script to enhance face(s) in single input

python --test_img_path ./test_dir/test_hzgg.jpg --results_dir test_hzgg_results --gpus 1

This script do the following things: - Crop and align all the faces from input image, stored at


- Parse these faces and then enhance them, results stored at

- Paste then enhanced faces back to the original image

- You can use
to specify how many GPUs to use,
means running on CPU. The program will use GPU with the most available memory. Set
to specify the GPU if you do not want automatic GPU selection.

Test image folder

To test multiple images, we first crop out all the faces and align them use the following script.

python --src_dir test_dir --results_dir test_dir_align_results

For images (e.g.

) contain multiple faces, the aligned faces will be stored as

And then parse the aligned faces and enhance them with
python --src_dir test_dir_align_results --results_dir test_dir_enhance_results

Results will be saved to three folders respectively:

Additional test script

For your convenience, we also provide script to test multiple unaligned images and paste the enhance results back. Note the paste back operation could be quite slow for large size images containing many faces (dlib takes time to detect faces in large image).

python --src_dir test_dir --results_dir test_unalign_results

This script basically do the same thing as
for each image in

Train the Model

Data Preparation

  • Download FFHQ and put the images to
  • Download parsing masks (
    ) HERE generated by the pretrained FPN and put them to

Note: you may change

to your own path. But images and masks must be stored under

Train Script for PSFRGAN

Here is an example train script for PSFRGAN:

python --gpus 2 --model enhance --name PSFRGAN_v001 \
    --g_lr 0.0001 --d_lr 0.0004 --beta1 0.5 \
    --gan_mode 'hinge' --lambda_pix 10 --lambda_fm 10 --lambda_ss 1000 \
    --Dinput_nc 22 --D_num 3 --n_layers_D 4 \
    --batch_size 2 --dataset ffhq  --dataroot ../datasets/FFHQ \
    --visual_freq 100 --print_freq 10 #--continue_train
  • Please change the
    option for different experiments. Tensorboard records with the same name will be moved to
    , and the weight directory will only store weight history of latest experiment with the same name.
  • --gpus
    specify number of GPUs used to train. The script will use GPUs with more available memory first. To specify the GPU index, use
    export CUDA_VISIBLE_DEVICES=your_gpu_ids
    before the script.
  • Uncomment
    to resume training. Current codes do not resume the optimizer state.
  • It needs at least 8GB memory to train with batch_size=1.

Scripts for FPN

You may also train your own FPN and generate masks for the HQ images by yourself with the following steps:

  • Download CelebAHQ-Mask dataset. Generate
    with the provided scripts in
  • Train FPN with the following commmand
    python --gpus 1 --model parse --name FPN_v001 \
    --lr 0.0002 --batch_size 8 \
    --dataset celebahqmask --dataroot ../datasets/CelebAMask-HQ \
    --visual_freq 100 --print_freq 10 #--continue_train
  • Generate parsing masks with your own FPN using the following command:
    python --save_masks_dir ../datasets/FFHQ/masks512 --batch_size 8 --parse_net_weight path/to/your/own/FPN 


    author = {Chen, Chaofeng and Li, Xiaoming and Lingbo, Yang and Lin, Xianhui and Zhang, Lei and Wong, KKY},
    title = {Progressive Semantic-Aware Style Transformation for Blind Face Restoration},
    Journal = {IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
    year = {2021}


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


This work is inspired by SPADE, and closed related to DFDNet and HiFaceGAN. Our codes largely benefit from CycleGAN.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.