当前位置:   article > 正文

GroundingDINO运行教程_groundingdino_swint_ogc.pth

groundingdino_swint_ogc.pth

1. 从 GitHub 克隆 GroundingDINO 存储库。

git clone https://github.com/IDEA-Research/GroundingDINO.git

2. 将当前目录更改为 GroundingDINO 文件夹

cd GroundingDINO/

3. 在当前目录中安装所需的依赖项。

pip install -e .

4. 下载预先训练的模型权重。

mkdir weights

cd weights

wget -q https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth

cd ..

5.新建一个python文件 test.py,填写以下代码

  1. from groundingdino.util.inference import load_model, load_image, predict, annotate
  2. import cv2
  3. model = load_model("./groundingdino/config/GroundingDINO_SwinT_OGC.py", "./weights/groundingdino_swint_ogc.pth")
  4. IMAGE_PATH = "./.asset/cat_dog.jpeg"
  5. TEXT_PROMPT = "chair . person . dog ."
  6. BOX_TRESHOLD = 0.35
  7. TEXT_TRESHOLD = 0.25
  8. image_source, image = load_image(IMAGE_PATH)
  9. boxes, logits, phrases = predict(
  10. model=model,
  11. image=image,
  12. caption=TEXT_PROMPT,
  13. box_threshold=BOX_TRESHOLD,
  14. text_threshold=TEXT_TRESHOLD
  15. )
  16. annotated_frame = annotate(image_source=image_source, boxes=boxes, logits=logits, phrases=phrases)
  17. cv2.imwrite("annotated_image.jpg", annotated_frame)

6. 终端运行python test.py,生成annotated_image.jpg文件

报错:

解决办法:配置正确版本的cuda

解决办法:由于服务器不能科学上网,所以不能在线下载bert预训练模型,在项目根目录运行 git clone https://huggingface.co/bert-base-uncased ,更改groundingdino/util/get_tokenlizer.py代码

  1. from transformers import AutoTokenizer, BertModel, BertTokenizer, RobertaModel, RobertaTokenizerFast
  2. import os
  3. # 获取指定文本编码器类型的分词器
  4. def get_tokenlizer(text_encoder_type, local_files_only=True):
  5. # 检查text_encoder_type是否为字符串
  6. if not isinstance(text_encoder_type, str):
  7. # 如果不是字符串,尝试从对象中获取text_encoder_type属性
  8. if hasattr(text_encoder_type, "text_encoder_type"):
  9. text_encoder_type = text_encoder_type.text_encoder_type
  10. # 如果字典中存在"text_encoder_type"键,则从字典中获取
  11. elif text_encoder_type.get("text_encoder_type", False):
  12. text_encoder_type = text_encoder_type.get("text_encoder_type")
  13. # 如果是目录且存在,则保持不变
  14. elif os.path.isdir(text_encoder_type) and os.path.exists(text_encoder_type):
  15. pass
  16. else:
  17. # 如果无法确定text_encoder_type类型,则引发错误
  18. raise ValueError(
  19. "Unknown type of text_encoder_type: {}".format(type(text_encoder_type))
  20. )
  21. print("final text_encoder_type: {}".format(text_encoder_type))
  22. # 使用transformers库中的AutoTokenizer根据text_encoder_type加载分词器
  23. tokenizer = AutoTokenizer.from_pretrained(text_encoder_type, local_files_only=local_files_only)
  24. return tokenizer
  25. # 获取指定预训练语言模型的模型实例
  26. def get_pretrained_language_model(text_encoder_type, local_files_only=True):
  27. # 根据text_encoder_type选择合适的预训练语言模型
  28. if text_encoder_type == "bert-base-uncased" or (os.path.isdir(text_encoder_type) and os.path.exists(text_encoder_type)):
  29. # 如果是BERT模型,则使用BertModel加载
  30. return BertModel.from_pretrained(text_encoder_type, local_files_only=local_files_only)
  31. elif text_encoder_type == "roberta-base":
  32. # 如果是RoBERTa模型,则使用RobertaModel加载
  33. return RobertaModel.from_pretrained(text_encoder_type, local_files_only=local_files_only)
  34. # 如果text_encoder_type不是已知的模型类型,则引发错误
  35. raise ValueError("Unknown text_encoder_type {}".format(text_encoder_type))

解决警告:到报警告的代码上,更改代码为 return _VF.meshgrid(tensors, **kwargs, indexing="ij")

还有两个警告没有解决,但是不影响程序运行,还有下面的问题 ,我没遇到过 ,但是也注意一下

后续会出这篇论文的详解和训练代码和教程.......请关注一下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/359308
推荐阅读
相关标签
  

闽ICP备14008679号