Coverage Ratio in Model Training: How It Shapes Faceswap Output Quality
The coverage ratio determines what percentage of each extracted face is cropped and fed to the neural network during training, directly controlling the trade-off between fine facial detail and contextual smoothness in the final swapped output.
The coverage ratio is a critical hyper-parameter in the deepfakes/faceswap training pipeline that defines how much of each aligned face image the model learns to reconstruct. Configured in the training settings and applied throughout the data generation pipeline, this percentage value (0–100%) fundamentally alters the effective input resolution and the amount of surrounding context the network receives. Understanding this parameter is essential for optimizing model performance and achieving seamless face swaps.
What Is the Coverage Ratio in Model Training?
Configuration and User Interface
In plugins/train/train_config.py (lines 63-80), the coverage ratio is exposed to users as a ConfigItem named coverage. This setting accepts integer values from 0 to 100, with a default of 100%. The configuration description explains the visual effects of different percentages across the three centering modes: face, head, and legacy.
Technical Definition in the Codebase
The raw value is converted to a float for internal calculations in plugins/train/model/_base/model.py (lines 106-117). The ModelBase.coverage_ratio property returns the user-selected value divided by 100:
@property
def coverage_ratio(self):
"""float: The coverage ratio as a decimal (e.g., 0.75 for 75%)."""
return self.config.coverage() / 100.
This float (ranging from 0.0 to 1.0) propagates through the entire training pipeline, determining the dimensions of training patches fed to the neural network.
How the Coverage Ratio Affects Training Data
The Cropping Formula
When generating training data, the framework applies the coverage ratio to crop each aligned face image. The formula, documented in the codebase, ensures the resulting dimensions remain even (required for neural network processing):
cropped_size = (original_size * coverage_ratio // 2) * 2
This calculation guarantees that the cropped region maintains square proportions while accommodating the specific percentage of the original aligned image requested by the user.
Interaction with Centering Modes
The effective visual area changes depending on the selected centering mode:
centering=face: Typically requires coverage > 75% to retain cheek-to-cheek informationcentering=head: Usually set to 100% to include the full head including haircentering=legacy: Follows specific recommendations—62.5% for eyebrow-to-eyebrow, 75% for temple-to-temple
In lib/training/generator.py (lines 68-71), the DataGenerator class reads model.coverage_ratio and passes it to the face cache initialization, ensuring all training images are cropped consistently before augmentation.
Impact on Model Output Quality
Detail vs. Context Trade-off
Adjusting the coverage ratio creates a direct trade-off between two competing qualities:
- Higher Detail (lower coverage, e.g., 62.5%): Smaller patches concentrate pixels on core facial features (eyes, nose, mouth), potentially yielding sharper textures within the swapped region. However, this may produce hard edges where the swapped face meets the original image background.
- Greater Context (higher coverage, e.g., 100%): Larger patches include surrounding hair, ears, and neck regions, providing the network with boundary information that reduces visible seams during conversion, though potentially sacrificing some fine-grained facial detail.
Output Resolution and Scaling
The model always trains on square patches sized according to process_size (the maximum dimension among all model inputs/outputs). However, the effective output resolution scales with the coverage ratio:
effective_output_size = process_size * coverage_ratio
During the conversion phase, Faceswap uses the stored coverage_ratio to restore the original image dimensions, ensuring the swapped face aligns correctly with the target video frame.
Implementing Coverage Ratio in Code
Accessing the Property from ModelBase
To inspect the coverage ratio of a trained model programmatically:
from plugins.train.model._base import ModelBase
from pathlib import Path
from argparse import Namespace
model = ModelBase(
model_dir=Path("models/my_model"),
arguments=Namespace(configfile="config/train.cfg"),
predict=True,
)
print(f"Coverage ratio used during training: {model.coverage_ratio:.2f}") # e.g., 0.75
This accesses the coverage_ratio property defined in plugins/train/model/_base/model.py, which returns the configuration value normalized to a 0.0–1.0 range.
Calculating Crop Dimensions
To manually crop an aligned face using the same logic as the training pipeline:
import numpy as np
def crop_to_coverage(image, coverage_ratio):
"""
Crop a square aligned face to the specified coverage ratio.
Args:
image: numpy array of shape (size, size, channels)
coverage_ratio: float between 0.0 and 1.0
"""
size = image.shape[0]
# Ensure even dimensions after scaling (matches faceswap implementation)
cropped = int((size * coverage_ratio // 2) * 2)
offset = (size - cropped) // 2
return image[offset:offset+cropped, offset:offset+cropped]
# Example: Extract 75% coverage from a 512x512 aligned face
cropped_face = crop_to_coverage(aligned_image, 0.75)
This mirrors the internal calculation used by the framework to prepare training batches.
DataGenerator Integration
The training generator automatically handles coverage ratio application when loading batches:
from lib.training.generator import DataGenerator
generator = DataGenerator(
model=model,
side="a",
images=["/path/to/face1.jpg", "/path/to/face2.jpg"],
batch_size=8,
)
# Internally initializes cache with:
# self._face_cache = get_cache(side, filenames=images,
# size=self._process_size,
# coverage_ratio=self._coverage_ratio)
As implemented in lib/training/generator.py, the constructor extracts coverage_ratio from the model instance and passes it to the caching layer, which performs the actual cropping operations during data loading.
Summary
- The coverage ratio is a float value (0.0–1.0) derived from the user-configured percentage in
train_config.py, defaulting to 1.0 (100%). - It determines the cropped training patch size via the formula
(original_size * coverage_ratio // 2) * 2, ensuring even pixel dimensions. - Lower ratios increase pixel density on facial features but risk hard edges; higher ratios include contextual boundaries (hair, neck) for seamless blending.
- The parameter interacts with centering modes (face/head/legacy) to define specific visual regions (eyebrow-to-eyebrow at 62.5%, full head at 100%).
- The value persists through training and is reused during conversion to scale the output back to the target frame resolution.
Frequently Asked Questions
What is the default coverage ratio in faceswap?
The default coverage ratio is 100% (1.0 as a float), defined in plugins/train/train_config.py. This setting uses the entire aligned face image for training, which is generally recommended for centering=head mode where the full head including hair must be reconstructed.
How does coverage ratio interact with centering modes?
Each centering mode requires specific coverage ratios for optimal results. For centering=legacy, 62.5% captures eyebrow-to-eyebrow, while 75% captures temple-to-temple. For centering=face, values above 75% preserve cheek-to-cheek information. For centering=head, 100% is typically required to include the full cranium and hairline.
Why does my swapped face show hard edges against the background?
Hard edges usually indicate a low coverage ratio (e.g., 62.5% or 75%) combined with inadequate blending. When the model trains on tight facial crops without sufficient surrounding context, it learns to reconstruct the face interior but receives limited information about the transition zones (jawline, hairline). Increasing coverage to 87.5% or 100% provides the network with boundary context necessary for seamless integration.
Can I change the coverage ratio after training has started?
No, changing the coverage ratio mid-training is not supported and would break model compatibility. The ratio determines the input dimensions and the specific pixel regions the network learns to map. Altering this value would change the tensor shapes and the semantic meaning of the training data, requiring you to restart training from scratch with the new configuration.
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:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →