Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
9d6c0641
Unverified
Commit
9d6c0641
authored
Jul 25, 2024
by
Pavel Iakubovskii
Committed by
GitHub
Jul 25, 2024
Browse files
Fix code snippet for Grounding DINO (#32229)
Fix code snippet for grounding-dino
parent
3a83ec48
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
27 deletions
+34
-27
docs/source/en/model_doc/grounding-dino.md
docs/source/en/model_doc/grounding-dino.md
+34
-27
No files found.
docs/source/en/model_doc/grounding-dino.md
View file @
9d6c0641
...
...
@@ -41,33 +41,40 @@ The original code can be found [here](https://github.com/IDEA-Research/Grounding
Here's how to use the model for zero-shot object detection:
```
python
import
requests
import
torch
from
PIL
import
Image
from
transformers
import
AutoProcessor
,
AutoModelForZeroShotObjectDetection
,
model_id
=
"IDEA-Research/grounding-dino-tiny"
processor
=
AutoProcessor
.
from_pretrained
(
model_id
)
model
=
AutoModelForZeroShotObjectDetection
.
from_pretrained
(
model_id
).
to
(
device
)
image_url
=
"http://images.cocodataset.org/val2017/000000039769.jpg"
image
=
Image
.
open
(
requests
.
get
(
image_url
,
stream
=
True
).
raw
)
# Check for cats and remote controls
text
=
"a cat. a remote control."
inputs
=
processor
(
images
=
image
,
text
=
text
,
return_tensors
=
"pt"
).
to
(
device
)
with
torch
.
no_grad
():
outputs
=
model
(
**
inputs
)
results
=
processor
.
post_process_grounded_object_detection
(
outputs
,
inputs
.
input_ids
,
box_threshold
=
0.4
,
text_threshold
=
0.3
,
target_sizes
=
[
image
.
size
[::
-
1
]]
)
>>>
import
requests
>>>
import
torch
>>>
from
PIL
import
Image
>>>
from
transformers
import
AutoProcessor
,
AutoModelForZeroShotObjectDetection
>>>
model_id
=
"IDEA-Research/grounding-dino-tiny"
>>>
device
=
"cuda"
>>>
processor
=
AutoProcessor
.
from_pretrained
(
model_id
)
>>>
model
=
AutoModelForZeroShotObjectDetection
.
from_pretrained
(
model_id
).
to
(
device
)
>>>
image_url
=
"http://images.cocodataset.org/val2017/000000039769.jpg"
>>>
image
=
Image
.
open
(
requests
.
get
(
image_url
,
stream
=
True
).
raw
)
>>>
# Check for cats and remote controls
>>>
text
=
"a cat. a remote control."
>>>
inputs
=
processor
(
images
=
image
,
text
=
text
,
return_tensors
=
"pt"
).
to
(
device
)
>>>
with
torch
.
no_grad
():
...
outputs
=
model
(
**
inputs
)
>>>
results
=
processor
.
post_process_grounded_object_detection
(
...
outputs
,
...
inputs
.
input_ids
,
...
box_threshold
=
0.4
,
...
text_threshold
=
0.3
,
...
target_sizes
=
[
image
.
size
[::
-
1
]]
...
)
>>>
print
(
results
)
[{
'boxes'
:
tensor
([[
344.6959
,
23.1090
,
637.1833
,
374.2751
],
[
12.2666
,
51.9145
,
316.8582
,
472.4392
],
[
38.5742
,
70.0015
,
176.7838
,
118.1806
]],
device
=
'cuda:0'
),
'labels'
:
[
'a cat'
,
'a cat'
,
'a remote control'
],
'scores'
:
tensor
([
0.4785
,
0.4381
,
0.4776
],
device
=
'cuda:0'
)}]
```
## Grounded SAM
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment