Noureddine RAMDI / CodeFormer: Deep learning-based blind face restoration with fidelity control

Created Tue, 05 May 2026 13:37:39 +0000 Modified Sat, 23 May 2026 20:41:27 +0000

sczhou/CodeFormer

CodeFormer tackles one of the persistent challenges in face restoration: how to reconstruct high-quality faces from degraded images without relying on reference images. What makes it particularly interesting is its fidelity weight parameter that lets you dial between pure restoration quality and faithfulness to the original input — a practical control for the tradeoff that often plagues generative restoration models.

What CodeFormer does and how it works

CodeFormer is a deep learning model introduced at NeurIPS 2022 for blind face restoration. “Blind” here means it does not assume any prior knowledge about the degradation type or severity; it works purely from the input image. The core of the architecture is a Codebook Lookup Transformer that uses a learned codebook of facial priors to reconstruct faces.

The repo provides a full pipeline going beyond just the model: it includes face detection and alignment, restoration, colorization, and inpainting. For enhancing the background and non-face regions, it optionally integrates Real-ESRGAN, a super-resolution model.

Under the hood, the model is implemented in PyTorch and builds on the BasicSR framework. It supports both image and video inputs, making it versatile for real-world use cases. The codebase is well-structured, with a clear separation between the face restoration core and auxiliary tasks like detection and background upsampling.

The fidelity weight: balancing restoration quality and identity preservation

The standout feature of CodeFormer is its fidelity weight parameter w which ranges from 0 to 1. This parameter controls the tradeoff between generating a high-quality restored face and preserving fidelity to the original degraded input.

  • At w=0, the model prioritizes restoration quality, often introducing plausible but hallucinated details to enhance the face.
  • At w=1, the model leans towards preserving the original face’s identity and avoids adding too many synthetic details.

This tradeoff is a real problem in generative face restoration models. Too much hallucination can produce visually pleasing but inaccurate faces, while too much fidelity results in under-restored, blurry outputs. CodeFormer exposes this balance as a tunable parameter, giving users control depending on their needs.

In practice, this means you can tune the output depending on whether you want a cleaner, sharper face for display purposes or a more faithful reconstruction for tasks like forensics or identity verification.

The code quality reflects this design clarity. The parameter is exposed cleanly in the inference scripts and the model architecture supports it naturally, avoiding hacks or post-processing tricks. This also makes CodeFormer easier to integrate into workflows where different fidelity levels are required.

Quick start with CodeFormer

The project provides detailed installation and inference instructions.

Dependencies and installation

  • PyTorch >= 1.7.1
  • CUDA >= 10.1
  • Other Python dependencies listed in requirements.txt
pip3 install -r requirements.txt
python basicsr/setup.py develop
conda install -c conda-forge dlib  # only needed for face detection or cropping with dlib

Download pretrained models

Download pretrained models for facelib and dlib (for face detection), as well as for CodeFormer itself. You can either download manually from the provided links or use these scripts:

python scripts/download_pretrained_models.py facelib
python scripts/download_pretrained_models.py dlib  # only for dlib face detector
python scripts/download_pretrained_models.py CodeFormer

Preparing test data

Put your test images into inputs/TestWhole or cropped/aligned faces into inputs/cropped_faces. To crop and align faces, run:

python scripts/crop_align_face.py -i [input folder] -o [output folder]

(Note: you may need to install dlib as shown above.)

Running inference

For the best comparison results, run with the --has_aligned flag if using cropped faces. The fidelity weight w can be adjusted between 0 and 1 to control the restoration vs. fidelity tradeoff. Results are saved in the results folder.

This straightforward CLI approach makes it easy to experiment with the balance between hallucination and preservation.

verdict

CodeFormer is a solid choice if you need a blind face restoration model with explicit control over the tradeoff between quality and fidelity. The fidelity weight parameter is a practical and well-implemented solution to a common pain point in generative restoration.

The repo is well-maintained, with clear code and a comprehensive pipeline covering detection, alignment, restoration, and background enhancement. Its dependency on PyTorch and BasicSR means it fits naturally into many computer vision workflows.

Limitations include the usual challenges of blind restoration: extreme degradations or unusual facial poses might still pose problems. The background enhancement depends on Real-ESRGAN, which adds complexity.

Overall, CodeFormer is worth exploring if you’re working on image or video face restoration, especially when you want fine-grained control over the fidelity-quality balance. The tradeoff parameter lets you tune results for different downstream use cases — a nice practical touch not often seen in this space.


→ GitHub Repo: sczhou/CodeFormer ⭐ 17,951 · Python