Update README.md
Browse files
README.md
CHANGED
|
@@ -1,121 +1,71 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
tags:
|
| 4 |
-
- vision
|
| 5 |
-
---
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
The
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
```
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
## Automatic-Mask-Generation
|
| 74 |
-
|
| 75 |
-
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
|
| 76 |
-
which are all fed to the model.
|
| 77 |
-
|
| 78 |
-
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)
|
| 79 |
-
```python
|
| 80 |
-
from transformers import pipeline
|
| 81 |
-
generator = pipeline("mask-generation", device = 0, points_per_batch = 256)
|
| 82 |
-
image_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
| 83 |
-
outputs = generator(image_url, points_per_batch = 256)
|
| 84 |
-
```
|
| 85 |
-
Now to display the image:
|
| 86 |
-
```python
|
| 87 |
-
import matplotlib.pyplot as plt
|
| 88 |
-
from PIL import Image
|
| 89 |
-
import numpy as np
|
| 90 |
-
|
| 91 |
-
def show_mask(mask, ax, random_color=False):
|
| 92 |
-
if random_color:
|
| 93 |
-
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
|
| 94 |
-
else:
|
| 95 |
-
color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
|
| 96 |
-
h, w = mask.shape[-2:]
|
| 97 |
-
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
|
| 98 |
-
ax.imshow(mask_image)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
plt.imshow(np.array(raw_image))
|
| 102 |
-
ax = plt.gca()
|
| 103 |
-
for mask in outputs["masks"]:
|
| 104 |
-
show_mask(mask, ax=ax, random_color=True)
|
| 105 |
-
plt.axis("off")
|
| 106 |
-
plt.show()
|
| 107 |
-
```
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Citation
|
| 111 |
-
|
| 112 |
-
If you use this model, please use the following BibTeX entry.
|
| 113 |
-
|
| 114 |
-
```
|
| 115 |
-
@article{kirillov2023segany,
|
| 116 |
-
title={Segment Anything},
|
| 117 |
-
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},
|
| 118 |
-
journal={arXiv:2304.02643},
|
| 119 |
-
year={2023}
|
| 120 |
-
}
|
| 121 |
-
```
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- vision
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
## Prompted-Mask-Generation
|
| 11 |
+
|
| 12 |
+
```python
|
| 13 |
+
from PIL import Image
|
| 14 |
+
import requests
|
| 15 |
+
from transformers import SamModel, SamProcessor
|
| 16 |
+
|
| 17 |
+
model = SamModel.from_pretrained("facebook/sam-vit-base")
|
| 18 |
+
processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
|
| 19 |
+
|
| 20 |
+
img_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
| 21 |
+
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
|
| 22 |
+
input_points = [[[450, 600]]] # 2D localization of a window
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
```python
|
| 27 |
+
inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to("cuda")
|
| 28 |
+
outputs = model(**inputs)
|
| 29 |
+
masks = processor.image_processor.post_process_masks(outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu())
|
| 30 |
+
scores = outputs.iou_scores
|
| 31 |
+
```
|
| 32 |
+
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).
|
| 33 |
+
For more details, refer to this notebook, which shows a walk throught of how to use the model, with a visual example!
|
| 34 |
+
|
| 35 |
+
## Automatic-Mask-Generation
|
| 36 |
+
|
| 37 |
+
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
|
| 38 |
+
which are all fed to the model.
|
| 39 |
+
|
| 40 |
+
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)
|
| 41 |
+
```python
|
| 42 |
+
from transformers import pipeline
|
| 43 |
+
generator = pipeline("mask-generation", device = 0, points_per_batch = 256)
|
| 44 |
+
image_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
| 45 |
+
outputs = generator(image_url, points_per_batch = 256)
|
| 46 |
+
```
|
| 47 |
+
Now to display the image:
|
| 48 |
+
```python
|
| 49 |
+
import matplotlib.pyplot as plt
|
| 50 |
+
from PIL import Image
|
| 51 |
+
import numpy as np
|
| 52 |
+
|
| 53 |
+
def show_mask(mask, ax, random_color=False):
|
| 54 |
+
if random_color:
|
| 55 |
+
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
|
| 56 |
+
else:
|
| 57 |
+
color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
|
| 58 |
+
h, w = mask.shape[-2:]
|
| 59 |
+
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
|
| 60 |
+
ax.imshow(mask_image)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
plt.imshow(np.array(raw_image))
|
| 64 |
+
ax = plt.gca()
|
| 65 |
+
for mask in outputs["masks"]:
|
| 66 |
+
show_mask(mask, ax=ax, random_color=True)
|
| 67 |
+
plt.axis("off")
|
| 68 |
+
plt.show()
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|