facebookresearch / facebookresearch/segment-anything
[Help] How to free up memory after each segmentation
- Dominant language
- Jupyter Notebook
- Stars
- 54.9k
- Forks
- 6.4k
- PR merge metrics
- No merged PRs in 30d
Description
Hi, I'm using SAM to get masks for segmenting my images (~320, 960*800 ea.) on CPU, but I noticed that SAM eats up the memory quickly. I'm using a for loop to segment each image, and after ~60 iterations the memory usage adds up gradually to 6GBs, which max out the free memory in my desktop. I wonder if there is a way to release memory after each iteration is finished?
My code looks like this (My for loop calls this function in each iteration):
```
def mp_ext(img):
.... variables are defined here and removed since it's irrelevant to my question
# Tell SAM to remember this image
image = cv2.imread(img)
predictor.set_image(image)
# Points are input to the model in (x,y) format and come with labels 1 (foreground point) or 0 (background point)
input_point = np.array([centre])
input_label = np.array([1])
# Make prediction
masks, scores, logits = predictor.predict(
point_coords=input_point,
point_labels=input_label,
multimask_output=True,
)
mask_input = logits[np.argmax(scores), :, :] # Choose the model's best mask
# add extra points for the melt track
input_point = np.array([p1,p2,p3,p4])
input_label = np.array([1,1,1,1])
masks, _, _ = predictor.predict(
point_coords=input_point,
point_labels=input_label,
mask_input=mask_input[None, :, :],
multimask_output=False,
)
```
Thank you!
Contributor guide
Research direction
Start with the reported mp_ext loop and its predictor.set_image and predictor.predict calls, then reproduce the CPU run while monitoring memory across roughly 60 images. Check which objects remain referenced after each iteration and compare memory behavior between prediction calls. Done means memory usage no longer grows until the desktop runs out of free memory.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, opencv, python
- Domain
- computer-vision, machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100