很快就第2了哦

AI 技术的迭代,已经以天为单位。所以,如果你有什么好的想法,最好赶紧做,不然睡一觉可能就被抢先了。

SAM2~~_人工智能

这个被很多人看好的 idea 源于 Meta 两天前发布的「分割一切」AI 模型(Segment Anything Model,简称 SAM)。Meta 表示,「SAM 已经学会了关于物体的一般概念,可以为任何图像或视频中的任何物体生成 mask,甚至包括在训练过程中没有遇到过的物体和图像类型。SAM 足够通用,可以涵盖广泛的用例,并且可以在新的图像『领域』即开即用,无需额外的训练。」 

SAM2~~_github_02

这一模型的发布在计算机视觉领域引发轰动,预示着 CV 也将走向「一个全能基础模型统一某个(某些?全部?)任务」的道路。当然,大家对此早有预感,但没想到这一天来得如此之快。

比基础模型迭代更快的是研究社区「二创」的速度。论文才刚刚发布两天,几位国内工程师就基于此想出了新的点子并将其付诸实践,组建出了一个不仅可以「分割一切」,还能「检测一切」、「生成一切」的视觉工作流模型。

SAM2~~_python_03

具体来说,他们使用一个 SOTA 的 zero-shot 目标检测器(Grounding DINO)提取物体 box 和类别,然后输入给 SAM 模型出 mask,使得模型可以根据文本输入检测和分割任意物体。另外,他们还将其和 Stable Diffusion 结合做可控的图像编辑。

这个三合一模型项目名叫 Grounded Segment Anything,三种类型的模型既可以分开使用,也可以组合使用。

项目链接:https://github.com/IDEA-Research/Grounded-Segment-Anything

对于 Grounded Segment Anything 未来的用途,项目作者构想了几种可能:

  • 可控的、自动的图像生成,用于构建新的数据集;
  • 提供更强的基础模型与分割预训练;
  • 引入 GPT-4,进一步激发视觉大模型的潜力;
  • 一条自动标记图像(带 box 和 mask)并生成新图像的完整 pipeline;
  • ……

安装

要实现 SAM+Stable Diffusion 需要一些安装步骤。首先该项目要求 Python 3.8 以上版本,pytorch 1.7 以上版本,torchvision 0.8 以上版本,并安装相关依赖项。项目作者还建议安装支持 CUDA 的 PyTorch 和 TorchVision。

然后,按照如下代码安装 Segment Anything:

python -m pip install -e segment_anything

安装 GroundingDINO:

python -m pip install -e GroundingDINO

以下是可选依赖项,这些对于掩码后处理、以 COCO 格式保存掩码、example notebook 以及以 ONNX 格式导出模型是必需的。另外,该项目还需要 jupyter 来运行 example notebook。

pip install opencv-python pycocotools matplotlib onnxruntime onnx ipykernel

运行 GroundingDINO demo

下载 groundingdino 检查点:

cd Grounded-Segment-Anything
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth

运行 demo:

export CUDA_VISIBLE_DEVICES=0
python grounding_dino_demo.py \
  --config GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py \
  --grounded_checkpoint groundingdino_swint_ogc.pth \
  --input_image assets/demo1.jpg \
  --output_dir "outputs" \
  --box_threshold 0.3 \
  --text_threshold 0.25 \
  --text_prompt "bear" \
  --device "cuda"

模型预测可视化将保存在 output_dir 中,如下所示:

SAM2~~_人工智能_04

运行 Grounded-Segment-Anything Demo

下载 segment-anything 和 ground- dino 的检查点:

cd Grounded-Segment-Anything
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth

运行 demo:

export CUDA_VISIBLE_DEVICES=0
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/demo1.jpg \
  --output_dir "outputs" \
  --box_threshold 0.3 \
  --text_threshold 0.25 \
  --text_prompt "bear" \
  --device "cuda"

模型预测可视化将保存在 output_dir 中,如下所示:

SAM2~~_github_05

运行 Grounded-Segment-Anything + Inpainting Demo

CUDA_VISIBLE_DEVICES=0
python grounded_sam_inpainting_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/inpaint_demo.jpg \
  --output_dir "outputs" \
  --box_threshold 0.3 \
  --text_threshold 0.25 \
  --det_prompt "bench" \
  --inpaint_prompt "A sofa, high quality, detailed" \
  --device "cuda"

运行 Grounded-Segment-Anything + Inpainting Gradio APP

python gradio_app.py