Views
No views yet

The formidable model size and demanding computational requirements of Segment Anything Model (SAM) have rendered it cumbersome for deployment on resource-constrained devices. Existing approaches for SAM compression typically involve training a new network from scratch, posing a challenging trade-off between compression costs and model performance. To address this issue, this paper introduces SlimSAM, a novel SAM compression method that achieves superior performance with remarkably low training costs. This is achieved by the efficient reuse of pre-trained SAMs through a unified pruning-distillation framework. To enhance knowledge inheritance from the original SAM, we employ an innovative alternate slimming strategy that partitions the compression process into a progressive procedure. Diverging from prior pruning techniques, we meticulously prune and distill decoupled model structures in an alternating fashion. Furthermore, a novel label-free pruning criterion is also proposed to align the pruning objective with the optimization target, thereby boosting the post-distillation after pruning. SlimSAM yields significant performance improvements while demanding over 10 times less training costs than any other existing methods. Even when compared to the original SAM-H, SlimSAM achieves approaching performance while reducing parameter counts to merely 0.9% (5.7M), MACs to 0.8% (21G), and requiring only 0.1% (10k) of the SAM training data.
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("nielsr/slimsam-77-uniform")
6processor = SamProcessor.from_pretrained("nielsr/slimsam-77-uniform")
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 window
11
12inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
13outputs = model(**inputs)
14masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
15scores = outputs.iou_scores1024 points
which are all fed to the model.points_per_batch argument)1from transformers import pipeline
2generator = pipeline(task="mask-generation", model="nielsr/slimsam-77-uniform", 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}
}
@misc{chen202301,
title={0.1% Data Makes Segment Anything Slim},
author={Zigeng Chen and Gongfan Fang and Xinyin Ma and Xinchao Wang},
year={2023},
eprint={2312.05284},
archivePrefix={arXiv},
primaryClass={cs.CV}
}