Grounded-Segment-Anything本地部署

2023-10-11 13:50

本文主要是介绍Grounded-Segment-Anything本地部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Grounded-Segment-Anything本地部署

  • 1.本地部署源码
    • 1.1 克隆源码
    • 1.2 下载初始参数
  • 2. 创建虚拟环境
  • 3. 测试相关文件
    • 3.1 运行`grounding_dino_demo.py`文件
    • 3.2 运行`grounded_sam_demo.py`文件
    • 3.3 运行`grounded_sam_simple_demo.py`文件
    • 3.4 `grounded_sam_inpainting_demo.py`文件
    • 3.5 ` 运行`automatic_label_ram_demo.py`文件
    • 3.6 运行`automatic_label_demo.py`文件
    • 3.7. 批量自动标注图片
  • 5. 总结

  • 源码链接:https://github.com/IDEA-Research/Grounded-Segment-Anything
  • 介绍
    • Segment Anything Model,简称 SAM。
    • SAM 已经学会了关于物体的一般概念,可以为任何图像或视频中的任何物体生成 mask,甚至包括在训练过程中没有遇到过的物体和图像类型。
    • SAM 足够通用,可以涵盖广泛的用例,并且可以在新的图像领域即开即用,无需额外的训练

1.本地部署源码

1.1 克隆源码

  • 克隆命令
    git clone https://github.com/IDEA-Research/Grounded-Segment-Anything.git
    
  • 问题一
    • 克隆下来后发现下面的三个文件夹是空的
      在这里插入图片描述

    • 在源码链接中有这两行命令,应该是把这三个文件夹文件下载下来,但是我的报错
      在这里插入图片描述

    • 解决 :手动下载,然后放在相应的文件夹
      在这里插入图片描述

  • 问题二【手动放在相应的文件夹后,调用里面的文件路径是错的】
    • 解决:修改相关路径,运行代码将不对的路径全部改掉
    • 修改路径 【segment_anything】
      在这里插入图片描述
    • 修改路径 【GroundingDINO】
      在这里插入图片描述

1.2 下载初始参数

  • 参数链接
    • groundingdino_swint_ogc.pth
    • sam_vit_h_4b8939.pth
    • sam_hq_vit_h.pth
    • ram_swin_large_14m.pth
    • tag2text_swin_14m.pth
  • 初始参数文件存放目录:项目根目录
    在这里插入图片描述

2. 创建虚拟环境

  • 创建虚拟环境
    conda create -n env_grounded_segment_anything python==3.8.10
    
  • 进入虚拟环境
    conda activate env_grounded_segment_anything
    
  • 安装pytorch
    pip install torch==1.10.0+cu113 torchvision==0.11.0+cu113 torchaudio==0.10.0 -f https://download.pytorch.org/whl/torch_stable.html
    
  • 安装requirements.txt
    pip install -r requirements.txt 
    
  • 使用pycharm打开项目
    在这里插入图片描述
  • 选择虚拟环境
    在这里插入图片描述

3. 测试相关文件

3.1 运行grounding_dino_demo.py文件

  • gpu,将DEVICE 值改为 cpu
  • gpu,无需修改参数
  • 生成标注图片
    在这里插入图片描述

3.2 运行grounded_sam_demo.py文件

  • 添加参数,电脑没有gpudevice 参数使用默认的cpu
    --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"  
    

在这里插入图片描述

  • 输出结果
    • 文件说明
      在这里插入图片描述
    • 图片显示
      在这里插入图片描述

3.3 运行grounded_sam_simple_demo.py文件

  • 无cuda报错
    在这里插入图片描述
  • inference.py修改device值改为cpu
    在这里插入图片描述
  • 测试demo4.jpg【无需修改代码】
    在这里插入图片描述
  • 测试demo7.jpg
    • 修改关于demo.jpg信息
      在这里插入图片描述
    • 结果
      在这里插入图片描述

3.4 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"
    

    在这里插入图片描述

  • 报错 【下载远程文件失败】
    在这里插入图片描述

  • 解决:手动下载

    • 下载链接:https://huggingface.co/runwayml/stable-diffusion-inpainting/tree/main
    • 将下载好的文件放在config_data文件夹中
      在这里插入图片描述
    • 修改为本地路径
      在这里插入图片描述
  • 重新运行:报错 【原因:我没有gpu】
    在这里插入图片描述

  • 解决,将cuda换为cpu
    在这里插入图片描述

  • 重新运行:报错

    • RuntimeError: "LayerNormKernelImpl" not implemented for 'Half'
      在这里插入图片描述

    • 解决方式:解决链接

    • float16改为float32
      在这里插入图片描述

  • 结果
    在这里插入图片描述

3.5 运行automatic_label_ram_demo.py`文件

  • 添加参数

    --ram_checkpoint
    ram_swin_large_14m.pth
    --grounded_checkpoint
    groundingdino_swint_ogc.pth
    --sam_checkpoint
    sam_vit_h_4b8939.pth
    --input_image
    assets/demo9.jpg
    --output_dir
    "outputs"
    --box_threshold
    0.25
    --text_threshold
    0.2
    --iou_threshold
    0.5
    
  • 结果
    在这里插入图片描述

3.6 运行automatic_label_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/demo9.jpg
    --output_dir
    "outputs"
    --box_threshold
    0.25
    --text_threshold
    0.2
    --iou_threshold
    0.5
    

    在这里插入图片描述

  • 报错
    在这里插入图片描述

  • 原因:下载文件失败,手动下载

    • 下载链接:https://huggingface.co/Salesforce/blip-image-captioning-large/tree/main
    • 将文件放在config_data
      在这里插入图片描述
    • 修改为本地路径
      在这里插入图片描述
  • 报错Resource punkt not found. Please use the NLTK Downloader to obtain the resources

    • 手动下载链接:http://www.nltk.org/nltk_data/
    • 下载wordnet,punkt,averaged_perceptron_tagger
    • 放到相应目录,zip解压文件都要有
      在这里插入图片描述
  • 结果
    在这里插入图片描述

3.7. 批量自动标注图片

  • 修改automatic_label_demo.py文件
  • 文件太长,其他的地方还要改
    if __name__ == "__main__":root_path=''  # 根目录images_name='images'   # 图片文件夹名images_path=os.path.join(root_path,images_name)images_outputs_path=os.path.join(root_path,'grounded_segment_anything_images')output_json = os.path.join(images_outputs_path,'json')output_orig = os.path.join(images_outputs_path,'orig')output_mask = os.path.join(images_outputs_path,'mask')output_automatic_label = os.path.join(images_outputs_path,'automatic_label')for i in [output_json,output_mask,output_orig,output_automatic_label]:os.makedirs(i, exist_ok=True)images_list=os.listdir(images_path)parser = argparse.ArgumentParser("Grounded-Segment-Anything Demo", add_help=True)parser.add_argument("--config", type=str, default='GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py',help="path to config file")parser.add_argument("--grounded_checkpoint", type=str, default='groundingdino_swint_ogc.pth', help="path to checkpoint file")parser.add_argument("--sam_checkpoint", type=str, default='sam_vit_h_4b8939.pth', help="path to checkpoint file")parser.add_argument("--split", default=",", type=str, help="split for text prompt")parser.add_argument("--openai_key", type=str, help="key for chatgpt")parser.add_argument("--openai_proxy", default=None, type=str, help="proxy for chatgpt")parser.add_argument("--box_threshold", type=float, default=0.25, help="box threshold")parser.add_argument("--text_threshold", type=float, default=0.2, help="text threshold")parser.add_argument("--iou_threshold", type=float, default=0.5, help="iou threshold")parser.add_argument("--device", type=str, default="cpu", help="running on cpu only!, default=False")args = parser.parse_args()# cfgconfig_file = args.config  # change the path of the model config filegrounded_checkpoint = args.grounded_checkpoint  # change the path of the modelsam_checkpoint = args.sam_checkpoint# image_path = args.input_imagesplit = args.splitopenai_key = args.openai_keyopenai_proxy = args.openai_proxybox_threshold = args.box_thresholdtext_threshold = args.text_thresholdiou_threshold = args.iou_thresholddevice = args.deviceopenai.api_key = openai_keyif openai_proxy:openai.proxy = {"http": openai_proxy, "https": openai_proxy}# load modelmodel = load_model(config_file, grounded_checkpoint, device=device)processor = BlipProcessor.from_pretrained("config_data/blip-image-captioning-large")if device == "cuda":blip_model = BlipForConditionalGeneration.from_pretrained("config_data/blip-image-captioning-large",torch_dtype=torch.float16).to("cuda")else:blip_model = BlipForConditionalGeneration.from_pretrained("config_data/blip-image-captioning-large")for img_name in images_list:image_path=os.path.join(images_path,img_name)image_pil, image = load_image(image_path)image_pil.save(os.path.join(output_orig, img_name))args = parser.parse_args()caption = generate_caption(image_pil, device=device)text_prompt = generate_tags(caption, split=split)print(f"Caption: {caption}")print(f"Tags: {text_prompt}")# visualize raw imageimage_pil.save(os.path.join(output_orig,img_name ))# run grounding dino modelboxes_filt, scores, pred_phrases = get_grounding_output(model, image, text_prompt, box_threshold, text_threshold, device=device)# initialize SAMpredictor = SamPredictor(build_sam(checkpoint=sam_checkpoint).to(device))image = cv2.imread(image_path)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)predictor.set_image(image)size = image_pil.sizeH, W = size[1], size[0]for i in range(boxes_filt.size(0)):boxes_filt[i] = boxes_filt[i] * torch.Tensor([W, H, W, H])boxes_filt[i][:2] -= boxes_filt[i][2:] / 2boxes_filt[i][2:] += boxes_filt[i][:2]boxes_filt = boxes_filt.cpu()# use NMS to handle overlapped boxesprint(f"Before NMS: {boxes_filt.shape[0]} boxes")nms_idx = torchvision.ops.nms(boxes_filt, scores, iou_threshold).numpy().tolist()boxes_filt = boxes_filt[nms_idx]pred_phrases = [pred_phrases[idx] for idx in nms_idx]print(f"After NMS: {boxes_filt.shape[0]} boxes")caption = check_caption(caption, pred_phrases)print(f"Revise caption with number: {caption}")transformed_boxes = predictor.transform.apply_boxes_torch(boxes_filt, image.shape[:2]).to(device)masks, _, _ = predictor.predict_torch(point_coords = None,point_labels = None,boxes = transformed_boxes.to(device),multimask_output = False,)# draw output imageplt.figure(figsize=(10, 10))plt.imshow(image)for mask in masks:show_mask(mask.cpu().numpy(), plt.gca(), random_color=True)for box, label in zip(boxes_filt, pred_phrases):show_box(box.numpy(), plt.gca(), label)plt.title(caption)plt.axis('off')plt.savefig(os.path.join(output_automatic_label,img_name),bbox_inches="tight", dpi=300, pad_inches=0.0)save_mask_data(output_mask,output_json,img_name, caption, masks, boxes_filt, pred_phrases)
    

5. 总结

  • 文件太多了,其他的不像测试了,因该都没有很大的问题
  • 文件初始化参数太大了,一个项目占了几十个G
  • 效果也不好,不想做了,摆烂

这篇关于Grounded-Segment-Anything本地部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/188495

相关文章

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Java实现本地缓存的常用方案介绍

《Java实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

Maven项目打包时添加本地Jar包的操作步骤

《Maven项目打包时添加本地Jar包的操作步骤》在Maven项目开发中,我们经常会遇到需要引入本地Jar包的场景,比如使用未发布到中央仓库的第三方库或者处理版本冲突的依赖项,本文将详细介绍如何通过M... 目录一、适用场景说明​二、核心操作命令​1. 命令格式解析​2. 实战案例演示​三、项目配置步骤​1

使用Python实现调用API获取图片存储到本地的方法

《使用Python实现调用API获取图片存储到本地的方法》开发一个自动化工具,用于从JSON数据源中提取图像ID,通过调用指定API获取未经压缩的原始图像文件,并确保下载结果与Postman等工具直接... 目录使用python实现调用API获取图片存储到本地1、项目概述2、核心功能3、环境准备4、代码实现

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

python如何下载网络文件到本地指定文件夹

《python如何下载网络文件到本地指定文件夹》这篇文章主要为大家详细介绍了python如何实现下载网络文件到本地指定文件夹,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下...  在python中下载文件到本地指定文件夹可以通过以下步骤实现,使用requests库处理HTTP请求,并结合o

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

ubuntu如何部署Dify以及安装Docker? Dify安装部署指南

《ubuntu如何部署Dify以及安装Docker?Dify安装部署指南》Dify是一个开源的大模型应用开发平台,允许用户快速构建和部署基于大语言模型的应用,ubuntu如何部署Dify呢?详细请... Dify是个不错的开源LLM应用开发平台,提供从 Agent 构建到 AI workflow 编排、RA

ubuntu16.04如何部署dify? 在Linux上安装部署Dify的技巧

《ubuntu16.04如何部署dify?在Linux上安装部署Dify的技巧》随着云计算和容器技术的快速发展,Docker已经成为现代软件开发和部署的重要工具之一,Dify作为一款优秀的云原生应用... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。它