We implemente the segment anything with the PaddlePaddle framework. Segment Anything Model (SAM) is a new task, model, and dataset for image segmentation. It built a largest segmentation dataset to date (by far), with over 1 billion masks on 11M licensed and privacy respecting images. Further, SAM can produce high quality object masks from different types of prompts including points, boxes, masks and text. SAM has impressive zero-shot performance on a variety of tasks, even often competitive with or even superior to prior fully supervised results. However, the SAM model based on text prompt is not released at the moment. Therefore, we use a combination of SAM and CLIP to calculate the similarity between the output masks and text prompt. In this way, you can use text prompt to segment anything. In addition, we also implement SAM that can generate masks for all objects in whole image.
We provide the pretrained model parameters of PaddlePaddle format, including vit_b, vit_l, vit_h and vit_t for MobileSAM. For text prompt, we also provide the CLIP_ViT_B model parameters of PaddlePaddle format.
Install PaddlePaddle and relative environments based on the installation guide.
Install PaddleSeg based on the reference.
Clone the PaddleSeg reporitory:
git clone https://github.com/PaddlePaddle/PaddleSeg.git
cd PaddleSeg
pip install -r requirements.txt
pip install ftfy regex
cd contrib/SegmentAnything/
Download the example image to contrib/SegmentAnything/examples
and the vocab to contrib/SegmentAnything/
wget https://paddleseg.bj.bcebos.com/dygraph/demo/cityscapes_demo.png
wget https://bj.bcebos.com/paddleseg/dygraph/bpe_vocab_16e6/bpe_simple_vocab_16e6.txt.gz
Then, the file structure is as following:
PaddleSeg/contrib
├── SegmentAnything
│ ├── examples
│ │ └── cityscapes_demo.png
│ ├── segment_anything
│ ├── scripts
│ └── bpe_simple_vocab_16e6.txt.gz
In this step, we start a gradio service with the following scrip on local machine and you can try out our project with your own images. Based on this service, You can experience the ability to segment the whole image and segment the object based on text prompts.
Run the following script:
python scripts/text_to_sam_clip.py --model-type [vit_l/vit_b/vit_h/vit_t] # default is vit_h
Note:
vit_b
, vit_l
, vit_h
and vit_t
, represent vit_base, vit_large, vit_huge and vit_mobilesam. Large model is more accurate but slower. You can choose the suitable model size based on your device.CLIP Vit-B
model for extracting text and image features.SAM vit_h
needs 16G memory and costs around 10s to infer an image on V100.Open the webpage on your localhost: http://0.0.0.0:8078
Try it out by clear and upload the test image! Our example looks like:
You can run the following commands to produce masks from different types of prompts including points and boxes, as follow. The picture result will be saved to the output/
directory, with the image name the same as the input.
python scripts/promt_predict.py --input_path xxx.png --box_prompt 1050 370 1500 700 --model-type [vit_l/vit_b/vit_h] # default is vit_h
python scripts/promt_predict.py --input_path xxx.png --point_prompt 1200 450 --model-type [vit_l/vit_b/vit_h] # default is vit_h
python scripts/amg_paddle.py --model-type [vit_l/vit_b/vit_h/vit_t] # default is vit_h
Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alexander C. Berg, Wan-Yen Lo, Piotr Dollár, Ross Girshick. Segment Anything.
Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever Proceedings of the 38th International Conference on Machine Learning, PMLR 139:8748-8763, 2021. CLIP
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。