Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
tags:
|
4 |
+
- slimsam
|
5 |
+
---
|
6 |
+
|
7 |
+
# Model Card for SlimSAM (compressed version of SAM = Segment Anything)
|
8 |
+
|
9 |
+
<p>
|
10 |
+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/sam-architecture.png" alt="Model architecture">
|
11 |
+
<em> Detailed architecture of Segment Anything Model (SAM).</em>
|
12 |
+
</p>
|
13 |
+
|
14 |
+
# Table of Contents
|
15 |
+
|
16 |
+
0. [TL;DR](#TL;DR)
|
17 |
+
1. [Model Details](#model-details)
|
18 |
+
2. [Usage](#usage)
|
19 |
+
3. [Citation](#citation)
|
20 |
+
|
21 |
+
# TL;DR
|
22 |
+
|
23 |
+
[Link to original repository](https://github.com/czg1225/SlimSAM)
|
24 |
+
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/sam-beancans.png" alt="Snow" width="600" height="600"> | <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/sam-dog-masks.png" alt="Forest" width="600" height="600"> | <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/sam-car-seg.png" alt="Mountains" width="600" height="600"> |
|
25 |
+
|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
|
26 |
+
|
27 |
+
The **Segment Anything Model (SAM)** produces high quality object masks from input prompts such as points or boxes, and it can be used to generate masks for all objects in an image. It has been trained on a [dataset](https://segment-anything.com/dataset/index.html) of 11 million images and 1.1 billion masks, and has strong zero-shot performance on a variety of segmentation tasks.
|
28 |
+
The abstract of the paper states:
|
29 |
+
|
30 |
+
> 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](https://segment-anything.com) to foster research into foundation models for computer vision.
|
31 |
+
|
32 |
+
**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](https://github.com/facebookresearch/segment-anything).
|
33 |
+
|
34 |
+
# Model Details
|
35 |
+
|
36 |
+
The SAM model is made up of 3 modules:
|
37 |
+
- The `VisionEncoder`: a VIT based image encoder. It computes the image embeddings using attention on patches of the image. Relative Positional Embedding is used.
|
38 |
+
- The `PromptEncoder`: generates embeddings for points and bounding boxes
|
39 |
+
- 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
|
40 |
+
- The `Neck`: predicts the output masks based on the contextualized masks produced by the `MaskDecoder`.
|
41 |
+
|
42 |
+
# Usage
|
43 |
+
|
44 |
+
## Prompted-Mask-Generation
|
45 |
+
|
46 |
+
```python
|
47 |
+
from PIL import Image
|
48 |
+
import requests
|
49 |
+
from transformers import SamModel, SamProcessor
|
50 |
+
|
51 |
+
model = SamModel.from_pretrained("nielsr/slimsam-50-uniform")
|
52 |
+
processor = SamProcessor.from_pretrained("nielsr/slimsam-50-uniform")
|
53 |
+
|
54 |
+
img_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
55 |
+
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
|
56 |
+
input_points = [[[450, 600]]] # 2D localization of a window
|
57 |
+
|
58 |
+
inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
|
59 |
+
outputs = model(**inputs)
|
60 |
+
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
|
61 |
+
scores = outputs.iou_scores
|
62 |
+
```
|
63 |
+
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](https://github.com/facebookresearch/segment-anything/issues/4#issuecomment-1497626844).
|
64 |
+
For more details, refer to this notebook, which shows a walk throught of how to use the model, with a visual example!
|
65 |
+
|
66 |
+
## Automatic-Mask-Generation
|
67 |
+
|
68 |
+
The model can be used for generating segmentation masks in a "zero-shot" fashion, given an input image. The model is automatically prompt with a grid of `1024` points
|
69 |
+
which are all fed to the model.
|
70 |
+
|
71 |
+
The pipeline is made for automatic mask generation. The following snippet demonstrates how easy you can run it (on any device! Simply feed the appropriate `points_per_batch` argument)
|
72 |
+
```python
|
73 |
+
from transformers import pipeline
|
74 |
+
generator = pipeline(task="mask-generation", model="nielsr/slimsam-50-uniform", device = 0, points_per_batch = 256)
|
75 |
+
image_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
76 |
+
outputs = generator(image_url, points_per_batch = 256)
|
77 |
+
```
|
78 |
+
Now to display the image:
|
79 |
+
```python
|
80 |
+
import matplotlib.pyplot as plt
|
81 |
+
from PIL import Image
|
82 |
+
import numpy as np
|
83 |
+
|
84 |
+
def show_mask(mask, ax, random_color=False):
|
85 |
+
if random_color:
|
86 |
+
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
|
87 |
+
else:
|
88 |
+
color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
|
89 |
+
h, w = mask.shape[-2:]
|
90 |
+
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
|
91 |
+
ax.imshow(mask_image)
|
92 |
+
|
93 |
+
|
94 |
+
plt.imshow(np.array(raw_image))
|
95 |
+
ax = plt.gca()
|
96 |
+
for mask in outputs["masks"]:
|
97 |
+
show_mask(mask, ax=ax, random_color=True)
|
98 |
+
plt.axis("off")
|
99 |
+
plt.show()
|
100 |
+
```
|
101 |
+
|
102 |
+
# Citation
|
103 |
+
|
104 |
+
If you use this model, please use the following BibTeX entry.
|
105 |
+
|
106 |
+
```
|
107 |
+
@article{kirillov2023segany,
|
108 |
+
title={Segment Anything},
|
109 |
+
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},
|
110 |
+
journal={arXiv:2304.02643},
|
111 |
+
year={2023}
|
112 |
+
}
|
113 |
+
@misc{chen202301,
|
114 |
+
title={0.1% Data Makes Segment Anything Slim},
|
115 |
+
author={Zigeng Chen and Gongfan Fang and Xinyin Ma and Xinchao Wang},
|
116 |
+
year={2023},
|
117 |
+
eprint={2312.05284},
|
118 |
+
archivePrefix={arXiv},
|
119 |
+
primaryClass={cs.CV}
|
120 |
+
}
|
121 |
+
```
|