facebookresearch / segment-anything

The repository provides code for running inference with the SegmentAnything Model (SAM), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Apache License 2.0
47.5k stars 5.62k forks source link

How to get one mask per image #659

Closed hanjoonwon closed 9 months ago

hanjoonwon commented 10 months ago

I ran it with my current images and it seems to be storing too many masks for one image and is inaccurate. I want a foreground mask image so I need one mask for each image, how do I modify the code? my image https://github.com/NVlabs/instant-ngp/tree/master/data/nerf/fox/images

pinguo-huxiaohe commented 10 months ago

multimask_output: bool = False can return one mask

hanjoonwon commented 9 months ago

multimask_output: bool = False can return one mask

thank you :) I was wondering where the multimask option is, it's not in amg.py or is it something you add in the executable script?

pinguo-huxiaohe commented 9 months ago

source code in SAM: from segment_anything import sam_model_registry, SamPredictor sam_checkpoint = "sam_vit_h_4b8939.pth" model_type = "vit_h" device = "cuda" sam = sam_model_registrymodel_type sam.to(device=device)

predictor = SamPredictor(sam) masks, scores, logits = predictor.predict( point_coords=input_point, point_labels=input_label, multimask_output=True, )

the best single mask can be chosen by picking the one with the highest score returned in scores. Based on my experience, if you want to use 'multimask_output=True' and get the mask you want, you should give more prompts , such as points or boxs

pinguo-huxiaohe commented 9 months ago

more info:https://github.com/facebookresearch/segment-anything/blob/main/notebooks/predictor_example.ipynb

hanjoonwon commented 9 months ago

more info:https://github.com/facebookresearch/segment-anything/blob/main/notebooks/predictor_example.ipynb

thanks! Not directly applicable in the ubuntu anaconda environment, but should use notebooks.

pinguo-huxiaohe commented 9 months ago

it can use in anaconda, download the source code and weights, " import cv2 import numpy from segment_anything import SamPredictor, sam_model_registry

sam = sam_model_registry"vit_h"

sam = sam_model_registry"vit_l"

sam = sam_model_registry"vit_b" predictor = SamPredictor(sam)

image_data = cv2.imread( '...') predictor.set_image(image_data) input_point = numpy.array([[500, 375]]) input_label = numpy.array([1]) masks, scores, logits = predictor.predict( point_coords=input_point, point_labels=input_label, multimask_output=True, ) "

hanjoonwon commented 9 months ago

it can use in anaconda, download the source code and weights, " import cv2 import numpy from segment_anything import SamPredictor, sam_model_registry

sam = sam_model_registry"vit_h"

sam = sam_model_registry"vit_l"

sam = sam_model_registry"vit_b" predictor = SamPredictor(sam)

image_data = cv2.imread( '...') predictor.set_image(image_data) input_point = numpy.array([[500, 375]]) input_label = numpy.array([1]) masks, scores, logits = predictor.predict( point_coords=input_point, point_labels=input_label, multimask_output=True, ) "

thank you so much!