【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

这篇具有很好参考价值的文章主要介绍了【图像分割】Meta分割一切(SAM)模型环境配置和使用教程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

注意:python>=3.8, pytorch>=1.7,torchvision>=0.8

Feel free to ask any question. 遇到问题欢迎评论区讨论.

官方教程:

https://github.com/facebookresearch/segment-anything

1 环境配置

1.1 安装主要库:

(1)pip:

有可能出现错误,需要配置好Git。

pip install git+https://github.com/facebookresearch/segment-anything.git

(2)本地安装:

有可能出现错误,需要配置好Git。

git clone git@github.com:facebookresearch/segment-anything.git
cd segment-anything; pip install -e .

(3)手动下载+手动本地安装:

【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

 zip文件:

链接:https://pan.baidu.com/s/1dQ--kTTJab5eloKm6nMYrg 
提取码:1234 

解压后运行: 

cd segment-anything-main
pip install -e .

1.2 安装依赖库:

pip install opencv-python pycocotools matplotlib onnxruntime onnx

matplotlib 3.7.1和3.7.0可能报错

如果报错:pip install matplotlib==3.6.2

1.3 下载权重文件:

下载三个权重文件中的一个,我用的第一个。

  • default or vit_h: ViT-H SAM model.
  • vit_l: ViT-L SAM model.
  • vit_b: ViT-B SAM model.

 如果下载过慢:

链接:https://pan.baidu.com/s/11wZUcjYWNL6kxOH5MFGB-g 
提取码:1234 

2 使用教程

2.1 根据在图片上选择的点扣出物体

原始图像:

【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

 导入依赖库和展示相关的函数:

import cv2
import matplotlib.pyplot as plt
import numpy as np
from segment_anything import sam_model_registry, SamPredictor

def show_mask(mask, ax, random_color=False):
    if random_color:
        color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
    else:
        color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
    h, w = mask.shape[-2:]
    mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
    ax.imshow(mask_image)


def show_points(coords, labels, ax, marker_size=375):
    pos_points = coords[labels == 1]
    neg_points = coords[labels == 0]
    ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white',
               linewidth=1.25)
    ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white',
               linewidth=1.25)

确定使用的权重文件位置和是否使用cuda等:

sam_checkpoint = "F:\sam_vit_h_4b8939.pth"
device = "cuda"
model_type = "default"

模型实例化:

sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
sam.to(device=device)
predictor = SamPredictor(sam)

读取图像并选择抠图点:

image = cv2.imread(r"F:\Dataset\Tomato_Appearance\Tomato_Xishi\images\xs_1.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

predictor.set_image(image)

input_point = np.array([[1600, 1000]])
input_label = np.array([1])

plt.figure(figsize=(10,10))
plt.imshow(image)
show_points(input_point, input_label, plt.gca())
plt.axis('on')
plt.show()

【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

 扣取图像(会同时提供多个扣取结果):

masks, scores, logits = predictor.predict(
    point_coords=input_point,
    point_labels=input_label,
    multimask_output=True,
)

# 遍历读取每个扣出的结果
for i, (mask, score) in enumerate(zip(masks, scores)):
    plt.figure(figsize=(10,10))
    plt.imshow(image)
    show_mask(mask, plt.gca())
    show_points(input_point, input_label, plt.gca())
    plt.title(f"Mask {i+1}, Score: {score:.3f}", fontsize=18)
    plt.axis('off')
    plt.show()

     【图像分割】Meta分割一切(SAM)模型环境配置和使用教程【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

 尝试扣取其他位置:

【图像分割】Meta分割一切(SAM)模型环境配置和使用教程【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

 【图像分割】Meta分割一切(SAM)模型环境配置和使用教程【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

2.2 扣取图像中的所有物体

官方教程:

https://github.com/facebookresearch/segment-anything/blob/main/notebooks/automatic_mask_generator_example.ipynb

依赖库和函数导入:

from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor
import cv2
import matplotlib.pyplot as plt
import numpy as np

def show_anns(anns):
    if len(anns) == 0:
        return
    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)
    ax = plt.gca()
    ax.set_autoscale_on(False)
    polygons = []
    color = []
    for ann in sorted_anns:
        m = ann['segmentation']
        img = np.ones((m.shape[0], m.shape[1], 3))
        color_mask = np.random.random((1, 3)).tolist()[0]
        for i in range(3):
            img[:,:,i] = color_mask[i]
        ax.imshow(np.dstack((img, m*0.35)))

读取图片:

image = cv2.imread(r"F:\Dataset\Tomato_Appearance\Tomato_Xishi\images\xs_1.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

实例化模型:

sam_checkpoint = "F:\sam_vit_h_4b8939.pth"
model_type = "default"
device = "cuda"

sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
sam.to(device=device)

 分割并展示(速度有点慢):

mask_generator = SamAutomaticMaskGenerator(sam)
masks = mask_generator.generate(image)

plt.figure(figsize=(20,20))
plt.imshow(image)
show_anns(masks)
plt.axis('off')
plt.show()

【图像分割】Meta分割一切(SAM)模型环境配置和使用教程【图像分割】Meta分割一切(SAM)模型环境配置和使用教程

2.3 根据文字扣取物体

配置另外一个库:

https://github.com/IDEA-Research/Grounded-Segment-Anything

配置教程:

【图像分割】Grounded Segment Anything根据文字自动画框或分割环境配置和基本使用教程_Father_of_Python的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-411083.html

到了这里,关于【图像分割】Meta分割一切(SAM)模型环境配置和使用教程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【计算机视觉 | 语义分割】OVSeg:分割一切后,SAM又能分辨类别了,Meta/UTAustin提出全新开放类分割模型

    前几日,Meta 推出了「分割一切」AI 模型 Segment Anything,令网友直呼 CV 不存在了?! 而在另一篇被 CVPR 2023 收录的论文中,Meta、UTAustin 联合提出了新的开放语言风格模型(open-vocabulary segmentation, OVSeg),它能让 Segment Anything 模型知道所要分隔的类别。 论文地址: 从效果上来看

    2024年02月12日
    浏览(48)
  • SAM - 分割一切图像【AI大模型】

    如果你认为 AI 领域已经通过 ChatGPT、GPT4 和 Stable Diffusion 快速发展,那么请系好安全带,为 AI 的下一个突破性创新做好准备。 推荐:用 NSDT场景设计器 快速搭建3D场景。 Meta 的 FAIR 实验室刚刚发布了 Segment Anything Model (SAM),这是一种最先进的图像分割模型,旨在改变计算机视

    2023年04月21日
    浏览(35)
  • 【Meta-AI】Sam-分割一切 测试

    ​ 【什么是 SAM】   近日,Meta AI在官网发布了基础模型 Segment Anything Model(SAM)并开源,其本质是用GPT的方式(基于Transform 模型架构)让计算机具备理解了图像里面的一个个“对象”的通用能力。SAM模型建立了一个可以接受文本提示、基于海量数据(603138)训练而获得泛化能力

    2024年02月12日
    浏览(29)
  • 【CV大模型SAM(Segment-Anything)】真是太强大了,分割一切的SAM大模型使用方法:可通过不同的提示得到想要的分割目标

    本文主要介绍SAM模型的使用方法:如何使用不同的提示进行目标分割。而且该模型在CPU的环境下就可以快速运行,真心不错~,赶紧来试试吧 关于Segment-Anything模型的 相关代码、论文PDF、预训练模型、使用方法 等,我都已打包好,供需要的小伙伴交流研究, 获取方式如下 : 关

    2023年04月18日
    浏览(33)
  • Segment Anything Model (SAM)——分割一切,具有预测提示输入的图像分割实践

    不得不说,最近的AI技术圈很火热,前面的风头大都是chatGPT的,自从前提Meta发布了可以分割一切的CV大模型之后,CV圈也热起来了,昨天只是初步了解了一下SAM,然后写了一篇基础介绍说明的博客,早上一大早起来已经有2k左右的阅读量了。  我果断跑去官方项目地址看下:

    2023年04月19日
    浏览(52)
  • SAM 模型真的是强悍到可以“分割一切”了吗?

    关注公众号,发现CV技术之美 上周,Meta AI发布了 Segment Anything Model(SAM)—— 第一个图像分割基础模型。很多计算机视觉从业者惊呼“这下CV真的不存在了,快跑!”。但是SAM 模型真的是强悍到可以“分割一切”了吗?它在哪些场景或任务中还不能较好地驾驭呢? 研究社区

    2024年02月06日
    浏览(45)
  • 【论文阅读】Segment Anything(SAM)——可分割一切的CV大模型

    【前言】随着ChatGPT席卷自然语言处理,Facebook凭借着Segment Anything在CV圈也算扳回一城。迄今为止,github的star已经超过3万,火的可谓一塌糊涂。作为AI菜鸟,可不得自己爬到巨人肩膀上瞅一瞅~ 论文地址:https://arxiv.org/abs/2304.02643 代码地址:GitHub - facebookresearch/segment-anything: T

    2024年02月15日
    浏览(35)
  • 计算机视觉:比SAM快50倍的分割一切视觉模型FastSAM

    目录 引言 1 FastSAM介绍 1.1 FastSAM诞生 1.2 模型算法 1.3 实验结果 2 FastSAM运行环境构建 2.1 conda环境构建 2.2 运行环境安装 2.3 模型下载 3 FastSAM运行 3.1 命令行运行 3.1.1 Everything mode  3.1.2 Text prompt 3.1.3 Box prompt (xywh) 3.1.4 Points prompt  3.2 通过代码调用 4 总结 MetaAI提出的能够“分割一切

    2024年02月11日
    浏览(34)
  • Meta AI 开源万物可分割 AI 模型(SAM)

    4 月 6 日,根据 Meta AI 官方博客,Meta AI 宣布推出了一个 AI 模型 Segment Anything Model(SAM,分割一切模型)。据介绍,该模型能够根据文本指令等方式实现图像分割,而且万物皆可识别和一键抠图。 github源码地址:facebookresearch/segment-anything 官方网站体验地址:segment-anything.com/

    2023年04月11日
    浏览(34)
  • Segment Anything Model (SAM)——卷起来了,那个号称分割一切的CV大模型他来了

    最近每天打开微信看到10个公众号里面差不多有11个都在各种玩赚chatGPT,每个都在说是各种大好风口,哎,看得眼睛都是累的。 今天下午无意间看到Meta发布了一款号称能分割一切的CV大模型,CV圈也开始卷起来,今年各种大模型要爆发了感觉。 吃瓜群众满怀好奇,点开了解一

    2023年04月10日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包