Update README.md
Browse files
README.md
CHANGED
@@ -2,7 +2,120 @@
|
|
2 |
license: apache-2.0
|
3 |
---
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
``` python
|
8 |
model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50").to("cuda")
|
@@ -15,4 +128,85 @@ inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to
|
|
15 |
outputs = model(**inputs)
|
16 |
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
|
17 |
scores = outputs.iou_scores
|
18 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
license: apache-2.0
|
3 |
---
|
4 |
|
5 |
+
# SlimSAM: 0.1% Data Makes Segment Anything Slim
|
6 |
+
<div align="center">
|
7 |
+
<img src="images/paper/intro.PNG" width="66%">
|
8 |
+
<img src="images/paper/everything.PNG" width="100%">
|
9 |
+
</div>
|
10 |
+
|
11 |
+
> **0.1% Data Makes Segment Anything Slim**
|
12 |
+
> [Zigeng Chen](https://github.com/czg1225), [Gongfan Fang](https://fangggf.github.io/), [Xinyin Ma](https://horseee.github.io/), [Xinchao Wang](https://sites.google.com/site/sitexinchaowang/)
|
13 |
+
> [Learning and Vision Lab](http://lv-nus.org/), National University of Singapore
|
14 |
+
> Paper: [[Arxiv]](https://arxiv.org/abs/2312.05284)
|
15 |
+
|
16 |
+
### Updates
|
17 |
+
* 🚀 **December 11, 2023**: Release the training code, inference code and pre-trained models for **SlimSAM**.
|
18 |
+
|
19 |
+
## Introduction
|
20 |
+
|
21 |
+
<div align="center">
|
22 |
+
<img src="images/paper/process.PNG" width="100%">
|
23 |
+
</div>
|
24 |
+
|
25 |
+
**SlimSAM** is a novel SAM compression method, which efficiently reuses pre-trained SAMs without the necessity for extensive retraining. 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.
|
26 |
+
|
27 |
+
![Frame](images/paper/frame.PNG?raw=true)
|
28 |
+
|
29 |
+
SlimSAM achieves approaching performance while reducing the parameter counts to **0.9\% (5.7M)**, MACs to **0.8\% (21G)**, and requiring mere **0.1\% (10k)** of the training data when compared to the original SAM-H. Extensive experiments demonstrate that our method realize significant superior performance while utilizing over **10 times** less training data when compared to other SAM compression methods.
|
30 |
+
|
31 |
+
## Visualization Results
|
32 |
+
|
33 |
+
Qualitative comparison of results obtained using point prompts, box prompts, and segment everything prompts are shown in the following section.
|
34 |
+
|
35 |
+
### Segment Everything Prompts
|
36 |
+
<div align="center">
|
37 |
+
<img src="images/paper/everything2.PNG" width="100%">
|
38 |
+
</div>
|
39 |
+
|
40 |
+
### Box Prompts and Point Prompts
|
41 |
+
<div align="center">
|
42 |
+
<img src="images/paper/prompt.PNG" width="100%">
|
43 |
+
</div>
|
44 |
+
|
45 |
+
|
46 |
+
## Quantitative Results
|
47 |
+
|
48 |
+
We conducted a comprehensive comparison encompassing performance, efficiency, and training costs with other SAM compression methods and structural pruning methods.
|
49 |
+
|
50 |
+
### Comparing with other SAM compression methods.
|
51 |
+
<div align="center">
|
52 |
+
<img src="images/paper/compare_tab1.PNG" width="100%">
|
53 |
+
</div>
|
54 |
+
|
55 |
+
### Comparing with other structural pruning methods.
|
56 |
+
<div align="center">
|
57 |
+
<img src="images/paper/compare_tab2.PNG" width="50%">
|
58 |
+
</div>
|
59 |
+
|
60 |
+
## Installation
|
61 |
+
|
62 |
+
The code requires `python>=3.8`, as well as `pytorch>=1.7` and `torchvision>=0.8`. Please follow the instructions [here](https://pytorch.org/get-started/locally/) to install both PyTorch and TorchVision dependencies. Installing both PyTorch and TorchVision with CUDA support is strongly recommended.
|
63 |
+
|
64 |
+
|
65 |
+
Install with
|
66 |
+
|
67 |
+
```
|
68 |
+
pip install -e .
|
69 |
+
```
|
70 |
+
|
71 |
+
The following optional dependencies are necessary for mask post-processing, saving masks in COCO format.
|
72 |
+
|
73 |
+
```
|
74 |
+
pip install opencv-python pycocotools matplotlib
|
75 |
+
```
|
76 |
+
|
77 |
+
## Dataset
|
78 |
+
We use the original SA-1B dataset in our code. See [here](https://ai.facebook.com/datasets/segment-anything/) for an overview of the datastet. The dataset can be downloaded [here](https://ai.facebook.com/datasets/segment-anything-downloads/).
|
79 |
+
|
80 |
+
The download dataset should be saved as:
|
81 |
+
|
82 |
+
```
|
83 |
+
<train_data_root>/
|
84 |
+
sa_xxxxxxx.jpg
|
85 |
+
sa_xxxxxxx.json
|
86 |
+
......
|
87 |
+
<val_data_root>/
|
88 |
+
sa_xxxxxxx.jpg
|
89 |
+
sa_xxxxxxx.json
|
90 |
+
......
|
91 |
+
```
|
92 |
+
|
93 |
+
|
94 |
+
To decode a mask in COCO RLE format into binary:
|
95 |
+
|
96 |
+
``` python
|
97 |
+
from pycocotools import mask as mask_utils
|
98 |
+
mask = mask_utils.decode(annotation["segmentation"])
|
99 |
+
```
|
100 |
+
|
101 |
+
See [here](https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/mask.py) for more instructions to manipulate masks stored in RLE format.
|
102 |
+
|
103 |
+
|
104 |
+
## <a name="Models"></a>Model Checkpoints
|
105 |
+
|
106 |
+
The base model of our method is available. To enhance collaboration with our dependency dectection algorithm, we have split the original image encoder's qkv layer into three distinct linear layers: q, k, and v.
|
107 |
+
<div align="center">
|
108 |
+
<img src="images/paper/split.PNG" width="70%">
|
109 |
+
</div>
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
Click the links below to download the checkpoints of orginal SAM-B.
|
114 |
+
|
115 |
+
- `SAM-B`: [SAM-B model.](https://drive.google.com/file/d/1CtcyOm4h9bXgBF8DEVWn3N7g9-3r4Xzz/view?usp=sharing)
|
116 |
+
|
117 |
+
|
118 |
+
Fast state_dict loading for local uniform pruning SlimSAM-50 model:
|
119 |
|
120 |
``` python
|
121 |
model = SamModel.from_pretrained("Zigeng/SlimSAM-uniform-50").to("cuda")
|
|
|
128 |
outputs = model(**inputs)
|
129 |
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
|
130 |
scores = outputs.iou_scores
|
131 |
+
```
|
132 |
+
|
133 |
+
|
134 |
+
## <a name="Inference"></a>Inference
|
135 |
+
|
136 |
+
First download [SlimSAM-50 model](https://drive.google.com/file/d/1iCN9IW0Su0Ud_fOFoQUnTdkC3bFveMND/view?usp=sharing) or [SlimSAM-77 model](https://drive.google.com/file/d/1L7LB6gHDzR-3D63pH9acD9E0Ul9_wMF-/view) for inference
|
137 |
+
|
138 |
+
|
139 |
+
We provide detailed instructions in 'inference.py' on how to use a range of prompts, including 'point' and 'box' and 'everything', for inference purposes.
|
140 |
+
|
141 |
+
```
|
142 |
+
CUDA_VISIBLE_DEVICES=0 python inference.py
|
143 |
+
```
|
144 |
+
|
145 |
+
## <a name="Train"></a>Train
|
146 |
+
|
147 |
+
First download a [SAM-B model](https://drive.google.com/file/d/1CtcyOm4h9bXgBF8DEVWn3N7g9-3r4Xzz/view?usp=sharing) into 'checkpoints/' as the base model.
|
148 |
+
|
149 |
+
### Step1: Embedding Pruning + Bottleneck Aligning ###
|
150 |
+
The model after step1 is saved as 'checkpoints/vit_b_slim_step1_.pth'
|
151 |
+
|
152 |
+
```
|
153 |
+
CUDA_VISIBLE_DEVICES=0 python prune_distill_step1.py --traindata_path <train_data_root> --valdata_path <val_data_root> --prune_ratio <pruning ratio> --epochs <training epochs>
|
154 |
+
```
|
155 |
+
|
156 |
+
### Step2: Bottleneck Pruning + Embedding Aligning ###
|
157 |
+
The model after step2 is saved as 'checkpoints/vit_b_slim_step2_.pth'
|
158 |
+
|
159 |
+
```
|
160 |
+
CUDA_VISIBLE_DEVICES=0 python prune_distill_step2.py --traindata_path <train_data_root> --valdata_path <val_data_root> --prune_ratio <pruning ratio> --epochs <training epochs> --model_path 'checkpoints/vit_b_slim_step1_.pth'
|
161 |
+
```
|
162 |
+
|
163 |
+
You can adjust the training settings to meet your specific requirements. While our method demonstrates impressive performance with just 10,000 training data, incorporating additional training data will further enhance the model's effectiveness
|
164 |
+
|
165 |
+
## BibTex of our SlimSAM
|
166 |
+
If you use SlimSAM in your research, please use the following BibTeX entry. Thank you!
|
167 |
+
|
168 |
+
```bibtex
|
169 |
+
@misc{chen202301,
|
170 |
+
title={0.1% Data Makes Segment Anything Slim},
|
171 |
+
author={Zigeng Chen and Gongfan Fang and Xinyin Ma and Xinchao Wang},
|
172 |
+
year={2023},
|
173 |
+
eprint={2312.05284},
|
174 |
+
archivePrefix={arXiv},
|
175 |
+
primaryClass={cs.CV}
|
176 |
+
}
|
177 |
+
```
|
178 |
+
|
179 |
+
## Acknowledgement
|
180 |
+
|
181 |
+
<details>
|
182 |
+
<summary>
|
183 |
+
<a href="https://github.com/facebookresearch/segment-anything">SAM</a> (Segment Anything) [<b>bib</b>]
|
184 |
+
</summary>
|
185 |
+
|
186 |
+
```bibtex
|
187 |
+
@article{kirillov2023segany,
|
188 |
+
title={Segment Anything},
|
189 |
+
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},
|
190 |
+
journal={arXiv:2304.02643},
|
191 |
+
year={2023}
|
192 |
+
}
|
193 |
+
```
|
194 |
+
</details>
|
195 |
+
|
196 |
+
|
197 |
+
|
198 |
+
<details>
|
199 |
+
<summary>
|
200 |
+
<a href="https://github.com/VainF/Torch-Pruning">Torch Pruning</a> (DepGraph: Towards Any Structural Pruning) [<b>bib</b>]
|
201 |
+
</summary>
|
202 |
+
|
203 |
+
```bibtex
|
204 |
+
@inproceedings{fang2023depgraph,
|
205 |
+
title={Depgraph: Towards any structural pruning},
|
206 |
+
author={Fang, Gongfan and Ma, Xinyin and Song, Mingli and Mi, Michael Bi and Wang, Xinchao},
|
207 |
+
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
|
208 |
+
pages={16091--16101},
|
209 |
+
year={2023}
|
210 |
+
}
|
211 |
+
```
|
212 |
+
</details>
|