【图像分割】Grounded Segment Anything根据文字自动画框或分割环境配置和基本使用教程

1 环境配置  

要求:python>=3.8, pytorch>=1.7, torchvision>=0.8

官方地址:https://github.com/IDEA-Research/Grounded-Segment-AnythingMarrying Grounding DINO with Segment Anything & Stable Diffusion & BLIP & Whisper & ChatBot - Automatically Detect , Segment and Generate Anything with Image, Text, and Audio Inputs - GitHub - IDEA-Research/Grounded-Segment-Anything: Marrying Grounding DINO with Segment Anything & Stable Diffusion & BLIP & Whisper & ChatBot - Automatically Detect , Segment and Generate Anything with Image, Text, and Audio Inputshttps://github.com/IDEA-Research/Grounded-Segment-Anything

最简单的方法是直接Git,Git环境配好的直接Clone就好了,如果没有,请按照下面的流程。 

下面 1.1 和 1.2 部分的下载好的文件,下载解压后可以跳过1.1和1.2:

注意:官方Github库每天都在更新,建议下载下面的压缩包以防报错。

链接:https://pan.baidu.com/s/1u-qZK03wcn0dye_q_pL5zA 
提取码:6666 

1.1 下载Grounded Segment Anything库

 下载后解压。

1.2 下载引用的库

 GitHub - BingfengYan/VISAM at d7c38233882ff9d34d5cbecb8495e175e4dffc8cCombining "segment-anything" with MOT, it create the era of "MOTS" - GitHub - BingfengYan/VISAM at d7c38233882ff9d34d5cbecb8495e175e4dffc8chttps://github.com/BingfengYan/VISAM/tree/d7c38233882ff9d34d5cbecb8495e175e4dffc8c

GitHub - linjing7/grounded-sam-osx at 6688b036c7856a302f9315bb16864d66fb2cdadeSubmodule for Grounded-SAM. Contribute to linjing7/grounded-sam-osx development by creating an account on GitHub.https://github.com/linjing7/grounded-sam-osx/tree/6688b036c7856a302f9315bb16864d66fb2cdade

分别手动下载这两个引用的库,并保存在Grounded-Segment-Anything文件夹中所对应的位置。

 1.3 使用pip进行安装

(1)安装segment_anything:

python -m pip install -e segment_anything

参考: 

【图像分割】Meta分割一切(SAM)模型环境配置和使用教程_Father_of_Python的博客-CSDN博客

 (2)安装GroundingDINO:

在第1部分中下载好的目录下运行

注意:如果pip安装GroundingDIN失败,大概率电脑的C++有问题或者版本过低。

python -m pip install -e GroundingDINO

  (3)安装diffusers:

 pip install --upgrade diffusers[torch]

   (4)安装grounded-sam-osx:

注意:需要下载好Bash

参考:

windows下安装git和gitbash安装教程_64-bit git for windows setup._星光路人的博客-CSDN博客windos上git安装,git bash安装https://blog.csdn.net/u010348546/article/details/124280236?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168188651216800227468785%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168188651216800227468785&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-3-124280236-null-null.142%5Ev84%5Einsert_down1,239%5Ev2%5Einsert_chatgpt&utm_term=git%20bash%20windows&spm=1018.2226.3001.4187

cd grounded-sam-osx
bash install.sh

    (5)安装其他依赖:

 pip install opencv-python pycocotools matplotlib onnxruntime onnx ipykernel

  1.4 下载权重文件

将下列下载好的权重文件放在Grounded-Segment-Anything目录下。

权重文件1:

https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth

权重文件2:

https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth

如果下载速度慢:

链接:https://pan.baidu.com/s/1T2Bm4hWpkwEUAX5lKZnM0g 
提取码:6666 
链接:https://pan.baidu.com/s/1UJ8GjXHQhOD_ZMnzUkSSwg 
提取码:6666 

2 根据文字自动画框

在解压好的目录下运行:

python grounding_dino_demo.py --config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py --grounded_checkpoint groundingdino_swint_ogc.pth --input_image assets/xs_7.jpg --output_dir "outputs" --box_threshold 0.3 --text_threshold 0.25 --text_prompt "tomato" --device "cuda"

注意:我指定画框的是tomato(番茄)

效果展示:

 2 根据文字自动画框并分割

 在目录下运行:

python grounded_sam_demo.py --config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py --grounded_checkpoint groundingdino_swint_ogc.pth --sam_checkpoint sam_vit_h_4b8939.pth --input_image assets/xs_7.jpg --output_dir "outputs" --box_threshold 0.3 --text_threshold 0.25 --text_prompt "tomato" --device "cuda"

注意:我指定画框的是tomato(番茄)

效果展示:

生成三个文件(包含一个Json文件):

 3 APP运行

python gradio_app.py

### 关于 Grounded-Segment-Anything (Grounded-SAM) 模型 #### 功能概述 Grounded-Segment-Anything 结合了多个强大的开源工具,包括 **Segment Anything Model (SAM)** **GroundingDINO**。它不仅能够自动检测分割图像中的对象,还支持通过文本输入的方式指定特定的目标进行分割[^2]。此外,该项目还可以与其他模型集成,例如 Stable Diffusion Recognize Anything,从而扩展其应用场景。 #### 项目目录结构及说明 项目的官方仓库提供了详细的文档脚本,帮助开发者快速上手。以下是主要的目录及其用途: - `grounded_sam/`: 存放核心代码逻辑,实现了 SAM GroundingDINO 的融合。 - `weights/`: 预训练权重文件存储位置,其中包含 SAM 的预训练模型权重文件链接[^5]。 - `examples/`: 提供了一些示例脚本,展示如何使用该框架完成不同的任务。 - `requirements.txt`: 列出了依赖项列表,便于安装环境。 可以通过访问项目主页获取更详尽的信息:[Grounded-Segment-Anything](https://gitcode.com/gh_mirrors/gr/Grounded-Segment-Anything)[^3]。 --- #### 安装与配置指南 为了成功部署 Grounded-SAM,需按照以下方法准备开发环境并加载必要的资源: 1. 创建虚拟环境并激活: ```bash python -m venv env source env/bin/activate ``` 2. 安装所需库: ```bash pip install torch torchvision opencv-python-headless numpy transformers ``` 3. 下载预训练权重文件至本地路径下(如 `./weights` 文件夹),具体可参考提供的下载地址。 4. 运行测试脚本来验证安装是否正常工作。如果遇到性能瓶颈,则可能是由于硬件资源不足引起[^4]。 --- #### 实现方式详解 Grounded-SAM 主要分为以下几个模块来实现目标检测与分割功能: 1. **文本到框映射**: 借助 GroundingDINO 将自然语言描述转化为对应的边界框坐标。 2. **实例级掩码生成**: 调用 Segment Anything Model 来基于上述得到的区域建议生产精确像素级别的掩膜。 3. (可选)进一步处理阶段: 如果涉及艺术创作等领域需求的话,可以引入额外组件比如 Style Transfer Networks 对结果做美化调整等操作。 下面给出一段简单的 Python 示例代码用于调用此 API 执行基本的任务: ```python from grounded_sam import SamPredictor, build_grounding_dino_model import cv2 import matplotlib.pyplot as plt # 初始化 DINO SAM predictor dino_model = build_grounding_dino_model() sam_predictor = SamPredictor() image_path = 'example.jpg' text_prompt = "a dog" # 加载图片 image_bgr = cv2.imread(image_path) image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) # 获取 bounding box from text prompt via grounding dino boxes = dino_model.predict_with_caption(image=image_rgb, caption=text_prompt) # 使用 sam predictor 得到 mask for box in boxes: masks, _, _ = sam_predictor.set_image_and_predict_mask(box=box, image=image_rgb) # 可视化最终效果 plt.figure(figsize=(10, 10)) plt.imshow(masks.sum(axis=-1), cmap='gray') plt.axis('off') plt.show() ``` --- #### 性能优化技巧 对于希望提高 Mobile Segment Anything 表现的情况,量化技术是一个值得尝试的方向。例如采用 PyTorch 中内置函数对线性层部分实施动态量化以减少内存占用同时维持较高精度水平[^1]: ```python import torch.nn as nn from torch.quantization import quantize_dynamic model = ... # your original model instance here quantized_model = quantize_dynamic(model=model, qconfig_spec={nn.Linear}, dtype=torch.qint8) ``` 注意这仅适用于某些类型的神经网络架构;实际应用前应充分评估转换后的质量损失情况。 ---
评论 102
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WageningenUR

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值