segment-anything icon indicating copy to clipboard operation
segment-anything copied to clipboard

Cannot generate masks by using SamAutomaticMaskGenerator

Open Hippozhibos opened this issue 1 year ago • 6 comments

Hi!

I'm using the code from automatic_mask_generator_example.ipynb to process my own image, but I cannot get any masks by using SamAutomaticMaskGenerator. However, when I use the code from predictor_example.ipynb, it works well. Do you have any idea about why is that? and how can I revise my code to generate masks automatically? Following are my code, hope that will be clear enough.

"

import modules part. Same as example code.

import ...

def show_ functions. Same as example code.

def show_anns(anns): def show_mask(mask, ax, random_color=False): def show_points(coords, labels, ax, marker_size=375): def show_box(box, ax):

load model and finetuned checkpoint

args = Namespace() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") args.image_size = 256 args.encoder_adapter = True args.sam_checkpoint = "/workdir/models/epoch30_sam.pth" sam = sam_model_registry"vit_b".to(device)

load image. Successfully loaded

image = cv2.imread('/home/zhangzhibo/SAM/SAM_demo_result.png') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) plt.figure(figsize=(20,20)) plt.imshow(image) plt.axis('off') plt.show()

Generate masks

mask_generator = SamAutomaticMaskGenerator(sam) masks = mask_generator.generate(image)

print(len(masks))

print(masks[0].keys())

plt.figure(figsize=(10,10)) plt.imshow(image) show_anns(masks) plt.axis('on') plt.show() "

The outputs are: len(masks) = 0, and show_anns(masks) actually add no masks on the image. I tried define mask_generator as this:

"mask_generator = SamAutomaticMaskGenerator( model=sam, points_per_side=32, pred_iou_thresh=0.85, stability_score_thresh=0.9, )"

and that make no difference.

Would you mind to help me and check about this code? Looking forward to your reply, Thanks!

Hippozhibos avatar Nov 22 '23 12:11 Hippozhibos

It seems like I should set the pred_iou_thresh and stability_score_thresh smaller...

Hippozhibos avatar Nov 22 '23 13:11 Hippozhibos

I meet the same question when running the demo “automatic_mask_generator_example.ipynb“ given by official. Have you fixed it?

Zeka-Fak avatar Nov 23 '23 10:11 Zeka-Fak

Same issue here when running the automatic_mask_generator_example.ipynb demo

mura94 avatar Nov 26 '23 20:11 mura94

@Lylain @mura94 I don't certainly know the reason, but it seems like when you set a smaller value to these 2 parameters "pred_iou_thresh" and "stability_score_thresh"(within the function of SamAutomaticMaskGenerator, their default value is 0.85 and 0.9, respectively) the model could generate some masks. These 2 parameters seem defined how strict the model should be when it judges whether a region should be treated as a mask or not. So smaller value means looser standard, and then you can get more masks.

Hippozhibos avatar Nov 28 '23 12:11 Hippozhibos

Hi @Hippozhibos what values did you use to make yours work? Have you compared with results from the web demo? I am trying to simulate those same results but to no avail

smandava98 avatar Jan 04 '24 09:01 smandava98

  1. I meet the same question by the func "SamAutomaticMaskGenerator" with "device:cuda".
  2. But, when the "device" changed from 'cuda' to 'cpu', the masks‘s length is not zero, it works well !!! I do not know the reason, but i am sure it's not caused by parameters "pred_iou_thresh" and "stability_score_thresh".

jiahuanLu avatar May 07 '24 02:05 jiahuanLu