Normalization Methods for Face Extraction in Faceswap: A Complete Guide

Faceswap provides four built-in normalization methods—none, clahe, hist, and mean—that you can apply during face extraction via the --normalize CLI flag to control image preprocessing before alignment.

When working with the deepfakes/faceswap repository, understanding the available normalization methods for face extraction is essential for optimizing your training data. These preprocessing techniques determine how raw face images are adjusted before they reach the aligner, directly impacting contrast, brightness, and overall image quality for downstream deepfake generation.

Available Normalization Methods for Face Extraction

The extraction pipeline in plugins/extract/pipeline.py defines the normalize_method argument with four valid options: None (or none), clahe, hist, and mean. Each method serves specific preprocessing needs depending on your source material lighting conditions.

None (Disabled)

Setting normalize_method to none disables all normalization, passing the raw extracted face directly to the aligner without modification. This preserves the original pixel values and color distribution from your source video or images.

Use this option when your training data already has consistent lighting and contrast, or when you want to handle normalization manually in your training pipeline rather than during extraction.

CLAHE (Contrast Limited Adaptive Histogram Equalization)

The clahe method applies Contrast Limited Adaptive Histogram Equalization, which improves local contrast by computing histograms over small tiles of the image rather than the entire global distribution. This technique prevents over-amplification of noise while enhancing details in both bright and dark regions.

CLAHE is particularly effective for faces with uneven lighting, such as harsh shadows or backlighting, as it normalizes contrast locally without washing out important facial features.

Histogram Equalization (Hist)

Selecting hist performs global histogram equalization across the entire face image. This method redistributes pixel intensity values to span the full available range (typically 0-255), improving overall contrast by flattening the intensity histogram.

While effective for low-contrast images, global histogram equalization can sometimes over-saturate bright areas or amplify background noise, making it less suitable than CLAHE for variable lighting conditions.

Mean Normalization

The mean method normalizes the face by subtracting the mean pixel value from each channel, performing mean-centering on the image data. This shifts the pixel distribution so that the average intensity becomes zero (or 128 for unsigned 8-bit images), removing brightness bias while preserving the relative contrast structure.

Mean normalization is commonly used in machine learning preprocessing pipelines to ensure input data has zero mean, which can help with gradient flow during neural network training.

How to Configure Normalization in the Extraction Pipeline

You control normalization methods for face extraction through the command-line interface defined in scripts/extract.py. The --normalize (or -n) flag accepts the method name and propagates it through the extraction pipeline to the aligner plugin.

Command-Line Usage Examples


# Disable normalization (default behavior)

faceswap extract -i input_folder -o output_folder --aligner=centroid

# Apply CLAHE normalization

faceswap extract -i input_folder -o output_folder --aligner=centroid -n clahe

# Use histogram equalization

faceswap extract -i input_folder -o output_folder --aligner=centroid -n hist

# Apply mean normalization

faceswap extract -i input_folder -o output_folder --aligner=centroid -n mean

Pipeline Integration

In plugins/extract/pipeline.py, the normalize_method parameter is defined with type validation ensuring only valid options pass through:

normalize_method: {None, 'clahe', 'hist', 'mean'}

The pipeline instantiates aligner plugins and passes the selected method via the set_normalize_method setter defined in the aligner base class.

Technical Implementation Details

The normalization logic resides primarily in plugins/extract/align/_base/aligner.py, where the base aligner class manages method validation and storage.

Method Validation and Storage

The aligner base class defines the _normalize_method attribute with strict type hints:

self._normalize_method: T.Literal["clahe", "hist", "mean"] | None = None

The set_normalize_method setter accepts the string value and validates it against the allowed literals before assignment. This ensures that only none, clahe, hist, or mean (plus None) can propagate through the system, preventing invalid preprocessing configurations from reaching the extraction workers.

Application During Extraction

Once validated, the normalization method is applied to face images during the alignment phase, before the aligner processes facial landmarks. This preprocessing step ensures consistent input characteristics regardless of source lighting conditions, standardizing the data that feeds into the landmark detection models.

Summary

  • Four normalization methods are available in Faceswap: none (disabled), clahe (adaptive histogram equalization), hist (global histogram equalization), and mean (mean-centering).
  • Configuration occurs via the --normalize or -n CLI flag in scripts/extract.py, with validation in plugins/extract/pipeline.py.
  • Implementation resides in plugins/extract/align/_base/aligner.py, where the set_normalize_method setter validates and stores the chosen technique.
  • Selection guidance: Use clahe for uneven lighting, hist for low global contrast, mean for zero-centering requirements, and none when preserving original pixel values is critical.

Frequently Asked Questions

What is the default normalization method in Faceswap extraction?

The default normalization method is none, meaning no preprocessing is applied to extracted faces unless explicitly specified. When you run the extraction command without the -n or --normalize flag, the pipeline passes raw face images directly to the aligner without contrast adjustment or mean-centering.

Which normalization method should I use for training deepfake models?

For most training scenarios, clahe provides the best balance because it enhances local contrast without amplifying noise, handling varied lighting conditions common in source footage. If your training data has consistent studio lighting, mean normalization helps with gradient flow during neural network training by centering pixel values around zero. Avoid hist if your source material contains bright backgrounds, as global equalization can wash out facial details.

Can I change the normalization method after extraction is complete?

No, normalization is applied during the extraction phase before alignment and cannot be modified on already-extracted faces without re-running the extraction process. The normalization setting is baked into the aligned face images stored in your output folder. To apply a different method, you must delete or backup the existing extracted faces and re-run faceswap extract with the desired -n parameter.

Where in the codebase is the normalization actually applied to the image?

The normalization method is validated and stored in plugins/extract/align/_base/aligner.py within the set_normalize_method property setter, which enforces the type constraint T.Literal["clahe", "hist", "mean"] | None. The actual pixel-level processing occurs during the alignment phase when the aligner plugin applies the selected technique to face images before landmark detection, though the specific image processing implementation depends on the individual aligner plugin being used (such as cv2 operations for CLAHE or histogram equalization).

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →