lllyasviel/Fooocus · error · RuntimeError

An image must be set with .set_image(...) to generate an emb

Error message

An image must be set with .set_image(...) to generate an embedding.

What it means

get_image_embedding() returns the features tensor cached by set_image(); with no image set there is nothing to return, so it raises RuntimeError. The following assert ('Features must exist...') covers the impossible case where the flag and features disagree. Shape is 1xCxHxW (typically C=256, H=W=64).

Source

Thrown at extras/sam/predictor.py:271

            multimask_output=multimask_output,
        )

        # Upscale the masks to the original image resolution
        masks = self.patcher.model.postprocess_masks(low_res_masks, self.input_size, self.original_size)

        if not return_logits:
            masks = masks > self.patcher.model.mask_threshold

        return masks, iou_predictions, low_res_masks

    def get_image_embedding(self) -> torch.Tensor:
        """
        Returns the image embeddings for the currently set image, with
        shape 1xCxHxW, where C is the embedding dimension and (H,W) are
        the embedding spatial dimension of SAM (typically C=256, H=W=64).
        """
        if not self.is_image_set:
            raise RuntimeError(
                "An image must be set with .set_image(...) to generate an embedding."
            )
        assert self.features is not None, "Features must exist if an image has been set."
        return self.features

    @property
    def device(self) -> torch.device:
        return self.patcher.model.device

    def reset_image(self) -> None:
        """Resets the currently set image."""
        self.is_image_set = False
        self.features = None
        self.orig_h = None
        self.orig_w = None
        self.input_h = None
        self.input_w = None

View on GitHub (pinned to ae05379cc9)

Solutions

  1. Call set_image(image) first; get_image_embedding() then returns the cached features without recompute.
  2. To reset between images, call reset_image() then set_image(next) before get_image_embedding().
  3. For pure embedding pipelines, keep the order fixed: set_image -> get_image_embedding -> (optionally predict).
  4. Cache the returned tensor (it stays valid until the next set_image/reset_image).

Example fix

# before
emb = predictor.get_image_embedding()  # RuntimeError

# after
predictor.set_image(image_rgb)
emb = predictor.get_image_embedding()  # 1x256x64x64
Defensive patterns

Strategy: validation

Validate before calling

if not predictor.is_image_set:
    raise RuntimeError('set_image() must be called before get_image_embedding()')

Type guard

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

Try / catch

try:
    emb = predictor.get_image_embedding()
except RuntimeError as e:
    if 'set_image' in str(e):
        raise RuntimeError('No image set: call set_image() before get_image_embedding()') from e
    raise

Prevention

When it happens

Trigger: Calling predictor.get_image_embedding() before set_image(), or after reset_image(). Happens often when building offline embedding caches for the ONNX-style split workflow.

Common situations: Precomputing image embeddings for a gallery (search/retrieval apps) and calling the API in the wrong order; refactoring set_image into a separate worker process while leaving get_image_embedding in another.

Related errors


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