视频介绍:视频地址:https://www.bilibili.com/video/BV11s4y1N7Hv/
Github项目主页:https://github.com/facebookresearch/segment-anything
论文下载地址:https://arxiv.org/abs/2304.02643 { [Paper
] [Project
] [Demo
] [Dataset
] [Blog
] [BibTeX
] }
打开这里自己可以尝试:https://segment-anything.com/demo
我的尝试结果:(左边是分割图,右边是原图)
以下内容来自Github上的项目介绍
Segment Anything
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
[Paper
] [Project
] [Demo
] [Dataset
] [Blog
] [BibTeX
]
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 of 11 million images and 1.1 billion masks, and has strong zero-shot performance on a variety of segmentation tasks.
Installation
The code requires python>=3.8
, as well as pytorch>=1.7
and torchvision>=0.8
. Please follow the instructions here to install both PyTorch and TorchVision dependencies. Installing both PyTorch and TorchVision with CUDA support is strongly recommended.
Install Segment Anything:
1 2 |
pip install git https://github.com/facebookresearch/segment-anything.git |
or clone the repository locally and install with
1 2 3 |
git clone git@github.com:facebookresearch/segment-anything.git cd segment-anything; pip install -e . |
The following optional dependencies are necessary for mask post-processing, saving masks in COCO format, the example notebooks, and exporting the model in ONNX format. jupyter
is also required to run the example notebooks.
1 2 |
pip install opencv-python pycocotools matplotlib onnxruntime onnx |
Getting Started
First download a model checkpoint. Then the model can be used in just a few lines to get masks from a given prompt:
1 2 3 4 5 6 |
from segment_anything import SamPredictor, sam_model_registry sam = sam_model_registry[""](checkpoint="<path/to/checkpoint>") predictor = SamPredictor(sam) predictor.set_image() masks, _, _ = predictor.predict() |
or generate masks for an entire image:
1 2 3 4 5 |
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry sam = sam_model_registry[""](checkpoint="<path/to/checkpoint>") mask_generator = SamAutomaticMaskGenerator(sam) masks = mask_generator.generate() |
Additionally, masks can be generated for images from the command line:
1 2 |
python scripts/amg.py --checkpoint <path/to/checkpoint> --model-type --input --output <path/to/output> |
See the examples notebooks on using SAM with prompts and automatically generating masks for more details.