在本地跑 GroundingDINO 代码 (github)
首先down下来代码:
git clone https://github.com/IDEA-Research/GroundingDINO.git
然后跟着 readme 走,先下载预训练参数放到 ./weight 文件夹:
mkdir weights
cd weights
wget -q https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
然后新开一个 py 文件,复制 readme 上的代码运行:
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 = "weights/dog-3.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)
然后报错和 debug,这个错误都是普适性错误,第二次犯了,所以记录下来,防止再犯
NameError: name ‘_C’ is not defined
省流
运行
pip3 install -q -e .
详细过程
之前跑另一个模型的时候也遇到这个问题,勉强记得是因为没有编译模型。down 下来的代码有个 setup.py
文件,于是我尝试运行:
python setup.py
但是报错:error: no commands supplied
然后发现后面应该加上 build 或者 install,所以我重新运行:
python setup.py install
python setup.py build
但是还是不行,还是报错
查到大家在讨论这个问题:NameError: name ‘_C’ is not defined
说应该设置环境变量 CUDA_HOME,原因是,查 setup 代码:
if CUDA_HOME is not None and (torch.cuda.is_available() or "TORCH_CUDA_ARCH_LIST" in os.environ):
print("Compiling with CUDA")
extension = CUDAExtension
sources += source_cuda
define_macros += [("WITH_CUDA", None)]
extra_compile_args["nvcc"] = [
"-DCUDA_HAS_FP16=1",
"-D__CUDA_NO_HALF_OPERATORS__",
"-D__CUDA_NO_HALF_CONVERSIONS__",
"-D__CUDA_NO_HALF2_OPERATORS__",
]
但是确实打印了 Compiling with CUDA,但是还是没有那个重要的 _C
然后重新看了下 readme 怎么写的,发现有一步是:pip3 install -q -e .
这步我当时跳过了,因为我本地有一个已经配置好的环境,需要的包都装好了,想着不用 install 了反而把已经配置好的版本弄乱了,没想到就是这句话决定了一切 orz
结论:要遵循 readme 写的来做,不然人家为啥那么写呢。
文章出处登录后可见!