{"record":{"id":"32830a61f2379318","repo":"PaddlePaddle/PaddleOCR","slug":"weight-format-not-supported-by-conversion","errorCode":null,"errorMessage":"Weight format not supported by conversion.","messagePattern":"Weight format not supported by conversion\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"ppocr/modeling/backbones/rec_resnetv2.py","lineNumber":633,"sourceCode":"\n\ndef adapt_input_conv(in_chans, conv_weight):\n    conv_type = conv_weight.dtype\n    conv_weight = (\n        conv_weight.float()\n    )  # Some weights are in torch.half, ensure it's float for sum on CPU\n    O, I, J, K = conv_weight.shape\n    if in_chans == 1:\n        if I > 3:\n            assert conv_weight.shape[1] % 3 == 0\n            # For models with space2depth stems\n            conv_weight = conv_weight.reshape(O, I // 3, 3, J, K)\n            conv_weight = conv_weight.sum(dim=2, keepdim=False)\n        else:\n            conv_weight = conv_weight.sum(dim=1, keepdim=True)\n    elif in_chans != 3:\n        if I != 3:\n            raise NotImplementedError(\"Weight format not supported by conversion.\")\n        else:\n            # NOTE this strategy should be better than random init, but there could be other combinations of\n            # the original RGB input layer weights that'd work better for specific cases.\n            repeat = int(math.ceil(in_chans / 3))\n            conv_weight = conv_weight.repeat(1, repeat, 1, 1)[:, :in_chans, :, :]\n            conv_weight *= 3 / float(in_chans)\n    conv_weight = conv_weight.to(conv_type)\n    return conv_weight\n\n\ndef named_apply(\n    fn: Callable, module: nn.Layer, name=\"\", depth_first=True, include_root=False\n) -> nn.Layer:\n    if not depth_first and include_root:\n        fn(module=module, name=name)\n    for child_name, child_module in module.named_children():\n        child_name = \".\".join((name, child_name)) if name else child_name\n        named_apply(","sourceCodeStart":615,"sourceCodeEnd":651,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/ppocr/modeling/backbones/rec_resnetv2.py#L615-L651","documentation":"This is weight-conversion helper logic (adapt_patch_embed-style, in_chans handling for ResNetV2-family rec backbones) that adapts a pretrained 3-channel stem conv to a different input channel count. For in_chans==1 it can sum/replicate filters, but for any other in_chans != 3 the original weight must have exactly I==3 input filters (conv_weight.shape[1]); otherwise conversion is undefined and NotImplementedError is raised.","triggerScenarios":"Loading a pretrained checkpoint whose first conv expects a channel count other than 3 while requesting in_chans other than 1 or 3 — e.g. in_chans=4 with a checkpoint stem built for 6-channel input (I=6).","commonSituations":"Fine-tuning rec ResNetV2 variants on multi-spectral/RGBA or concatenated inputs; using a checkpoint from a different model family whose stem layout doesn't match the 3-channel assumption of the adapter.","solutions":["Use in_chans=3 with standard RGB preprocessing (the supported pretrained path)","Or use in_chans=1 with grayscale input, which the converter handles by summing filters","For other channel counts, provide a stem weight with exactly 3 input filters, or write a custom adapter that initializes the new stem (e.g. average or repeat of the 3-channel kernels) instead of relying on this helper"],"exampleFix":"# before\ncreate_model(pretrained=True, in_chans=4)  # checkpoint stem I==6 -> NotImplementedError\n\n# after\ncreate_model(pretrained=True, in_chans=3)  # keep RGB; or in_chans=1 for grayscale","handlingStrategy":"validation","validationCode":"assert in_chans in (1, 3), f'stem-weight conversion supports in_chans 1 or 3, got {in_chans}'","typeGuard":"def convertible_in_chans(c: int) -> bool:\n    return c in (1, 3)","tryCatchPattern":null,"preventionTips":["Prefer RGB (3-channel) preprocessing when using pretrained ResNetV2 rec weights","If you must use N channels, plan a custom stem init instead of the built-in converter","Check checkpoint stem shape[1]==3 before attempting in_chans adaptation"],"tags":["pretrained-weights","channel-adaptation","resnet","recognition"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}