mirror of
https://github.com/huggingface/transformers.git
synced 2025-07-06 22:30:09 +06:00

* cleanup * updates * more refactoring * make style * update inits * support other inputs in base * update based on review Co-authored-by: Nicolas Patry <patry.nicolas@gmail.com> * Update tests/pipelines/test_pipelines_automatic_mask_generation.py Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com> * update * fixup * TODO x and y to refactor, _h _w refactored here * update docstring * more nits * style on these * more doc fix * rename variables * update * updates * style * update * fix `_mask_to_rle_pytorch` * styling * fix ask to rle, wrong outputs * add device arg * update * more updates, fix tets * udpate * update docstrings * styling * fixup * add notebook on the docs * update orginal sizes * fix docstring * updat condition on point_per-batch * updates tests * fix CI test * extend is required, append does not work! * fixup * fix CI tests * whit pixels left * address doc comments * fix doc * slow pipeline tests * update auto init * add revision * make fixup * update p!ipoeline tag when calling tests * alphabeitcal order in inits * fix copies * last style nits * Apply suggestions from code review Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * reformat docstring * more reformat * address most of the comments * Update src/transformers/pipelines/mask_generation.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * final refactor * Update src/transformers/models/sam/image_processing_sam.py Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com> * fixup and fix slow tests * revert --------- Co-authored-by: Nicolas Patry <patry.nicolas@gmail.com> Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com> Co-authored-by: younesbelkada <younesbelkada@gmail.com> Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com> Co-authored-by: amyeroberts <22614925+amyeroberts@users.noreply.github.com>
100 lines
4.3 KiB
Plaintext
100 lines
4.3 KiB
Plaintext
<!--Copyright 2023 The HuggingFace Team. All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
|
|
the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
|
|
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
specific language governing permissions and limitations under the License.
|
|
-->
|
|
|
|
# SAM
|
|
|
|
## Overview
|
|
|
|
SAM (Segment Anything Model) was proposed in [Segment Anything](https://arxiv.org/pdf/2304.02643v1.pdf) by Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alex Berg, Wan-Yen Lo, Piotr Dollar, Ross Girshick.
|
|
|
|
The model can be used to predict segmentation masks of any object of interest given an input image.
|
|
|
|

|
|
|
|
The abstract from the paper is the following:
|
|
|
|
*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 \href{https://segment-anything.com}{https://segment-anything.com} to foster research into foundation models for computer vision.*
|
|
|
|
Tips:
|
|
|
|
- The model predicts binary masks that states the presence or not of the object of interest given an image.
|
|
- The model predicts much better results if input 2D points and/or input bounding boxes are provided
|
|
- You can prompt multiple points for the same image, and predict a single mask.
|
|
- Fine-tuning the model is not supported yet
|
|
- According to the paper, textual input should be also supported. However, at this time of writing this seems to be not supported according to [the official repository](https://github.com/facebookresearch/segment-anything/issues/4#issuecomment-1497626844).
|
|
|
|
|
|
This model was contributed by [ybelkada](https://huggingface.co/ybelkada) and [ArthurZ](https://huggingface.co/ArthurZ).
|
|
The original code can be found [here](https://github.com/facebookresearch/segment-anything).
|
|
|
|
Below is an example on how to run mask generation given an image and a 2D point:
|
|
|
|
```python
|
|
import torch
|
|
from PIL import Image
|
|
import requests
|
|
from transformers import SamModel, SamProcessor
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
model = SamModel.from_pretrained("facebook/sam-vit-huge").to(device)
|
|
processor = SamProcessor.from_pretrained("facebook/sam-vit-huge")
|
|
|
|
img_url = "https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png"
|
|
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert("RGB")
|
|
input_points = [[[450, 600]]] # 2D location of a window in the image
|
|
|
|
inputs = processor(raw_image, input_points=input_points, return_tensors="pt").to(device)
|
|
outputs = model(**inputs)
|
|
|
|
masks = processor.image_processor.post_process_masks(
|
|
outputs.pred_masks.cpu(), inputs["original_sizes"].cpu(), inputs["reshaped_input_sizes"].cpu()
|
|
)
|
|
scores = outputs.iou_scores
|
|
```
|
|
|
|
Resources:
|
|
|
|
- [Demo notebook](https://github.com/huggingface/notebooks/blob/main/examples/segment_anything.ipynb) for using the model
|
|
- [Demo notebook](https://github.com/huggingface/notebooks/blob/main/examples/automatic_mask_generation.ipynb) for using automatic mask generation pipeline.
|
|
|
|
## SamConfig
|
|
|
|
[[autodoc]] SamConfig
|
|
|
|
## SamVisionConfig
|
|
|
|
[[autodoc]] SamVisionConfig
|
|
|
|
## SamMaskDecoderConfig
|
|
|
|
[[autodoc]] SamMaskDecoderConfig
|
|
|
|
## SamPromptEncoderConfig
|
|
|
|
[[autodoc]] SamPromptEncoderConfig
|
|
|
|
|
|
## SamProcessor
|
|
|
|
[[autodoc]] SamProcessor
|
|
|
|
|
|
## SamImageProcessor
|
|
|
|
[[autodoc]] SamImageProcessor
|
|
|
|
|
|
## SamModel
|
|
|
|
[[autodoc]] SamModel
|
|
- forward
|