{"record":{"id":"083a9507de7ac05d","repo":"lllyasviel/Fooocus","slug":"wrong-params","errorCode":null,"errorMessage":"Wrong params!","messagePattern":"Wrong params!","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ldm_patched/pfn/architecture/face/codeformer.py","lineNumber":392,"sourceCode":"            )\n        self.generator = Generator(\n            nf, ch_mult, res_blocks, img_size, attn_resolutions, emb_dim\n        )\n\n        if model_path is not None:\n            chkpt = torch.load(model_path, map_location=\"cpu\", weights_only=True)\n            if \"params_ema\" in chkpt:\n                self.load_state_dict(\n                    torch.load(model_path, map_location=\"cpu\", weights_only=True)[\"params_ema\"]\n                )\n                logger.info(f\"vqgan is loaded from: {model_path} [params_ema]\")\n            elif \"params\" in chkpt:\n                self.load_state_dict(\n                    torch.load(model_path, map_location=\"cpu\", weights_only=True)[\"params\"]\n                )\n                logger.info(f\"vqgan is loaded from: {model_path} [params]\")\n            else:\n                raise ValueError(\"Wrong params!\")\n\n    def forward(self, x):\n        x = self.encoder(x)\n        quant, codebook_loss, quant_stats = self.quantize(x)\n        x = self.generator(quant)\n        return x, codebook_loss, quant_stats\n\n\ndef calc_mean_std(feat, eps=1e-5):\n    \"\"\"Calculate mean and std for adaptive_instance_normalization.\n    Args:\n        feat (Tensor): 4D tensor.\n        eps (float): A small value added to the variance to avoid\n            divide-by-zero. Default: 1e-5.\n    \"\"\"\n    size = feat.size()\n    assert len(size) == 4, \"The input feature should be 4D tensor.\"\n    b, c = size[:2]","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/ldm_patched/pfn/architecture/face/codeformer.py#L374-L410","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"import torch\n\nckpt = torch.load(model_path, map_location='cpu', weights_only=True)\n# before: neither 'params_ema' nor 'params' -> ValueError: Wrong params!\n# after:\nif 'params_ema' not in ckpt and 'params' not in ckpt:\n    torch.save({'params': ckpt}, model_path)  # re-wrap raw state dict\n    ckpt = torch.load(model_path, map_location='cpu', weights_only=True)","handlingStrategy":"validation","validationCode":"import torch\n\ndef is_codeformer_vqgan(path) -> bool:\n    try:\n        ckpt = torch.load(path, map_location='cpu', weights_only=True)\n    except Exception:\n        return False\n    return isinstance(ckpt, dict) and ('params_ema' in ckpt or 'params' in ckpt)\n\nif not is_codeformer_vqgan(vqgan_path):\n    raise SystemExit(f'{vqgan_path} is not a CodeFormer VQGAN file (needs params/params_ema key)')","typeGuard":"def is_vqgan_ckpt(obj) -> bool:\n    return isinstance(obj, dict) and ('params_ema' in obj or 'params' in obj)","tryCatchPattern":"try:\n    restorer = CodeFormer(vqgan_path, codeformer_path, ...)\nexcept ValueError as e:\n    if str(e) == 'Wrong params!':\n        raise ModelFileError(f'{vqgan_path}: expected keys params/params_ema missing - wrong or corrupt CodeFormer VQGAN file') from e\n    raise","preventionTips":["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"],"tags":["codeformer","face-restoration","vqgan","checkpoint","wrong-file"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}