CodeFormer (fastforks)

CodeFormer (Zhou et al., NeurIPS 2022): blind face restoration with a learned codebook: a VQGAN encoder, a transformer that predicts the code of each of the 16 x 16 latent positions, and the VQGAN decoder with controllable feature fusion from the encoder (fidelity w). Input: an aligned 512 x 512 face crop (FFHQ alignment); detecting, aligning and pasting the face back are not part of the model.

This repo is the model card only: it holds no weights. fastforks downloads the authors' weights from the links below and checks them against the SHA-256 in the model card. Code: https://github.com/kleinicke/fastforks. Kernels for the kernels backend: https://huggingface.co/fastforks/kernels. Original code: https://github.com/sczhou/CodeFormer at b33cc7d639d6.

Task: restoration. Tier: experimental. Outputs: image. Backends: portable, kernels.

Input: one RGB image. Output: the restored (upscaled) image, model.scale x the input size, RGB in [0, 1]. tile= runs large images in overlapping tiles (as ComfyUI).

Variants

Weights are the authors' files (not hosted here). SHA-256 shortened to 12 hex digits; the full hashes are in card.toml.

variant weights (author link) SHA-256 weights licence commercial use
codeformer@1 codeformer.pth 1009e537e0c2 LicenseRef-noncommercial no

Licence

Original-code strings, recorded as they are (research-use package; check them before any other use). Code: LicenseRef-noncommercial. Licence files: LICENSE.

Training data: Flickr-Faces-HQ (FFHQ, 70k face images) (Paper Sec. 4.1: FFHQ (70 000 faces, resized to 512 x 512) with synthetic degradations (blur, downsampling, noise, JPEG).).

Licence evidence: https://github.com/sczhou/CodeFormer/blob/b33cc7d639d6545bfcccc7e0bc6ae51f24e79c2b/LICENSE ; https://github.com/sczhou/CodeFormer/blob/b33cc7d639d6545bfcccc7e0bc6ae51f24e79c2b/README.md (weights: https://github.com/sczhou/CodeFormer/releases/tag/v0.1.0)

Input size

any H x W >= 512, padded to a multiple of 32 Tested up to (portable, fp16, RTX 5060 Ti 16 GB): codeformer 512x512 (0.5 GiB peak allocated, 0.7 reserved)

Speed and memory

Restoration, synthetic, 512x512

model time (ms): original portable kernels kernels speed-up: vs original vs portable peak memory (MiB, allocated / reserved): original portable kernels
codeformer 85.7 48.6 40.4 2.1x 1.2x 959 / 1090 512 / 638 432 / 496

Startup and first call

model load (s): original portable kernels first call (s): original portable kernels
codeformer 1.24 0.69 0.65 0.41 0.23 0.23

torch.compile and TensorRT

model input time: portable compile TensorRT fp16 kernels preparation: compile (cold first call) ONNX + engine build peak MiB (alloc / reserved): portable compile kernels TensorRT (engine + torch)
codeformer 512x512 48.6 ms 30.8 ms 16.3 ms 40.4 ms 34.1 s 64.8 s 512 / 638 407 / 458 432 / 496 593 / 642

Fixed-size cells: time (ms) / peak allocated / peak reserved memory (MiB).

Hardware, protocol and caveats: benchmarks (RTX 5060 Ti 16 GB, sm_120). Original = the original code named above at the precision of portable (fp16, or (fp32) where fp16 is unsupported or not finite); original fp32 = its default fp32 run. Peak memory is the torch allocator's.

Accuracy

Parity with the original code

Every variant is checked against reference outputs of the original code at its pinned commit. CPU, float32: max |diff| <= 0.002 vs the original. GPU, float16 (the default), portable and kernels: mean |diff| <= 0.02 and p99 <= 0.25 (absolute error of the output image).

Usage

from fastforks import restoration

# non-commercial weights: run with FASTFORKS_NONCOMMERCIAL=1 in the environment
model = restoration.load("codeformer")  # mode="auto": the preferred backend from recorded measurements
result = model("photo.png")  # tile=512 for large images
result.image  # [3, sH, sW] float32 RGB in [0, 1], s = model.scale
result.backend  # what ran, e.g. Backend(mode='portable', device='cuda:0', dtype='float16')

Install with pip install fastforks (see https://github.com/kleinicke/fastforks). Other variants: codeformer.

Kernels backend

restoration.load("codeformer", mode="kernels") runs our CUDA kernels (igev, cutlass_conv, cudnn_hacc). It needs a prebuilt kernel for your GPU architecture (downloaded from https://huggingface.co/fastforks/kernels on first use) and a recent NVIDIA driver; if there is none it raises BackendUnavailableError with the reason and never falls back silently. mode="auto" picks kernels when it can run and logs the choice.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Collection including fastforks/codeformer