deepfakes/faceswap · error · FaceswapError

'{name}' is not a valid Loss function. Choose from: {list(va

Error message

'{name}' is not a valid Loss function. Choose from: {list(valid)}

What it means

FaceswapError raised by the loss-function factory in lib/model/losses/__init__.py when the requested loss name is not in the valid mapping (mae, mse, ssim, ms_ssim, lpips_*, flip, logcosh, laploss, pixel_gradient_diff, smooth_loss, etc.). It is a configuration-validation error raised at model build time.

Source

Thrown at lib/model/losses/__init__.py:46

    The requested Torch Loss function
    """
    valid = {"ffl": FocalFrequencyLoss,
             "flip": LDRFLIPLoss,
             "gmsd": GMSDLoss,
             "l_inf_norm": LInfNorm,
             "laploss": LaplacianPyramidLoss,
             "logcosh": LogCosh,
             "lpips_alex": LPIPSLoss,
             "lpips_squeeze": LPIPSLoss,
             "lpips_vgg16": LPIPSLoss,
             "ms_ssim": MSSIMLoss,
             "mae": nn.L1Loss,
             "mse": nn.MSELoss,
             "pixel_gradient_diff": GradientLoss,
             "ssim": SSIMLoss,
             "smooth_loss": GeneralizedLoss}
    if name not in valid:
        raise FaceswapError(f"'{name}' is not a valid Loss function. Choose from: {list(valid)}")

    kwargs: dict[str, T.Any] = {}
    if name in ("mae", "mse"):
        kwargs["reduction"] = "none"
    if name == "flip" or name.startswith("lpips_"):
        kwargs["color_order"] = color_order
    if name.startswith("lpips_"):
        kwargs["trunk_network"] = name.rsplit("_", maxsplit=1)[-1]
        kwargs["crop"] = True
    return valid[name](**kwargs)

View on GitHub (pinned to f530cb7508)

Solutions

  1. Use one of the names listed in the error message exactly (lowercase, underscores).
  2. Check the installed version's valid mapping if unsure — the error prints the full list.
  3. Update stale config files after upgrading faceswap.

Example fix

# model config
# before
loss_function = ms-ssim
# after
loss_function = ms_ssim
Defensive patterns

Strategy: validation

Validate before calling

VALID_LOSSES = {'mae','mse','ssim','ms_ssim','lpips_alex','lpips_squeeze',
                'lpips_vgg16','flip','logcosh','laploss','pixel_gradient_diff',
                'smooth_loss'}
assert config.loss_function in VALID_LOSSES, f'pick from {sorted(VALID_LOSSES)}'

Type guard

def is_valid_loss(name: str) -> bool:
    return isinstance(name, str) and name.lower() in VALID_LOSSES

Try / catch

try:
    loss_func = LossFactory(config.loss_function)
except FaceswapError as err:
    if 'not a valid Loss function' in str(err):
        config.loss_function = 'mse'  # safe default
        loss_func = LossFactory(config.loss_function)
    else:
        raise

Prevention

When it happens

Trigger: Typoing a loss name in train configuration (e.g. 'ms-ssim' instead of 'ms_ssim', 'l1' instead of 'mae'); using a loss name from an older/newer faceswap version that no longer exists; case sensitivity ('MSE' vs 'mse').

Common situations: Hand-editing the model config file or CLI overrides with an invalid loss; copying configs from tutorials for a different faceswap version.

Related errors


AI-assisted analysis of deepfakes/faceswap@f530cb7508 (2026-08-15). Data as JSON: /api/errors/47073226a3228d74. Report an issue: GitHub.