tensorflow/models · error · ValueError

`image` not of type np.uint8

Error message

`image` not of type np.uint8

What it means

Dtype guard in draw_mask_on_image_array: the routine blends the mask into the image using uint8 arithmetic; an `image` array of another dtype would produce wrong color blending or silent upcasting, so it is rejected before drawing.

Solutions

  1. Convert the image to np.uint8 before visualization.
  2. Use image.astype(np.uint8) after scaling float images to [0, 255].
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at official/vision/utils/object_detection/visualization_utils.py:663 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/9e02e478a9b1ee85. Report an issue: GitHub.

Appendix: source

Thrown at official/vision/utils/object_detection/visualization_utils.py:663

                 outline=color,
                 fill=color)


def draw_mask_on_image_array(image, mask, color='red', alpha=0.4):
  """Draws mask on an image.

  Args:
    image: uint8 numpy array with shape (img_height, img_height, 3)
    mask: a uint8 numpy array of shape (img_height, img_height) with values
      between either 0 or 1.
    color: color to draw the keypoints with. Default is red.
    alpha: transparency value between 0 and 1. (default: 0.4)

  Raises:
    ValueError: On incorrect data type for image or masks.
  """
  if image.dtype != np.uint8:
    raise ValueError('`image` not of type np.uint8')
  if mask.dtype != np.uint8:
    raise ValueError('`mask` not of type np.uint8')
  if np.any(np.logical_and(mask != 1, mask != 0)):
    raise ValueError('`mask` elements should be in [0, 1]')
  if image.shape[:2] != mask.shape:
    raise ValueError('The image has spatial dimensions %s but the mask has '
                     'dimensions %s' % (image.shape[:2], mask.shape))
  rgb = ImageColor.getrgb(color)
  pil_image = Image.fromarray(image)

  solid_color = np.expand_dims(
      np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3])
  pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA')
  pil_mask = Image.fromarray(np.uint8(255.0 * alpha * mask)).convert('L')
  pil_image = Image.composite(pil_solid_color, pil_image, pil_mask)
  np.copyto(image, np.array(pil_image.convert('RGB')))

View on GitHub (pinned to e006f5f0d5)