Views
No views yet
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 AutoProcessor, AutoModelForMaskGeneration
4
5# load the RobustSAM model and processor
6processor = AutoProcessor.from_pretrained("jadechoghari/robustsam-vit-large")
7model = AutoModelForMaskGeneration.from_pretrained("jadechoghari/robustsam-vit-large")
8
9# load an image from a url
10img_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
11raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
12
13# we define input points (2D localization of an object in the image)
14input_points = [[[450, 600]]] # example point
151# process the image and input points
2inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
3
4# generate masks using the model
5with torch.no_grad():
6 outputs = model(**inputs)
7masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
8scores = outputs.iou_scores
91024 points
which are all fed to the model.points_per_batch argument)1from transformers import pipeline
2
3# initialize the pipeline for mask generation
4generator = pipeline("mask-generation", model="jadechoghari/robustsam-vit-large", device=0, points_per_batch=256)
5
6image_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
7outputs = generator(image_url, points_per_batch=256)1import matplotlib.pyplot as plt
2from PIL import Image
3import numpy as np
4
5# simple function to display the mask
6def show_mask(mask, ax, random_color=False):
7 if random_color:
8 color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
9 else:
10 color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
11
12 # get the height and width from the mask
13 h, w = mask.shape[-2:]
14 mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
15 ax.imshow(mask_image)
16
17# display the original image
18plt.imshow(np.array(raw_image))
19ax = plt.gca()
20
21# loop through the masks and display each one
22for mask in outputs["masks"]:
23 show_mask(mask, ax=ax, random_color=True)
24
25plt.axis("off")
26
27# show the image with the masks
28plt.show()
![]() |
![]() |
![]() |
![]() |
1@inproceedings{chen2024robustsam,
2 title={RobustSAM: Segment Anything Robustly on Degraded Images},
3 author={Chen, Wei-Ting and Vong, Yu-Jiet and Kuo, Sy-Yen and Ma, Sizhou and Wang, Jian},
4 journal={CVPR},
5 year={2024}
6}