位置: IT常识 - 正文

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

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

推荐整理分享【图像分割】Meta分割一切(SAM)模型环境配置和使用教程(图像分割最新算法),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:图像分割ncut,图像分割csdn,图像分割unet,图像分割实现,图像分割实现,图像分割otsu,图像分割otsu,图像分割miou,内容如对您有帮助,希望把文章链接给更多的朋友!

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

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

官方教程:

https://github.com/facebookresearch/segment-anything1 环境配置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.gitcd segment-anything; pip install -e .

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

 zip文件:

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

解压后运行: 

cd segment-anything-mainpip 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 cv2import matplotlib.pyplot as pltimport numpy as npfrom segment_anything import sam_model_registry, SamPredictordef 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()

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

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()

     

 尝试扣取其他位置:

 

2.2 扣取图像中的所有物体

官方教程:

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

依赖库和函数导入:

from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictorimport cv2import matplotlib.pyplot as pltimport numpy as npdef 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()

2.3 根据文字扣取物体

配置另外一个库:

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

后续更新细节

本文链接地址:https://www.jiuchutong.com/zhishi/288855.html 转载请保留说明!

上一篇:【Java基础】一个Java文件可以有多个类(外部类、内部类)(java基础教程)

下一篇:被翡翠湾环绕的芬尼特岛,太浩湖,加利福尼亚 (© Rachid Dahnoun/Tandem Stills + Motion)(翡翠湾攻略)

  • 增值税一般纳税人税率是多少?
  • 接受捐赠的增值税处理
  • 成本费用总额占营业收入比重
  • 合并报表中怎么抵消投资性房地产
  • 实际发生应收账款坏账时的会计处理
  • 工业企业生产成本包括哪些
  • 劳务报酬所得怎么扣税
  • 公司如何开现金支票给个人
  • 如何确定企业
  • 建筑业农民工工资
  • 出差过程中招待员工
  • 公司利润不分配可以怎么处理
  • 加油的时候可以开发票吗
  • 营改增后房地产开发税费一览表
  • 已抵扣发票红冲后发票还给对方公司
  • 航天金税服务费怎么做账
  • 出差补贴费
  • 增值税如何填报
  • 2017年广告费税率
  • 加计扣除的研发费用范围
  • 外籍人士劳务费怎么交税
  • 外购存货成本包括哪些
  • 购进农产品怎么做账
  • 发票在验旧日期之后作废吗
  • 所得税季报营业外收入怎么填
  • 此次新政策对原来就是小型微利企业的纳税人有影响吗?
  • 企业收到政府补贴100000元业务题
  • 小型微利企业怎么认定最新标准
  • 缴纳的权利许可有哪些
  • 既征增值税又征消费税的是
  • .sfx.exe是什么文件
  • CodeIgniter视图使用注意事项
  • php smtp类
  • laravel 使用redis
  • Yii2 assets清除缓存的方法
  • 销售费用期末余额
  • 李宏毅课程
  • 固定成本又称什么成本
  • 账面价值账面余额摊余成本
  • 预缴与申报
  • 培训机构账务处理
  • 滴滴出行发票税率是多少
  • 预付账款借方怎么调平
  • 企业转让股权如何缴纳企业所得税
  • 计税工资什么意思2019
  • 资金结存属于资产科目吗
  • sql函数coalesce
  • 公司固定资产抵押贷款无法偿还
  • 工资是什么?包括哪些
  • 长期股权投资的账务处理
  • 固定资产的原价减去预计净残值等于什么
  • 确认收入时,也必须确认资产或债务
  • 出纳现金日记账怎么记账
  • 自查时发现以前的事情
  • 错账查找方法主要有
  • 财税公司工作内容
  • 没有发票的费用怎么做凭证
  • windows如何创建桌面快捷方式
  • 安装fedora33
  • suse 10.3 安装http apche2时遇到的rpm依赖问题的解决方法
  • cmos电池没电会有什么故障现象
  • xmp文件是干嘛用的
  • mac怎么创建网络
  • win7笔记本设置合上盖子不休眠
  • win8windows设置在哪里
  • win10一年更新一次
  • 分布式队列秒杀活动
  • cocos2dx加libevent库
  • 计算机图形学是什么专业
  • shell批量处理文件
  • python int 转 float
  • javascript获取css
  • 比较两个字符串的值是否相等
  • 自定义progressbar
  • div怎么求
  • position属性含义
  • 浙江税务打不开,提示新版本
  • 贵州地税网上申报大厅
  • 捐赠纳税
  • 国税注销需要什么资料
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设