Commit
·
ee32f46
1
Parent(s):
292d7ec
Update README.md
Browse files
README.md
CHANGED
@@ -35,32 +35,27 @@ fine-tuned versions on a task that interests you.
|
|
35 |
Here is how to use this model:
|
36 |
|
37 |
```python
|
38 |
-
import requests
|
39 |
import torch
|
40 |
from PIL import Image
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
|
|
|
52 |
with torch.no_grad():
|
53 |
outputs = model(**inputs)
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# you can pass them to processor for postprocessing
|
61 |
-
result = processor.post_process_panoptic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
|
62 |
-
# we refer to the demo notebooks for visualization (see "Resources" section in the Mask2Former docs)
|
63 |
-
predicted_panoptic_map = result["segmentation"]
|
64 |
```
|
65 |
|
66 |
For more code examples, we refer to the [documentation](https://huggingface.co/docs/transformers/master/en/model_doc/mask2former).
|
|
|
35 |
Here is how to use this model:
|
36 |
|
37 |
```python
|
|
|
38 |
import torch
|
39 |
from PIL import Image
|
40 |
+
import requests
|
41 |
+
from transformers import SamModel, SamProcessor
|
42 |
|
43 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
44 |
+
model = SamModel.from_pretrained("facebook/sam-vit-huge").to(device)
|
45 |
+
processor = SamProcessor.from_pretrained("facebook/sam-vit-huge")
|
46 |
|
47 |
+
img_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
48 |
+
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
|
49 |
+
input_points = [[[450, 600]]] # 2D location of a window in the image
|
50 |
|
51 |
+
inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to(device)
|
52 |
with torch.no_grad():
|
53 |
outputs = model(**inputs)
|
54 |
|
55 |
+
masks = processor.image_processor.post_process_masks(
|
56 |
+
outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu()
|
57 |
+
)
|
58 |
+
scores = outputs.iou_scores
|
|
|
|
|
|
|
|
|
|
|
59 |
```
|
60 |
|
61 |
For more code examples, we refer to the [documentation](https://huggingface.co/docs/transformers/master/en/model_doc/mask2former).
|