facebookresearch / facebookresearch/sam2

SAM2AutomaticMaskGenerator() function gets stuck

Open
#566 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
19.9k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Task:** Using SAM2AutomaticMaskGenerator() function in a **for loop** structure to segment everything for several images.
**Problem:** When I used this function in images segmentation, it ran smoothly for the first several images. But after that, things went wrong. It just stuck there. And I used windows task managers to monitor the usage of the GPU, it shows like the attached picture. And I don't know why.
![Image](https://github.com/user-attachments/assets/14f2d0ec-cb6d-4461-bf53-4ec62edb7561)

Here are the parameters I used.
**model:** sam2.1_hiera_large.pt
**points_per_side:** 64,
**points_per_batch:** 64,
**GPU:** RTX 3080
**Image size:** usually 680 * 1024
Under this situation, the normal inference time takes 16-25 seconds per image. But after handling a few images, the program just stuck there. My code is like:

```
for i in range(0,30):
image = Image.open(f"{i}.jpg").convert("RGB")
image = np.array(image)
### followings are codes to generate masks using SAM2AutomaticMaskGenerator()
```

I've tested the same code by using SAM, rather than SAM2, it runs really smoothly, without any stuck.
And I also handle the GPU memory by deleting the variable in the function, like the following codes, but the problem still happens.

```
def SAM2_func():
### here is how to use sam2 to segment the whole image.

del sam2
del mask_generator2
del masks
torch.cuda.empty_cache()
gc.collect()

return results
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.