lllyasviel/Fooocus · error · TypeError

Unknown data type: {image.dtype}

Error message

Unknown data type: {image.dtype}

What it means

Inside rgb_to_grayscale (vendored Kornia), when no explicit rgb_weights are given, the tensor dtype must be uint8 (8-bit path) or float16/32/64 (floating path). Any other dtype — typically torch.int, torch.long, torch.bfloat16, or bool — hits TypeError('Unknown data type'). The luminance weights tensor must match the image dtype for the weighted channel sum.

Source

Thrown at ldm_patched/contrib/external_canny.py:142

       color_conversions.html>`__.

    Example:
        >>> input = torch.rand(2, 3, 4, 5)
        >>> gray = rgb_to_grayscale(input) # 2x1x4x5
    """

    if len(image.shape) < 3 or image.shape[-3] != 3:
        raise ValueError(f"Input size must have a shape of (*, 3, H, W). Got {image.shape}")

    if rgb_weights is None:
        # 8 bit images
        if image.dtype == torch.uint8:
            rgb_weights = torch.tensor([76, 150, 29], device=image.device, dtype=torch.uint8)
        # floating point images
        elif image.dtype in (torch.float16, torch.float32, torch.float64):
            rgb_weights = torch.tensor([0.299, 0.587, 0.114], device=image.device, dtype=image.dtype)
        else:
            raise TypeError(f"Unknown data type: {image.dtype}")
    else:
        # is tensor that we make sure is in the same device/dtype
        rgb_weights = rgb_weights.to(image)

    # unpack the color image channels with RGB order
    r: Tensor = image[..., 0:1, :, :]
    g: Tensor = image[..., 1:2, :, :]
    b: Tensor = image[..., 2:3, :, :]

    w_r, w_g, w_b = rgb_weights.unbind()
    return w_r * r + w_g * g + w_b * b

def canny(
    input,
    low_threshold = 0.1,
    high_threshold = 0.2,
    kernel_size  = 5,
    sigma = 1,

View on GitHub (pinned to ae05379cc9)

Solutions

  1. Cast before calling: img = img.to(torch.float32) (values in [0,1]) or img.to(torch.uint8).
  2. For numpy input, use img.astype(np.float32) or np.uint8 before torch.from_numpy.
  3. If you need bfloat16 support, pass explicit rgb_weights = rgb_weights.to(image.dtype) matching your dtype, or patch the elif to include torch.bfloat16.
  4. Normalize floats to [0,1] so the 0.299/0.587/0.114 weights produce sane luma.

Example fix

# before
img = torch.from_numpy(np.array(pil_img))  # may be int64/other
grey = rgb_to_grayscale(img)  # TypeError

# after
img = torch.from_numpy(np.array(pil_img)).to(torch.float32).div_(255.)
grey = rgb_to_grayscale(img)
Defensive patterns

Strategy: type-guard

Validate before calling

SUPPORTED = (torch.uint8, torch.float16, torch.float32, torch.float64)
if image.dtype not in SUPPORTED:
    image = image.to(torch.float32)
assert image.dtype in SUPPORTED

Type guard

def has_supported_img_dtype(t: torch.Tensor) -> bool:
    return t.dtype in (torch.uint8, torch.float16, torch.float32, torch.float64)

Try / catch

try:
    gray = rgb_to_grayscale(img)
except TypeError as e:
    if 'Unknown data type' in str(e):
        gray = rgb_to_grayscale(img.to(torch.float32))
    else:
        raise

Prevention

When it happens

Trigger: Passing an image tensor of dtype torch.long/int32 (e.g. raw indices), torch.bool, or torch.bfloat16 into the Canny preprocessor chain without casting.

Common situations: Tensors created with torch.randint or from numpy int arrays; bfloat16 images from mixed-precision pipelines (some newer torch versions add bfloat16 paths that this vendored copy lacks); masks converted from bool surviving into the color path.

Related errors


AI-assisted analysis of lllyasviel/Fooocus@ae05379cc9 (2026-08-15). Data as JSON: /api/errors/c2b0da5b9ba2a85f. Report an issue: GitHub.