Views
No views yet
IDEA-Research/grounding-dino-tiny for zeromodels. One implementation runs unmodified on TensorFlow / Torch / JAX.GroundingDinoForObjectDetection, Swin-Tiny backbone): each query predicts a box and a score over the prompt tokens.1import os
2os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
3
4import torch
5from PIL import Image
6from zeromodels.models.grounding_dino import (
7 GroundingDinoForObjectDetection,
8 GroundingDinoProcessor,
9)
10
11model = GroundingDinoForObjectDetection.from_weights("zeromodels/grounding_dino_tiny")
12processor = GroundingDinoProcessor.from_weights("zeromodels/grounding_dino_tiny")
13
14image = Image.open("your_image.jpg").convert("RGB")
15# Prompts are free text; pass a list of candidates (or one "a. b. c." string). Skip
16# articles: in "a paddle" the "a" can outscore the noun.
17inputs = processor(images=image, text=["person", "paddle", "board"])
18
19with torch.no_grad(): # torch backend: avoids a large autograd graph (can OOM otherwise)
20 output = model(inputs)
21# output["logits"]: (1, 900, 256)
22# output["pred_boxes"]: (1, 900, 4)
23
24results = processor.post_process_object_detection(
25 output,
26 threshold=0.3,
27 target_sizes=[(image.height, image.width)],
28 input_ids=inputs["input_ids"],
29)[0]
30for score, name, box in sorted(
31 zip(results["scores"], results["text_labels"], results["boxes"]),
32 key=lambda d: -float(d[0]),
33):
34 print(f"{name}: {float(score):.3f} {[round(float(v)) for v in box]}")from_weights("zeromodels/<variant>"):| Variant | Hub | Backbone |
|---|---|---|
grounding_dino_tiny | zeromodels/grounding_dino_tiny | Swin-Tiny |
grounding_dino_base | zeromodels/grounding_dino_base | Swin-Base |
KERAS_BACKEND before importing Keras / zeromodels.with torch.no_grad(): — the forward keeps a large autograd graph otherwise and can OOM. The JAX / TensorFlow backends need no such wrap..; drop articles ("a", "the") so the noun scores highest. post_process_object_detection needs input_ids= to map scores back to prompt words (text_labels).threshold=0.3 is a reasonable start; raise it for cleaner scenes.hf: prefix, e.g. GroundingDinoForObjectDetection.from_weights("hf:IDEA-Research/grounding-dino-tiny").