{"record":{"id":"7d72e42798f5d525","repo":"lllyasviel/Fooocus","slug":"an-image-must-be-set-with-set-image-to-gener","errorCode":null,"errorMessage":"An image must be set with .set_image(...) to generate an embedding.","messagePattern":"An image must be set with \\.set_image\\(\\.\\.\\.\\) to generate an embedding\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"extras/sam/predictor.py","lineNumber":271,"sourceCode":"            multimask_output=multimask_output,\n        )\n\n        # Upscale the masks to the original image resolution\n        masks = self.patcher.model.postprocess_masks(low_res_masks, self.input_size, self.original_size)\n\n        if not return_logits:\n            masks = masks > self.patcher.model.mask_threshold\n\n        return masks, iou_predictions, low_res_masks\n\n    def get_image_embedding(self) -> torch.Tensor:\n        \"\"\"\n        Returns the image embeddings for the currently set image, with\n        shape 1xCxHxW, where C is the embedding dimension and (H,W) are\n        the embedding spatial dimension of SAM (typically C=256, H=W=64).\n        \"\"\"\n        if not self.is_image_set:\n            raise RuntimeError(\n                \"An image must be set with .set_image(...) to generate an embedding.\"\n            )\n        assert self.features is not None, \"Features must exist if an image has been set.\"\n        return self.features\n\n    @property\n    def device(self) -> torch.device:\n        return self.patcher.model.device\n\n    def reset_image(self) -> None:\n        \"\"\"Resets the currently set image.\"\"\"\n        self.is_image_set = False\n        self.features = None\n        self.orig_h = None\n        self.orig_w = None\n        self.input_h = None\n        self.input_w = None\n","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/lllyasviel/Fooocus/blob/ae05379cc97bc4361ec8b4ec90193dab21be763f/extras/sam/predictor.py#L253-L289","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call set_image(image) first; get_image_embedding() then returns the cached features without recompute.","To reset between images, call reset_image() then set_image(next) before get_image_embedding().","For pure embedding pipelines, keep the order fixed: set_image -> get_image_embedding -> (optionally predict).","Cache the returned tensor (it stays valid until the next set_image/reset_image)."],"exampleFix":"# before\nemb = predictor.get_image_embedding()  # RuntimeError\n\n# after\npredictor.set_image(image_rgb)\nemb = predictor.get_image_embedding()  # 1x256x64x64","handlingStrategy":"validation","validationCode":"if not predictor.is_image_set:\n    raise RuntimeError('set_image() must be called before get_image_embedding()')","typeGuard":"def has_embedding(predictor) -> bool:\n    return bool(predictor.is_image_set)","tryCatchPattern":"try:\n    emb = predictor.get_image_embedding()\nexcept RuntimeError as e:\n    if 'set_image' in str(e):\n        raise RuntimeError('No image set: call set_image() before get_image_embedding()') from e\n    raise","preventionTips":["Always pair set_image with get_image_embedding in the same function scope.","In embedding-cache scripts, assert is_image_set right before extraction.","Remember reset_image() invalidates previously fetched embedding references' source."],"tags":["sam","embeddings","state-machine","runtimeerror"],"backgroundTag":null,"analyzedSha":"ae05379cc97bc4361ec8b4ec90193dab21be763f","analyzedAt":"2026-08-15T04:23:59.533Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}