tensorflow/models · error · ValueError

`mask` not of type np.uint8

Error message

`mask` not of type np.uint8

What it means

Dtype guard in draw_mask_on_image_array: the mask is treated as a binary 0/1 uint8 layer blended with alpha into the image; a non-uint8 mask dtype breaks the blending arithmetic and is rejected before drawing.

Solutions

  1. Convert the mask to np.uint8 before visualization.
  2. Use mask.astype(np.uint8) after thresholding or scaling.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at official/vision/utils/object_detection/visualization_utils.py:665 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/deb7507adf36de55. Report an issue: GitHub.

Appendix: source

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


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')))


def visualize_boxes_and_labels_on_image_array(
    image,

View on GitHub (pinned to e006f5f0d5)