Views
No views yet

![]() | ![]() |
|---|
We introduce the Segment Anything (SA) project: a new task, model, and dataset for image segmentation. Using our efficient model in a data collection loop, we built the largest segmentation dataset to date (by far), with over 1 billion masks on 11M licensed and privacy respecting images. The model is designed and trained to be promptable, so it can transfer zero-shot to new image distributions and tasks. We evaluate its capabilities on numerous tasks and find that its zero-shot performance is impressive -- often competitive with or even superior to prior fully supervised results. We are releasing the Segment Anything Model (SAM) and corresponding dataset (SA-1B) of 1B masks and 11M images at https://segment-anything.com to foster research into foundation models for computer vision.
VisionEncoder: a VIT based image encoder. It computes the image embeddings using attention on patches of the image. Relative Positional Embedding is used.PromptEncoder: generates embeddings for points and bounding boxesMaskDecoder: a two-ways transformer which performs cross attention between the image embedding and the point embeddings (->) and between the point embeddings and the image embeddings. The outputs are fedNeck: predicts the output masks based on the contextualized masks produced by the MaskDecoder.1from PIL import Image
2import requests
3from transformers import SamModel, SamProcessor
4
5model = SamModel.from_pretrained("facebook/sam-vit-large")
6processor = SamProcessor.from_pretrained("facebook/sam-vit-large")
7
8img_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
9raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
10input_points = [[[450, 600]]] # 2D localization of a window1inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
2outputs = model(**inputs)
3masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
4scores = outputs.iou_scores1024 points
which are all fed to the model.points_per_batch argument)1from transformers import pipeline
2generator = pipeline("mask-generation", device = 0, points_per_batch = 256)
3image_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
4outputs = generator(image_url, points_per_batch = 256)1import matplotlib.pyplot as plt
2from PIL import Image
3import numpy as np
4
5def show_mask(mask, ax, random_color=False):
6 if random_color:
7 color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
8 else:
9 color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
10 h, w = mask.shape[-2:]
11 mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
12 ax.imshow(mask_image)
13
14
15plt.imshow(np.array(raw_image))
16ax = plt.gca()
17for mask in outputs["masks"]:
18 show_mask(mask, ax=ax, random_color=True)
19plt.axis("off")
20plt.show()@article{kirillov2023segany,
title={Segment Anything},
author={Kirillov, Alexander and Mintun, Eric and Ravi, Nikhila and Mao, Hanzi and Rolland, Chloe and Gustafson, Laura and Xiao, Tete and Whitehead, Spencer and Berg, Alexander C. and Lo, Wan-Yen and Doll{\'a}r, Piotr and Girshick, Ross},
journal={arXiv:2304.02643},
year={2023}
}