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
cd ..
5.新建一个python文件 test.py,填写以下代码
from groundingdino.util.inference import load_model, load_image, predict, annotate
import cv2
model = load_model("./groundingdino/config/GroundingDINO_SwinT_OGC.py", "./weights/groundingdino_swint_ogc.pth")
IMAGE_PATH = "./.asset/cat_dog.jpeg"
TEXT_PROMPT = "chair . person . dog ."
BOX_TRESHOLD = 0.35
TEXT_TRESHOLD = 0.25
image_source, image = load_image(IMAGE_PATH)
boxes, logits, phrases = predict(
model=model,
image=image,
caption=TEXT_PROMPT,
box_threshold=BOX_TRESHOLD,
text_threshold=TEXT_TRESHOLD
)
annotated_frame = annotate(image_source=image_source, boxes=boxes, logits=logits, phrases=phrases)
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代码
from transformers import AutoTokenizer, BertModel, BertTokenizer, RobertaModel, RobertaTokenizerFast
import os
# 获取指定文本编码器类型的分词器
def get_tokenlizer(text_encoder_type, local_files_only=True):
# 检查text_encoder_type是否为字符串
if not isinstance(text_encoder_type, str):
# 如果不是字符串,尝试从对象中获取text_encoder_type属性
if hasattr(text_encoder_type, "text_encoder_type"):
text_encoder_type = text_encoder_type.text_encoder_type
# 如果字典中存在"text_encoder_type"键,则从字典中获取
elif text_encoder_type.get("text_encoder_type", False):
text_encoder_type = text_encoder_type.get("text_encoder_type")
# 如果是目录且存在,则保持不变
elif os.path.isdir(text_encoder_type) and os.path.exists(text_encoder_type):
pass
else:
# 如果无法确定text_encoder_type类型,则引发错误
raise ValueError(
"Unknown type of text_encoder_type: {}".format(type(text_encoder_type))
)
print("final text_encoder_type: {}".format(text_encoder_type))
# 使用transformers库中的AutoTokenizer根据text_encoder_type加载分词器
tokenizer = AutoTokenizer.from_pretrained(text_encoder_type, local_files_only=local_files_only)
return tokenizer
# 获取指定预训练语言模型的模型实例
def get_pretrained_language_model(text_encoder_type, local_files_only=True):
# 根据text_encoder_type选择合适的预训练语言模型
if text_encoder_type == "bert-base-uncased" or (os.path.isdir(text_encoder_type) and os.path.exists(text_encoder_type)):
# 如果是BERT模型,则使用BertModel加载
return BertModel.from_pretrained(text_encoder_type, local_files_only=local_files_only)
elif text_encoder_type == "roberta-base":
# 如果是RoBERTa模型,则使用RobertaModel加载
return RobertaModel.from_pretrained(text_encoder_type, local_files_only=local_files_only)
# 如果text_encoder_type不是已知的模型类型,则引发错误
raise ValueError("Unknown text_encoder_type {}".format(text_encoder_type))
解决警告:到报警告的代码上,更改代码为 return _VF.meshgrid(tensors, **kwargs, indexing="ij")
还有两个警告没有解决,但是不影响程序运行,还有下面的问题 ,我没遇到过 ,但是也注意一下
后续会出这篇论文的详解和训练代码和教程.......请关注一下