RobustSAM: Segment Anything Robustly on Degraded Images (CVPR 2024 Highlight)
Model Card for ViT Large (ViT-L) version
Official repository for RobustSAM: Segment Anything Robustly on Degraded Images
Introduction
Segment Anything Model (SAM) has emerged as a transformative approach in image segmentation, acclaimed for its robust zero-shot segmentation capabilities and flexible prompting system. Nonetheless, its performance is challenged by images with degraded quality. Addressing this limitation, we propose the Robust Segment Anything Model (RobustSAM), which enhances SAM's performance on low-quality images while preserving its promptability and zero-shot generalization.
Our method leverages the pre-trained SAM model with only marginal parameter increments and computational requirements. The additional parameters of RobustSAM can be optimized within 30 hours on eight GPUs, demonstrating its feasibility and practicality for typical research laboratories. We also introduce the Robust-Seg dataset, a collection of 688K image-mask pairs with different degradations designed to train and evaluate our model optimally. Extensive experiments across various segmentation tasks and datasets confirm RobustSAM's superior performance, especially under zero-shot conditions, underscoring its potential for extensive real-world application. Additionally, our method has been shown to effectively improve the performance of SAM-based downstream tasks such as single image dehazing and deblurring.
Disclaimer: Content from
this model card has been written by the Hugging Face team, and parts of it were copy pasted from the original
SAM model card.
Model Details
The RobustSAM model is made up of 3 modules:
- The
VisionEncoder: a VIT based image encoder. It computes the image embeddings using attention on patches of the image. Relative Positional Embedding is used.
- The
PromptEncoder: generates embeddings for points and bounding boxes
- The
MaskDecoder: 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 fed
- The
Neck: predicts the output masks based on the contextualized masks produced by the MaskDecoder.
Usage
Below is an example on how to run mask generation given an image and a 2D point:
1import torch
2from PIL import Image
3import requests
4from transformers import RobustSamModel, RobustSamProcessor
5
6device = "cuda" if torch.cuda.is_available() else "cpu"
7model = RobustSamModel.from_pretrained("leolu030066/robustsam-vit-large").to(device)
8processor = RobustSamProcessor.from_pretrained("leolu030066/robustsam-vit-large")
9
10img_url = "https://huggingface.co/leolu030066/robustsam-vit-base/resolve/main/demo/demo_images/blur.jpg"
11raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
12input_points = [[[131, 233], [186, 84], [266, 54]]] # 2D location of a window in the image
13input_labels = [[1,1,1]]
14
15inputs = processor(images=np.array(raw_image), input_points=input_points, input_labels=input_labels,return_tensors="pt").to(device)
16with torch.no_grad():
17 output = model(multimask_output=False, return_logits=False,**inputs)
18 # output = hf_model(multimask_output=False, return_logits=False,clear = True,**inputs)
19
20masks = processor.image_processor.post_process_masks(
21 output.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu()
22)
23scores = output.iou_scores
Among other arguments to generate masks, you can pass 2D locations on the approximate position of your object of interest, a bounding box wrapping the object of interest (the format should be x, y coordinate of the top right and bottom left point of the bounding box), a segmentation mask. At this time of writing, passing a text as input is not supported by the official model according to
the official repository.
For more details, refer to this notebook, which shows a walk throught of how to use the model, with a visual example!
Visual Comparison
Reference
If you find this work useful, please consider citing us!
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}
Acknowledgements
We thank the authors of
SAM from which our repo is based off of.