Grounding 模型 + SAM 报错

引入 Grounding 目标检测模型串联 SAM 从而实现实例分割任务,目前支持 Grounding DINO 和 GLIP

参考教程

MMDetection-SAM

如果是 Grounding DINO 则安装如下依赖即可

cd playground
pip install git+https://github.com/facebookresearch/segment-anything.git
pip install git+https://github.com/IDEA-Research/GroundingDINO.git # 需要编译 CUDA OP,请确保你的 PyTorch 版本、GCC 版本和 NVCC 编译版本兼容

如果是 GLIP 则安装如下依赖即可

cd playground

pip install git+https://github.com/facebookresearch/segment-anything.git
pip install einops shapely timm yacs tensorboardX ftfy prettytable pymongo transformers nltk inflect scipy pycocotools opencv-python matplotlib

git clone https://github.com/microsoft/GLIP.git
cd GLIP; python setup.py build develop --user  # 需要编译 CUDA OP,请确保你的 PyTorch 版本、GCC 版本和 NVCC 编译版本兼容,暂时不支持 PyTorch 1.11+ 版本

执行功能演示代码报错

(mmdet-sam) hadoop@server:~/jupyter/mmdet-sam/playground/mmdet_sam$ python detector_sam_demo.py ../images/cat_remote.jpg \
    configs/GroundingDINO_SwinT_OGC.py \
    ../models/groundingdino_swint_ogc.pth \
    -t "cat . remote" \
    --sam-device cpu
[nltk_data] Downloading package punkt to /home/hadoop/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     /home/hadoop/nltk_data...
[nltk_data]   Package averaged_perceptron_tagger is already up-to-
[nltk_data]       date!
final text_encoder_type: bert-base-uncased
pytorch_model.bin: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 440M/440M [00:25<00:00, 17.2MB/s]
Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertModel: ['cls.predictions.transform.dense.bias', 'cls.predictions.decoder.weight', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.LayerNorm.bias', 'cls.seq_relationship.bias', 'cls.seq_relationship.weight', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.bias']
- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Traceback (most recent call last):
  File "detector_sam_demo.py", line 511, in <module>
    main()
  File "detector_sam_demo.py", line 438, in main
    det_model = build_detecter(args)
  File "detector_sam_demo.py", line 160, in build_detecter
    detecter = __build_grounding_dino_model(args)
  File "detector_sam_demo.py", line 117, in __build_grounding_dino_model
    checkpoint = torch.load(args.det_weight, map_location='cpu')
  File "/home/hadoop/anaconda3/envs/mmdet-sam/lib/python3.8/site-packages/torch/serialization.py", line 600, in load
    with _open_zipfile_reader(opened_file) as opened_zipfile:
  File "/home/hadoop/anaconda3/envs/mmdet-sam/lib/python3.8/site-packages/torch/serialization.py", line 242, in __init__
    super(_open_zipfile_reader, self).__init__(torch._C.PyTorchFileReader(name_or_buffer))
OSError: [Errno 22] Invalid argument

其中 242行代码长这样

这个语句是打开一个文件, 检查输入参数

模型地址: ../models/groundingdino_swint_ogc.pth

发现此模型只有44k, 肯定不对, 重新下载模型, 有662M, 重新下载模型重新跑

遇到此问题, 在网上搜答案是找不到的, 还是要分析好自己的输入参数

### Grounding DINO 与 SAM 集成方法概述 在计算机视觉任务中,Grounding DINO 和 Segment Anything Model (SAM) 是两种强大的工具。Grounding DINO 可以通过自然语言描述定位图像中的目标区域[^2],而 SAM 则能够高效地生成高质量的分割掩码[^1]。两者的结合可以显著提升涉及语义理解的任务性能。 #### 方法说明 一种常见的集成方式是先使用 Grounding DINO 提取感兴趣的目标边界框或关键点位置,随后将这些作为输入传递给 SAM 进行精细化分割。具体流程如下: - **文本到图像映射**:利用 Grounding DINO 的能力,接收一段文字描述并返回对应的检测框坐标以及置信度分数。 - **分割细化处理**:把上述得到的结果送入预训练好的 SAM 模型实例化对象,在此基础上完成像素级精确划分操作。 以下是 Python 中如何调用这两个库的一个简单例子: ```python from groundingdino.util.inference import load_model, predict import torch from segment_anything import sam_model_registry, SamPredictor # 加载模型 grounding_dino = load_model("path/to/GroundingDINO/weight.pth") sam_checkpoint = "path/to/sam_vit_h_4b8939.pth" model_type = "vit_h" device = "cuda" if torch.cuda.is_available() else "cpu" sam = sam_model_registry[model_type](checkpoint=sam_checkpoint).to(device=device) predictor = SamPredictor(sam) image_path = 'example.jpg' text_prompt = "a photo of a dog." boxes, logits, phrases = predict( model=grounding_dino, image=image_path, caption=text_prompt, box_threshold=0.35, text_threshold=0.25 ) for i in range(len(boxes)): x_min, y_min, x_max, y_max = boxes[i].numpy() predictor.set_image(image) input_box = np.array([x_min, y_min, x_max, y_max]) masks, _, _ = predictor.predict(point_coords=None, point_labels=None, box=input_box[None,:], multimask_output=False) ``` 此脚本展示了从加载必要的权重文件开始直到最终获得二值掩膜的过程。 #### 技术优势分析 这种组合不仅继承了各自单独使用的优点还进一步增强了整体系统的灵活性和鲁棒性。例如,在电商领域应用时,面对复杂背景下的产品图片,仅依靠传统边缘检测算法可能难以达到理想效果;但如果引入该方案,则可以通过简单的指令快速准确地标记出主体轮廓以便后续编辑加工。 另外值得注意的是,尽管当前版本已经表现出色但仍存在改进空间——比如当遇到多个相似类别物体共存于同一画面内时可能会出现混淆现象等问题待解决。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值