# Initilize the \`SAM\`/\`SAM2\` predictor without inferencing

**URL:** <https://community.ultralytics.com/t/initilize-the-sam-sam2-predictor-without-inferencing/1286>\
**Category:** Support\
**Tags:** code\
**Created:** [July 31, 2025, 2:02am UTC](https://community.ultralytics.com/t/initilize-the-sam-sam2-predictor-without-inferencing/1286 "2025-07-31T02:02:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kallinteris-Andreas](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/kallinteris-andreas/32/838_2.png) [@Kallinteris-Andreas](https://community.ultralytics.com/u/Kallinteris-Andreas)\
**Post date:** [July 31, 2025, 2:02am UTC](https://community.ultralytics.com/t/initilize-the-sam-sam2-predictor-without-inferencing/1286/1 "2025-07-31T02:02:51Z")

</div>

How to initialize SAM predictor without running inference to dummy images, to save time

```python
from ultralytics import SAM
            
# Initialize
self.model = SAM("sam2tiny")
self.model() ## note this prints:
"""
WARNING ⚠️ 'source' is missing. Using 'source=/home/master-andreas/my_proj/test_env/lib/python3.11/site-packages/ultralytics/assets'.

image 1/2 /home/master-andreas/my_proj/test_env/lib/python3.11/site-packages/ultralytics/assets/bus.jpg: 1024x1024 1 0, 1 1, 1 2, 1 3, 1 4, 1 5, 1 6, 1 7, 1 8, 1 9, 1 10, 1 11, 1 12, 9691.9ms
image 2/2 /home/master-andreas/my_proj/test_env/lib/python3.11/site-packages/ultralytics/assets/zidane.jpg: 1024x1024 1 0, 1 1, 1 2, 9208.4ms
"""

# Inference
results = self.model.predictor(image, points=[[point]], imgsz=size, multimask_output=True)

```

The reason why i want to initialize the `predictor` is: because i want `multimask_output=True` [Ambigueous promt with SAM, return top 3 Masks](https://community.ultralytics.com/t/ambigueous-promt-with-sam-return-top-3-masks/1240)

---

<div class="post-metadata">

**Author:** ![Toxite](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/toxite/32/123_2.png) [@Toxite](https://community.ultralytics.com/u/Toxite)\
**Post date:** [July 31, 2025, 7:58pm UTC](https://community.ultralytics.com/t/initilize-the-sam-sam2-predictor-without-inferencing/1286/2 "2025-07-31T19:58:34Z")

</div>

You can check this,:

> <https://github.com/ultralytics/ultralytics/issues/18838#issuecomment-2611706864>
>
> \### Search before asking
> 
> \- \[x\] I have searched the Ultralytics YOLO \[issues\](ht…tps://github.com/ultralytics/ultralytics/issues) and \[discussions\](https://github.com/orgs/ultralytics/discussions) and found no similar questions.
> 
> 
> \### Question
> 
> \# Optimizing SAM Inference for High-Resolution Image Segmentation with Hundreds of Prompts
> 
> \## \*\*Description\*\*
> 
> I'm currently working on segmenting objects within high-resolution images using SAM 2.1 and MobileSAM. My workflow involves the following steps:
> 
> 1. \*\*Bounding Box Detection:\*\*  
> - I utilize another model to perform bounding box (bbox) detection.
> - A single image typically contains \*\*hundreds of objects\*\*, resulting in \*\*hundreds of bbox annotations\*\*.
> 
> 2. \*\*Segmentation with SAM:\*\*  
> - Mainly using bbox prompts, I employ SAM to segment each detected object.
> - \*\*Challenge:\*\* While SAM's inference time is relatively fast, I followed the method described in \[Ultralytics SAM Documentation\](https://docs.ultralytics.com/ko/models/sam-2/) (results = model("path/to/image.jpg", bboxes=\[100, 100, 200, 200\])). As a result, there are \*\*hundreds of high-resolution image inputs\*\* and \*\*repeated image encodings\*\*, which cause \*\*significant time delays\*\*.
> 
> \## \*\*Problem Statement\*\*
> 
> My primary goal is to efficiently segment hundreds of objects in a single high-resolution image. Specifically:
> 
> \- \*\*Multiple Prompts Handling:\*\*  
> - For a single image, I need to input \*\*hundreds of bbox or point prompts for different objects\*\*.
> 
> \- \*\*Performance Bottleneck:\*\*  
> - Current approaches cause significant time lags due to hundreds of high-resolution image inputs and iterative image encoding, although there is only one type of input image.
> 
> \- \*\*Segmentation Accuracy:\*\*  
> - If you use whole image segmentation without specific prompts, objects are not accurately distinguished, so you must use bbox prompts.
> 
> \## \*\*Desired Outcome\*\*
> 
> To optimize the segmentation process, I aim to:
> 
> 1. \*\*Batch Processing of Prompts:\*\*  
> - If that's possibleI, i want input all (or a large batch of) bbox or point prompts \*\*simultaneously\*\* to minimize processing time.
> 
> 2. \*\*Single Image Encoding:\*\*  
> - \*\*image encoding step is performed only once\*\*, even if prompts are processed sequentially.
> 
> \## \*\*Request for Assistance\*\*
> 
> I'm seeking guidance or potential solutions to address the following:
> 
> \- \*\*Efficient Prompt Handling:\*\*  
> - How can I input hundreds of prompts for a single high-resolution image without incurring significant processing delays?
> 
> \- \*\*Optimizing Image Encoding:\*\*  
> - Is there a way to reuse image encoding from multiple prompt inputs, or to pre-encode and enter it when needed?
> 
> \## \*\*Additional Information\*\*
> 
> \- \*\*Tools & Versions:\*\*  
> - SAM 2.1 / MobileSAM
> - Ultralytics' SAM implementation
> 
> \- \*\*Image Specifications:\*\*  
> - Ultra-high resolution (\>10MB per image)
> - Hundreds of objects per image
> 
> I would like to know if an official feature for this is provided, or if you have any reference materials, insights, or suggestions for similar implementations, I would greatly appreciate it. Thank you for your support!
