lllyasviel/Fooocus · error · ValueError
Wrong params!
Error message
Wrong params!
What it means
In codeformer's VQAutoEncoder.load_state_dict path, the vqgan checkpoint loaded with weights_only=True must contain either a 'params_ema' or 'params' top-level key. If neither key exists, the file is not a CodeFormer VQGAN weights file in the expected format and ValueError('Wrong params!') is raised.
Source
Thrown at ldm_patched/pfn/architecture/face/codeformer.py:392
)
self.generator = Generator(
nf, ch_mult, res_blocks, img_size, attn_resolutions, emb_dim
)
if model_path is not None:
chkpt = torch.load(model_path, map_location="cpu", weights_only=True)
if "params_ema" in chkpt:
self.load_state_dict(
torch.load(model_path, map_location="cpu", weights_only=True)["params_ema"]
)
logger.info(f"vqgan is loaded from: {model_path} [params_ema]")
elif "params" in chkpt:
self.load_state_dict(
torch.load(model_path, map_location="cpu", weights_only=True)["params"]
)
logger.info(f"vqgan is loaded from: {model_path} [params]")
else:
raise ValueError("Wrong params!")
def forward(self, x):
x = self.encoder(x)
quant, codebook_loss, quant_stats = self.quantize(x)
x = self.generator(quant)
return x, codebook_loss, quant_stats
def calc_mean_std(feat, eps=1e-5):
"""Calculate mean and std for adaptive_instance_normalization.
Args:
feat (Tensor): 4D tensor.
eps (float): A small value added to the variance to avoid
divide-by-zero. Default: 1e-5.
"""
size = feat.size()
assert len(size) == 4, "The input feature should be 4D tensor."
b, c = size[:2]View on GitHub (pinned to ae05379cc9)
Solutions
- Download the official CodeFormer VQGAN weights (vqgan_codeformer.pth) which contain 'params_ema'
- If your file is a raw state dict, re-wrap it: torch.save({'params': sd}, path)
- Verify with torch.load(..., weights_only=True) which top-level key exists before pointing CodeFormer at the file
Example fix
import torch
ckpt = torch.load(model_path, map_location='cpu', weights_only=True)
# before: neither 'params_ema' nor 'params' -> ValueError: Wrong params!
# after:
if 'params_ema' not in ckpt and 'params' not in ckpt:
torch.save({'params': ckpt}, model_path) # re-wrap raw state dict
ckpt = torch.load(model_path, map_location='cpu', weights_only=True) Defensive patterns
Strategy: validation
Validate before calling
import torch
def is_codeformer_vqgan(path) -> bool:
try:
ckpt = torch.load(path, map_location='cpu', weights_only=True)
except Exception:
return False
return isinstance(ckpt, dict) and ('params_ema' in ckpt or 'params' in ckpt)
if not is_codeformer_vqgan(vqgan_path):
raise SystemExit(f'{vqgan_path} is not a CodeFormer VQGAN file (needs params/params_ema key)') Type guard
def is_vqgan_ckpt(obj) -> bool:
return isinstance(obj, dict) and ('params_ema' in obj or 'params' in obj) Try / catch
try:
restorer = CodeFormer(vqgan_path, codeformer_path, ...)
except ValueError as e:
if str(e) == 'Wrong params!':
raise ModelFileError(f'{vqgan_path}: expected keys params/params_ema missing - wrong or corrupt CodeFormer VQGAN file') from e
raise Prevention
- Download CodeFormer VQGAN weights only from the official release (vqgan_codeformer.pth)
- Do not swap codeformer.pth and vqgan_codeformer.pth paths
- Pre-check the top-level key ('params_ema' or 'params') before loading
When it happens
Trigger: Calling the CodeFormer face-restoration constructor with fidelity_ckpt/vqgan model_path pointing at the wrong artifact: the codeformer prompting/decoder weights instead of the VQGAN, a raw state dict, or a truncated download. torch.load succeeds, but the dict lacks both keys.
Common situations: Mixing up codeformer.pth and vqgan_codeformer.pth in the models folder; using an officially reformatted or community-quantized file that dropped the 'params'/'params_ema' wrapper; interrupted downloads.
Related errors
- checkpoint url or path is invalid
- checkpoint url or path is invalid
- CORRUPTED MODEL: one of the q-k-v values for the text encode
- ERROR: Could not detect model type of: {}
- normalize should be True if scale is passed
AI-assisted analysis of lllyasviel/Fooocus@ae05379cc9 (2026-08-15).
Data as JSON: /api/errors/083a9507de7ac05d.
Report an issue: GitHub.