CodeFormer restores faces on damaged photos with Codebook Lookup Transformer

NeurIPS 2022 project from NTU S-Lab balances restoration quality against identity preservation and adds colorization, inpainting, and background upscaling via Real-ESRGAN.

cpaua3 min282
CodeFormer restores faces on damaged photos with Codebook Lookup Transformer

CodeFormer, a neural-network tool for restoring and enhancing faces in old or damaged photographs, was presented at NeurIPS 2022 by researchers from the S-Lab at Nanyang Technological University. The system uses a Codebook Lookup Transformer architecture that lets users trade off restoration fidelity against identity preservation through a single weight parameter.

Codebook Lookup Transformer architecture balances quality and identity

The core innovation is a discrete codebook learned in a self-supervised manner. During inference the transformer looks up the closest codebook entries for degraded facial regions, then decodes them into restored details. A fidelity weight w in the range [0, 1] controls the balance: smaller values produce higher perceptual quality, larger values keep the result closer to the input identity.

Restoration, colorization, inpainting, and background upscaling

Beyond face restoration the repository ships dedicated checkpoints and scripts for colorizing black-and-white or faded portraits and for inpainting masked regions such as scratches or occlusions. An optional Real-ESRGAN backend can upscale background areas and, if requested, the restored face itself, enabling whole-image and video enhancement in a single pass.

Installation requires PyTorch, CUDA, and pretrained model downloads

The codebase targets Python with PyTorch ≥ 1.7.1 and CUDA ≥ 10.1. After cloning the repository, users create a conda environment, install dependencies from requirements.txt, and run python basicsr/setup.py develop. Pretrained weights for facelib, dlib (optional face detector), and CodeFormer itself are fetched via python scripts/download_pretrained_models.py and placed under weights/.

Inference modes for cropped faces, whole images, and video

Separate entry points handle each task. Cropped and aligned faces (512 × 512) are processed with inference_codeformer.py --has_aligned for restoration, inference_colorization.py for colorization, and inference_inpainting.py for inpainting. Whole-image enhancement adds --bg_upsampler realesrgan and optional --face_upsample. Video clips ending in .mp4, .mov, or .avi are accepted directly when ffmpeg is available.

Official demos on Hugging Face, Replicate, and OpenXLab

The authors maintain three official online demos: a Hugging Face Space, a Replicate model, and an OpenXLab application. All other hosted instances listed in the repository are third-party deployments not affiliated with the CodeFormer team.

Integrations with Stable Diffusion WebUI, ComfyUI, and ChaiNNer

CodeFormer has been embedded into popular generative-art toolchains. It appears as a post-processing option in Automatic1111's Stable Diffusion WebUI, as a node in ComfyUI, and as a module in the node-based image-processing framework ChaiNNer. Python packages are also published on PyPI under the names codeformer and codeformer-pip.

NTU S-Lab License 1.0 governs redistribution

The project is released under the NTU S-Lab License 1.0. Redistribution and commercial use must comply with its terms, which are included in the repository's LICENSE file.

Related reading

Sources

sczhou/CodeFormergithub.com/sczhou/CodeFormer
Share

Author

cpaua

VibeCode blog admin. Writing about vibe coding, AI and open source.

Related news

Comments

To leave a comment, log in
Loading...

Weekly digest

The best of vibe coding, AI agents and open source — once a week, no spam.