SAM+使用SAM应用数据集完成分割

news/2024/7/21 5:51:29 标签: 人工智能, pytorch, 图像处理

什么是SAM?

        SAM(Segment Anything Model)是由 Meta 的研究人员团队创建和训练的深度学习模型。在 Segment everything 研究论文中,SAM 被称为“基础模型”。

        基础模型是在大量数据上训练的机器学习模型(通常通过自监督或半监督学习),其目的是在更具体的任务上使用和重新训练。SAM 是一个预训练模型,旨在适应其他任务(特别是通过微调)。

sam安装

下载安装SAMhttps://github.com/facebookresearch/segment-anything

安装 Segment Anything:

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

或在本地克隆存储库并使用

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

Github页面里点击下载一个或者多个模型:

模型文件放到项目的目录即可。

H,L,B分别表示huge,large,base,从大到小。根据硬件能力选择合适的模型。

下列依次ViT-H SAM模型(vit_h),ViT-L SAM 模型(vit_1), ViT-B SAM 模型(vit_b)

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

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

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

 使用

 方法一:使用官方命令

建立input,output文件夹

在input中存放待分割的图片,output用作存放输出的mask。

在这我使用的是vit_h

python3 scripts/amg.py --checkpoint ./sam_vit_h_4b8939.pth --model-type default --input ./input.jpeg --output output

官方命令即执行amg.py文件,并传入了一些参数,当传入参数固定时可以直接写在amg.py文件中。

方法二:

# coding=gb2312
from segment_anything import SamPredictor, SamAutomaticMaskGenerator, sam_model_registry
import cv2
import numpy as np
import torch
import matplotlib.pyplot as plt
device = "cuda"
sam = sam_model_registry["default"](checkpoint="你下载的权重的位置")
#sam_vit_h_4b8939.pth 是预训练的默认权重,需要单独下载
sam.to(device=device)
mask_generator = SamAutomaticMaskGenerator(sam)

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)

    img = np.ones((sorted_anns[0]['segmentation'].shape[0], sorted_anns[0]['segmentation'].shape[1], 4))
    img[:,:,3] = 0
    for ann in sorted_anns:
        m = ann['segmentation']
        color_mask = np.concatenate([np.random.random(3), [0.35]])
        img[m] = color_mask
    ax.imshow(img)

image = cv2.imread('图片 位置.jpeg')
masks = mask_generator.generate(image)
plt.figure(figsize=(20,20))
plt.imshow(image)
show_anns(masks)
plt.axis('off')
plt.show() 

 

参考: 

https://zhuanlan.zhihu.com/p/627535252


http://www.niftyadmin.cn/n/5243530.html

相关文章

第四章 实验案例二:多表查询

实验案例二:多表查询 实验环境 某公司有一台已经安装了SQL Server 2016的服务器,class数据库中包含products表和 sales表.表中内容分别如图4.22和图4.23所示。 需求描述 1,在products表和sales 表中查询产品的名称.种类.成本、…

数据库基础DDL

一、字段属性 合适的字段类型对于高性能来说非常重要,基本原则如下:简单的类型占用资源更少;在可以正确存储数据的情况下,选最小的数据类型。 1、数据类型选择 整数类型 TINYINT、SMALLINT、MEDIUMINT、INT、BIGINT&#xff0…

2023_Spark_实验二十六:编写Shell模拟生成点击实时数据

引言:流式数据处理主要处理实时数据,由于实验教学过程中,每个同学无法拿到实时数据,因此我们开发shell脚本模拟实时数据生成,支持后续实验。 实验目的:通过开发模拟实时点击流shell脚本,模拟实时…

微信小程序:chooseimage从本地相册选择图片或使用相机拍照

文档 https://uniapp.dcloud.net.cn/api/media/image.html#chooseimage https://developers.weixin.qq.com/miniprogram/dev/api/media/image/wx.chooseImage.html 代码示例 const res await uni.chooseImage({count: 1, //默认9sizeType: [original, compressed], //可以…

机器学习之布谷鸟搜索算法(Cuckoo Search Algorithm,CSA)剖析

概念 布谷鸟搜索算法(Cuckoo Search Algorithm,CSA)是一种模拟自然界中布谷鸟种群行为的优化算法。这个算法的灵感来自布谷鸟的繁殖行为:布谷鸟会将自己的蛋放入别的鸟巢中,鸟主人可能会发现假蛋并将它们丢弃,而布谷鸟的蛋则有可能得以孵化。 这个算法的基本思想是模拟布…

数据分析中的绝地反击:如何解救一个陷入困境的数据模型

写在开头 大家好,欢迎来到我的数据探险之旅!今天我要给大家分享一段关于数据分析的奇幻故事,一个关于销售量预测模型的冒险。在这次旅程中,我遇到了一个强大的对手——预测准确率的困境,而我必须通过各种招数来解救这…

zookeeper1==zookeeper源码阅读,源码启动ZK集群

下载源码 Tags apache/zookeeper GitHub https://codeload.github.com/apache/zookeeper/zip/refs/tags/release-3.9.1 JDK8 MAVEN3.8.6 mvn -DskipTeststrue package 配置ZK1 zkServer.cmd中指出了启动类是 QuorumPeerMain QuorumPeer翻译成集群成员比较合理&#xf…

webrtc网之sip转webrtc

OpenSIP是一个开源的SIP(Session Initiation Protocol)服务器,它提供了一个可扩展的基础架构,用于建立、终止和管理VoIP(Voice over IP)通信会话。SIP是一种通信协议,用于建立、修改和终止多媒体…