{"record":{"id":"a03fa7443f444611","repo":"lllyasviel/Fooocus","slug":"input-size-must-have-a-shape-of-3-h-w-got","errorCode":null,"errorMessage":"Input size must have a shape of (*, 3, H, W). Got {image.shape}","messagePattern":"Input size must have a shape of \\(\\*, 3, H, W\\)\\. Got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"ldm_patched/contrib/external_canny.py","lineNumber":132,"sourceCode":"\n    Args:\n        image: RGB image to be converted to grayscale with shape :math:`(*,3,H,W)`.\n        rgb_weights: Weights that will be applied on each channel (RGB).\n            The sum of the weights should add up to one.\n    Returns:\n        grayscale version of the image with shape :math:`(*,1,H,W)`.\n\n    .. note::\n       See a working example `here <https://kornia.readthedocs.io/en/latest/\n       color_conversions.html>`__.\n\n    Example:\n        >>> input = torch.rand(2, 3, 4, 5)\n        >>> gray = rgb_to_grayscale(input) # 2x1x4x5\n    \"\"\"\n\n    if len(image.shape) < 3 or image.shape[-3] != 3:\n        raise ValueError(f\"Input size must have a shape of (*, 3, H, W). Got {image.shape}\")\n\n    if rgb_weights is None:\n        # 8 bit images\n        if image.dtype == torch.uint8:\n            rgb_weights = torch.tensor([76, 150, 29], device=image.device, dtype=torch.uint8)\n        # floating point images\n        elif image.dtype in (torch.float16, torch.float32, torch.float64):\n            rgb_weights = torch.tensor([0.299, 0.587, 0.114], device=image.device, dtype=image.dtype)\n        else:\n            raise TypeError(f\"Unknown data type: {image.dtype}\")\n    else:\n        # is tensor that we make sure is in the same device/dtype\n        rgb_weights = rgb_weights.to(image)\n\n    # unpack the color image channels with RGB order\n    r: Tensor = image[..., 0:1, :, :]\n    g: Tensor = image[..., 1:2, :, :]\n    b: Tensor = image[..., 2:3, :, :]","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/ldm_patched/contrib/external_canny.py#L114-L150","documentation":"Kornia's rgb_to_grayscale (vendored into external_canny.py) validates that the last-but-two dimension is exactly 3 (RGB channels) and that the tensor is at least 3-D. Any other layout — channel-first grayscale, channel-last HWC, or 4-channel RGBA — fails this shape contract. This mirrors Kornia's strict (*,3,H,W) convention.","triggerScenarios":"Feeding the Canny node an image tensor of shape (1,1,H,W), (H,W,3), (B,H,W,3), or (B,4,H,W); i.e. forgetting to permute HWC->CHW after decoding, or passing an already-grayscale/alpha image.","commonSituations":"Loading with PIL/opencv (HWC uint8) and converting to torch without permute(2,0,1); VAE-decoded latents or masks (1-channel) routed into the Canny preprocessor; RGBA exports from image editors.","solutions":["Permute HWC to CHW: img = torch.from_numpy(img).permute(2, 0, 1) and add a batch dim: img[None].","If image is RGBA, drop alpha first: img = img[..., :3].","If image is already grayscale, replicate to 3 channels: img = gray.repeat(1, 3, 1, 1).","Let the node's own loader handle conversion where available (it expects IMAGE in (B,H,W,3) and converts internally); avoid double-converting."],"exampleFix":"# before\nimg = torch.from_numpy(cv2.imread(p))  # HxWx3\ngrey = rgb_to_grayscale(img)  # ValueError\n\n# after\nimg = torch.from_numpy(cv2.imread(p)).permute(2, 0, 1).unsqueeze(0).float() / 255.  # 1x3xHxW\ngrey = rgb_to_grayscale(img)","handlingStrategy":"type-guard","validationCode":"def to_chw(img: torch.Tensor) -> torch.Tensor:\n    if img.shape[-1] in (1, 3) and (img.ndim < 3 or img.shape[-3] not in (1, 3)):\n        img = img.permute(*range(img.ndim - 3), img.ndim - 1, img.ndim - 3, img.ndim - 2)  # HWC->CHW\n    if img.shape[-3] == 1:\n        img = img.repeat_interleave(3, dim=-3)\n    if img.shape[-3] == 4:\n        img = img[..., :3, :, :]\n    assert img.shape[-3] == 3, f'expected (*,3,H,W), got {tuple(img.shape)}'\n    return img","typeGuard":"def is_rgb_chw(t: torch.Tensor) -> bool:\n    return t.ndim >= 3 and t.shape[-3] == 3","tryCatchPattern":"try:\n    gray = rgb_to_grayscale(img)\nexcept ValueError as e:\n    if 'shape of (*, 3, H, W)' in str(e):\n        gray = rgb_to_grayscale(to_chw(img))\n    else:\n        raise","preventionTips":["Standardize on CHW float tensors at your pipeline entry; convert once after cv2/PIL load.","Add shape assertions before Kornia-style color ops.","Drop alpha channels and expand grayscale at load time, not downstream."],"tags":["kornia","tensor-shape","canny","image-preprocessing"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}