SAM3SemanticPredictor uses previous image as `orig_img` when a new source image is passed
Search before asking
- I have searched the Ultralytics YOLO issues and discussions and found no similar questions.
Question
SAM3SemanticPredictor uses previous image as orig_img when a new source image is passed
Environment
- OS: Windows
- Python: 3.10.21
- Ultralytics: 8.4.161
- PyTorch: 2.7.0+cu128
- CUDA: 12.8
- GPU: NVIDIA GeForce RTX 5060 Ti 16GB
- Predictor:
ultralytics.models.sam.SAM3SemanticPredictor
Description
I am using SAM3SemanticPredictor in a FastAPI service to perform image segmentation with text prompts.
I encountered an issue where the image passed to SAM3SemanticPredictor is a new image, but results[0].orig_img and the image saved under runs/segment/predict-* are still from the previous inference.
For example:
Inference 1:
A.png
↓
SAM3
↓
results[0].orig_img = A.png
Inference 2:
B.png
↓
SAM3
↓
results[0].orig_img = A.png <-- unexpectedThe source path printed immediately before inference is definitely the new image.
The saved result directory is also a new directory (predict-8, etc.), but the image used as the visualization/background is still the previous image.
Reproduction code
The relevant part of my FastAPI endpoint is approximately:
async def segment(
file: UploadFile,
text_prompts: List[str] = Form(default=None)
):
file_path = await Utils.save_img(file)
if not text_prompts:
text_prompts = ["characters", "weapon", "Props"]
overrides = {
"conf": 0.25,
"task": "segment",
"mode": "predict",
"model": "sam3.1_multiplex.pt",
"quantize": 16,
"save": True,
}
predictor = SAM3SemanticPredictor(
overrides=overrides
)
print("========== SAM3 INPUT ==========")
print("path:", file_path)
print("exists:", file_path.exists())
print("size:", file_path.stat().st_size)
results = predictor(
source=str(file_path),
text=text_prompts
)
print("========== SAM3 OUTPUT ==========")
print("save_dir:", predictor.save_dir)
result = results[0]
print("========== RESULT ==========")
print("orig_img type:", type(result.orig_img))
print("orig_img shape:", result.orig_img.shape)
debug_path = file_path.with_name(
file_path.stem + "_debug_orig.jpg"
)
cv2.imwrite(
str(debug_path),
result.orig_img
)
print("DEBUG ORIG:", debug_path)I also tested the following previously:
predictor.reset_image()
img_a = cv2.imread(str(file_path))
predictor.set_image(img_a)
results = predictor(
source=img_a,
text=text_prompts
)However, removing reset_image() and set_image() did not solve the issue.
Actual logs
The input image is a new file:
========== SAM3 INPUT ==========
path: static\\\uploads\2026-09-24\7f7b91f84e644302a7efd0f1fbedb083.png
exists: True
size: 25831821Ultralytics then reports:
Ultralytics 8.4.161 Python-3.10.21 torch-2.7.0+cu128 CUDA:0 (NVIDIA GeForce RTX 5060 Ti, 16283MiB)
WARNING imgsz=[640] must be multiple of max stride 14, updating to [644]
WARNING imgsz=[640] must be multiple of max stride 14, updating to [644]
0: 644x644 24 characterss, 218.8ms
Speed: 3.3ms preprocess, 218.8ms inference, 101.1ms postprocess per image at shape (1, 3, 644, 644)
Results saved to runs\segment\predict-8
========== SAM3 OUTPUT ==========
save_dir: runs\segment\predict-8
========== RESULT ==========
orig_img type: <class 'numpy.ndarray'>
orig_img shape: (4699, 10461, 3)
DEBUG ORIG:
static\\\uploads\2026-09-24\7f7b91f84e644302a7efd0f1fbedb083_debug_orig.jpgI visually compared:
- The input file:
7f7b91f84e644302a7efd0f1fbedb083.png results[0].orig_img- The image saved in:
runs/segment/predict-8
The input file is the new image, but both orig_img and the saved visualization use the previous image.
Important observations
1. The input file itself is correct
The file exists and its size is different from the previous image.
path: .../7f7b91f84e644302a7efd0f1fbedb083.png
exists: True
size: 258318212. A new prediction directory is created
For example:
runs/segment/predict-8So this does not appear to be simply an old output directory being displayed.
3. results[0].orig_img is already the old image
This is important because the problem occurs before my own post-processing code.
I explicitly save:
cv2.imwrite(
str(debug_path),
result.orig_img
)and the resulting debug image is the previous image.
4. Creating a new SAM3SemanticPredictor per request did not solve it
The predictor is created inside the FastAPI request handler:
predictor = SAM3SemanticPredictor(
overrides=overrides
)Therefore I am not intentionally sharing a global predictor instance between requests.
5. Removing reset_image() and set_image() did not solve it
I also tested the simpler form:
results = predictor(
source=str(file_path),
text=text_prompts
)The problem still occurs.
Expected behavior
If I call:
results = predictor(
source=str(new_image_path),
text=text_prompts
)I expect:
results[0].orig_imgto correspond to new_image_path.
The visualization saved by Ultralytics should also use the new image as the background.
Actual behavior
Instead:
source = new image
↓
SAM3SemanticPredictor
↓
results[0].orig_img = previous image
↓
runs/segment/predict-* = previous imageThe segmentation itself appears to run successfully, but the original image associated with the result appears to come from the previous inference.
Questions
Could someone help clarify where SAM3SemanticPredictor gets results[0].orig_img from?
Specifically:
- Is
orig_imgexpected to always come from the currentsource? - Does
SAM3SemanticPredictormaintain any image/dataset/cache state that can survive between calls? - Is
set_image()supposed to be used together withsource=forSAM3SemanticPredictor, or should only one of these APIs be used? - Could
SAM3SemanticPredictorbe reusing an internal dataset/loader or cached image? - Is there a recommended way to completely reset the predictor before passing a new image?
- Is this behavior expected in Ultralytics
8.4.161?
Any guidance on the correct API usage or a minimal reproducible example would be greatly appreciated.
Thank you.
Additional
No response
Source: ultralytics/ultralytics