lllyasviel/Fooocus · error · RuntimeError

An image must be set with .set_image(...) before mask predic

Error message

An image must be set with .set_image(...) before mask prediction.

What it means

SAM's SamPredictor.predict() requires an input image before decoding prompts, because set_image() precomputes the image embeddings the mask decoder consumes. Calling predict() on a fresh predictor (or after reset_image()) raises RuntimeError. The check is a simple is_image_set flag.

Source

Thrown at extras/sam/predictor.py:146

            For ambiguous input prompts (such as a single click), this will often
            produce better masks than a single prediction. If only a single
            mask is needed, the model's predicted quality score can be used
            to select the best mask. For non-ambiguous prompts, such as multiple
            input prompts, multimask_output=False can give better results.
          return_logits (bool): If true, returns un-thresholded masks logits
            instead of a binary mask.

        Returns:
          (np.ndarray): The output masks in CxHxW format, where C is the
            number of masks, and (H, W) is the original image size.
          (np.ndarray): An array of length C containing the model's
            predictions for the quality of each mask.
          (np.ndarray): An array of shape CxHxW, where C is the number
            of masks and H=W=256. These low resolution logits can be passed to
            a subsequent iteration as mask input.
        """
        if not self.is_image_set:
            raise RuntimeError("An image must be set with .set_image(...) before mask prediction.")

        # Transform input prompts
        coords_torch, labels_torch, box_torch, mask_input_torch = None, None, None, None
        if point_coords is not None:
            assert (
                point_labels is not None
            ), "point_labels must be supplied if point_coords is supplied."
            point_coords = self.transform.apply_coords(point_coords, self.original_size)
            coords_torch = torch.as_tensor(point_coords, dtype=torch.float, device=self.load_device)
            labels_torch = torch.as_tensor(point_labels, dtype=torch.int, device=self.load_device)
            coords_torch, labels_torch = coords_torch[None, :, :], labels_torch[None, :]
        if box is not None:
            box = self.transform.apply_boxes(box, self.original_size)
            box_torch = torch.as_tensor(box, dtype=torch.float, device=self.load_device)
            box_torch = box_torch[None, :]
        if mask_input is not None:
            mask_input_torch = torch.as_tensor(mask_input, dtype=torch.float, device=self.load_device)
            mask_input_torch = mask_input_torch[None, :, :, :]

View on GitHub (pinned to ae05379cc9)

Solutions

  1. Call predictor.set_image(image_rgb) once per image before any predict() calls for that image.
  2. If switching images, call set_image() again (it recomputes features and re-raises is_image_set).
  3. For many prompts on one image, set_image once and loop predict() — that is the intended amortization.
  4. For headless/embedding workflows use SamPredictor with set_image, or the lower-level SamTwoWayTransformer API (ONNX `SamEmbedding`/`SamDecoder` split) instead of skipping set_image.

Example fix

# before
predictor = SamPredictor(sam)
masks, scores, logits = predictor.predict(point_coords=pts, point_labels=lbls)

# after
predictor = SamPredictor(sam)
predictor.set_image(image_rgb)  # required first
masks, scores, logits = predictor.predict(point_coords=pts, point_labels=lbls)
Defensive patterns

Strategy: validation

Validate before calling

if not predictor.is_image_set:
    raise RuntimeError('set_image() must be called before predict()')
# or simply guard the whole call:
assert predictor.is_image_set, 'call predictor.set_image(image) first'

Type guard

def can_predict(predictor) -> bool:
    return bool(predictor.is_image_set)

Try / catch

try:
    masks, scores, logits = predictor.predict(...)
except RuntimeError as e:
    if 'set_image' in str(e):
        predictor.set_image(image_rgb)
        masks, scores, logits = predictor.predict(...)
    else:
        raise

Prevention

When it happens

Trigger: Instantiating SamPredictor and immediately calling predict(point_coords=..., ...) or predict(box=...) without set_image(np_image) first; or calling predict after reset_image() without re-setting an image.

Common situations: Reusing one predictor across a batch of images and forgetting set_image between iterations; restructuring inference code so the embedding step is skipped for 'prompt-only' updates; embedding cached offline then attempting to decode with a stateless predictor.

Related errors


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