noni27 commited on
Commit
f997af4
1 Parent(s): a4bbc27

Upload 97 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +3 -0
  2. GroundingDINO/.asset/COCO.png +0 -0
  3. GroundingDINO/.asset/GD_GLIGEN.png +3 -0
  4. GroundingDINO/.asset/GD_SD.png +3 -0
  5. GroundingDINO/.asset/ODinW.png +0 -0
  6. GroundingDINO/.asset/arch.png +0 -0
  7. GroundingDINO/.asset/cat_dog.jpeg +0 -0
  8. GroundingDINO/.asset/cats.png +0 -0
  9. GroundingDINO/.asset/grounding_dino_logo.png +0 -0
  10. GroundingDINO/.asset/hero_figure.png +3 -0
  11. GroundingDINO/.asset/model_explan1.PNG +0 -0
  12. GroundingDINO/.asset/model_explan2.PNG +0 -0
  13. GroundingDINO/.gitignore +146 -0
  14. GroundingDINO/LICENSE +201 -0
  15. GroundingDINO/README.md +367 -0
  16. GroundingDINO/demo/create_coco_dataset.py +83 -0
  17. GroundingDINO/demo/gradio_app.py +125 -0
  18. GroundingDINO/demo/image_editing_with_groundingdino_gligen.ipynb +0 -0
  19. GroundingDINO/demo/image_editing_with_groundingdino_stablediffusion.ipynb +0 -0
  20. GroundingDINO/demo/inference_on_a_image.py +214 -0
  21. GroundingDINO/demo/test_ap_on_coco.py +233 -0
  22. GroundingDINO/environment.yaml +248 -0
  23. GroundingDINO/groundingdino.egg-info/PKG-INFO +213 -0
  24. GroundingDINO/groundingdino.egg-info/SOURCES.txt +42 -0
  25. GroundingDINO/groundingdino.egg-info/dependency_links.txt +1 -0
  26. GroundingDINO/groundingdino.egg-info/requires.txt +10 -0
  27. GroundingDINO/groundingdino.egg-info/top_level.txt +1 -0
  28. GroundingDINO/groundingdino/__init__.py +0 -0
  29. GroundingDINO/groundingdino/__pycache__/__init__.cpython-39.pyc +0 -0
  30. GroundingDINO/groundingdino/config/GroundingDINO_SwinB_cfg.py +43 -0
  31. GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py +43 -0
  32. GroundingDINO/groundingdino/config/__init__.py +0 -0
  33. GroundingDINO/groundingdino/datasets/__init__.py +0 -0
  34. GroundingDINO/groundingdino/datasets/__pycache__/__init__.cpython-39.pyc +0 -0
  35. GroundingDINO/groundingdino/datasets/__pycache__/transforms.cpython-39.pyc +0 -0
  36. GroundingDINO/groundingdino/datasets/cocogrounding_eval.py +269 -0
  37. GroundingDINO/groundingdino/datasets/transforms.py +311 -0
  38. GroundingDINO/groundingdino/models/GroundingDINO/__init__.py +15 -0
  39. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/__init__.cpython-39.pyc +0 -0
  40. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/bertwarper.cpython-39.pyc +0 -0
  41. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/fuse_modules.cpython-39.pyc +0 -0
  42. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/groundingdino.cpython-39.pyc +0 -0
  43. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/ms_deform_attn.cpython-39.pyc +0 -0
  44. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/transformer.cpython-39.pyc +0 -0
  45. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/transformer_vanilla.cpython-39.pyc +0 -0
  46. GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/utils.cpython-39.pyc +0 -0
  47. GroundingDINO/groundingdino/models/GroundingDINO/backbone/__init__.py +1 -0
  48. GroundingDINO/groundingdino/models/GroundingDINO/backbone/__pycache__/__init__.cpython-39.pyc +0 -0
  49. GroundingDINO/groundingdino/models/GroundingDINO/backbone/__pycache__/backbone.cpython-39.pyc +0 -0
  50. GroundingDINO/groundingdino/models/GroundingDINO/backbone/__pycache__/position_encoding.cpython-39.pyc +0 -0
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ GroundingDINO/.asset/GD_GLIGEN.png filter=lfs diff=lfs merge=lfs -text
37
+ GroundingDINO/.asset/GD_SD.png filter=lfs diff=lfs merge=lfs -text
38
+ GroundingDINO/.asset/hero_figure.png filter=lfs diff=lfs merge=lfs -text
GroundingDINO/.asset/COCO.png ADDED
GroundingDINO/.asset/GD_GLIGEN.png ADDED

Git LFS Details

  • SHA256: 6e36d497ace68412ecd6c064fff6d7481a685963ffc2ec047a8892411fb0ab8e
  • Pointer size: 132 Bytes
  • Size of remote file: 1.23 MB
GroundingDINO/.asset/GD_SD.png ADDED

Git LFS Details

  • SHA256: 92c8a690a2de028d42c9b876c73dca53b7736134eb77cce5b3cbda9d1c4b62de
  • Pointer size: 132 Bytes
  • Size of remote file: 1.16 MB
GroundingDINO/.asset/ODinW.png ADDED
GroundingDINO/.asset/arch.png ADDED
GroundingDINO/.asset/cat_dog.jpeg ADDED
GroundingDINO/.asset/cats.png ADDED
GroundingDINO/.asset/grounding_dino_logo.png ADDED
GroundingDINO/.asset/hero_figure.png ADDED

Git LFS Details

  • SHA256: 24b18b31e9f150bae0ae01b09608d7bf7fc34f42c8e17d85eda55ea4a55b1e91
  • Pointer size: 132 Bytes
  • Size of remote file: 2.98 MB
GroundingDINO/.asset/model_explan1.PNG ADDED
GroundingDINO/.asset/model_explan2.PNG ADDED
GroundingDINO/.gitignore ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # IDE
2
+ .idea/
3
+ .vscode/
4
+
5
+ # Byte-compiled / optimized / DLL files
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .Python
15
+ build/
16
+ develop-eggs/
17
+ dist/
18
+ downloads/
19
+ eggs/
20
+ .eggs/
21
+ lib/
22
+ lib64/
23
+ parts/
24
+ sdist/
25
+ var/
26
+ wheels/
27
+ pip-wheel-metadata/
28
+ share/python-wheels/
29
+ *.egg-info/
30
+ .installed.cfg
31
+ *.egg
32
+ MANIFEST
33
+
34
+ # PyInstaller
35
+ # Usually these files are written by a python script from a template
36
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
37
+ *.manifest
38
+ *.spec
39
+
40
+ # Installer logs
41
+ pip-log.txt
42
+ pip-delete-this-directory.txt
43
+
44
+ # Unit test / coverage reports
45
+ htmlcov/
46
+ .tox/
47
+ .nox/
48
+ .coverage
49
+ .coverage.*
50
+ .cache
51
+ nosetests.xml
52
+ coverage.xml
53
+ *.cover
54
+ *.py,cover
55
+ .hypothesis/
56
+ .pytest_cache/
57
+
58
+ # Translations
59
+ *.mo
60
+ *.pot
61
+
62
+ # Django stuff:
63
+ *.log
64
+ local_settings.py
65
+ db.sqlite3
66
+ db.sqlite3-journal
67
+
68
+ # Flask stuff:
69
+ instance/
70
+ .webassets-cache
71
+
72
+ # Scrapy stuff:
73
+ .scrapy
74
+
75
+ # Sphinx documentation
76
+ docs/_build/
77
+
78
+ # PyBuilder
79
+ target/
80
+
81
+ # Jupyter Notebook
82
+ .ipynb_checkpoints
83
+
84
+ # IPython
85
+ profile_default/
86
+ ipython_config.py
87
+
88
+ # pyenv
89
+ .python-version
90
+
91
+ # pipenv
92
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
93
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
94
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
95
+ # install all needed dependencies.
96
+ #Pipfile.lock
97
+
98
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
99
+ __pypackages__/
100
+
101
+ # Celery stuff
102
+ celerybeat-schedule
103
+ celerybeat.pid
104
+
105
+ # SageMath parsed files
106
+ *.sage.py
107
+
108
+ # Environments
109
+ .env
110
+ .venv
111
+ env/
112
+ venv/
113
+ ENV/
114
+ env.bak/
115
+ venv.bak/
116
+
117
+ # Spyder project settings
118
+ .spyderproject
119
+ .spyproject
120
+
121
+ # Rope project settings
122
+ .ropeproject
123
+
124
+ # mkdocs documentation
125
+ /site
126
+
127
+ # mypy
128
+ .mypy_cache/
129
+ .dmypy.json
130
+ dmypy.json
131
+
132
+ # Pyre type checker
133
+ .pyre/
134
+
135
+ # vscode
136
+ .vscode/
137
+ output/
138
+ outputs/
139
+ subs/
140
+ logs/
141
+
142
+ grounding/config/configs
143
+ grounding/version.py
144
+
145
+ vis/
146
+ tmp/
GroundingDINO/LICENSE ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2023 - present, IDEA Research.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
GroundingDINO/README.md ADDED
@@ -0,0 +1,367 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div align="center">
2
+ <img src="./.asset/grounding_dino_logo.png" width="30%">
3
+ </div>
4
+
5
+ # :sauropod: Grounding DINO
6
+
7
+ [![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/grounding-dino-marrying-dino-with-grounded/zero-shot-object-detection-on-mscoco)](https://paperswithcode.com/sota/zero-shot-object-detection-on-mscoco?p=grounding-dino-marrying-dino-with-grounded) [![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/grounding-dino-marrying-dino-with-grounded/zero-shot-object-detection-on-odinw)](https://paperswithcode.com/sota/zero-shot-object-detection-on-odinw?p=grounding-dino-marrying-dino-with-grounded) \
8
+ [![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/grounding-dino-marrying-dino-with-grounded/object-detection-on-coco-minival)](https://paperswithcode.com/sota/object-detection-on-coco-minival?p=grounding-dino-marrying-dino-with-grounded) [![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/grounding-dino-marrying-dino-with-grounded/object-detection-on-coco)](https://paperswithcode.com/sota/object-detection-on-coco?p=grounding-dino-marrying-dino-with-grounded)
9
+
10
+
11
+ **[IDEA-CVR, IDEA-Research](https://github.com/IDEA-Research)**
12
+
13
+ [Shilong Liu](http://www.lsl.zone/), [Zhaoyang Zeng](https://scholar.google.com/citations?user=U_cvvUwAAAAJ&hl=zh-CN&oi=ao), [Tianhe Ren](https://rentainhe.github.io/), [Feng Li](https://scholar.google.com/citations?user=ybRe9GcAAAAJ&hl=zh-CN), [Hao Zhang](https://scholar.google.com/citations?user=B8hPxMQAAAAJ&hl=zh-CN), [Jie Yang](https://github.com/yangjie-cv), [Chunyuan Li](https://scholar.google.com/citations?user=Zd7WmXUAAAAJ&hl=zh-CN&oi=ao), [Jianwei Yang](https://jwyang.github.io/), [Hang Su](https://scholar.google.com/citations?hl=en&user=dxN1_X0AAAAJ&view_op=list_works&sortby=pubdate), [Jun Zhu](https://scholar.google.com/citations?hl=en&user=axsP38wAAAAJ), [Lei Zhang](https://www.leizhang.org/)<sup>:email:</sup>.
14
+
15
+
16
+ [[`Paper`](https://arxiv.org/abs/2303.05499)] [[`Demo`](https://huggingface.co/spaces/ShilongLiu/Grounding_DINO_demo)] [[`BibTex`](#black_nib-citation)]
17
+
18
+
19
+ PyTorch implementation and pretrained models for Grounding DINO. For details, see the paper **[Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection](https://arxiv.org/abs/2303.05499)**.
20
+
21
+ ## :sun_with_face: Helpful Tutorial
22
+
23
+ - :grapes: [[Read our arXiv Paper](https://arxiv.org/abs/2303.05499)]
24
+ - :apple: [[Watch our simple introduction video on YouTube](https://youtu.be/wxWDt5UiwY8)]
25
+ - :blossom: &nbsp;[[Try the Colab Demo](https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/zero-shot-object-detection-with-grounding-dino.ipynb)]
26
+ - :sunflower: [[Try our Official Huggingface Demo](https://huggingface.co/spaces/ShilongLiu/Grounding_DINO_demo)]
27
+ - :maple_leaf: [[Watch the Step by Step Tutorial about GroundingDINO by Roboflow AI](https://youtu.be/cMa77r3YrDk)]
28
+ - :mushroom: [[GroundingDINO: Automated Dataset Annotation and Evaluation by Roboflow AI](https://youtu.be/C4NqaRBz_Kw)]
29
+ - :hibiscus: [[Accelerate Image Annotation with SAM and GroundingDINO by Roboflow AI](https://youtu.be/oEQYStnF2l8)]
30
+ - :white_flower: [[Autodistill: Train YOLOv8 with ZERO Annotations based on Grounding-DINO and Grounded-SAM by Roboflow AI](https://github.com/autodistill/autodistill)]
31
+
32
+ <!-- Grounding DINO Methods |
33
+ [![arXiv](https://img.shields.io/badge/arXiv-2303.05499-b31b1b.svg)](https://arxiv.org/abs/2303.05499)
34
+ [![YouTube](https://badges.aleen42.com/src/youtube.svg)](https://youtu.be/wxWDt5UiwY8) -->
35
+
36
+ <!-- Grounding DINO Demos |
37
+ [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/zero-shot-object-detection-with-grounding-dino.ipynb) -->
38
+ <!-- [![YouTube](https://badges.aleen42.com/src/youtube.svg)](https://youtu.be/cMa77r3YrDk)
39
+ [![HuggingFace space](https://img.shields.io/badge/🤗-HuggingFace%20Space-cyan.svg)](https://huggingface.co/spaces/ShilongLiu/Grounding_DINO_demo)
40
+ [![YouTube](https://badges.aleen42.com/src/youtube.svg)](https://youtu.be/oEQYStnF2l8)
41
+ [![YouTube](https://badges.aleen42.com/src/youtube.svg)](https://youtu.be/C4NqaRBz_Kw) -->
42
+
43
+ ## :sparkles: Highlight Projects
44
+
45
+ - [Semantic-SAM: a universal image segmentation model to enable segment and recognize anything at any desired granularity.](https://github.com/UX-Decoder/Semantic-SAM),
46
+ - [DetGPT: Detect What You Need via Reasoning](https://github.com/OptimalScale/DetGPT)
47
+ - [Grounded-SAM: Marrying Grounding DINO with Segment Anything](https://github.com/IDEA-Research/Grounded-Segment-Anything)
48
+ - [Grounding DINO with Stable Diffusion](demo/image_editing_with_groundingdino_stablediffusion.ipynb)
49
+ - [Grounding DINO with GLIGEN for Controllable Image Editing](demo/image_editing_with_groundingdino_gligen.ipynb)
50
+ - [OpenSeeD: A Simple and Strong Openset Segmentation Model](https://github.com/IDEA-Research/OpenSeeD)
51
+ - [SEEM: Segment Everything Everywhere All at Once](https://github.com/UX-Decoder/Segment-Everything-Everywhere-All-At-Once)
52
+ - [X-GPT: Conversational Visual Agent supported by X-Decoder](https://github.com/microsoft/X-Decoder/tree/xgpt)
53
+ - [GLIGEN: Open-Set Grounded Text-to-Image Generation](https://github.com/gligen/GLIGEN)
54
+ - [LLaVA: Large Language and Vision Assistant](https://github.com/haotian-liu/LLaVA)
55
+
56
+ <!-- Extensions | [Grounding DINO with Segment Anything](https://github.com/IDEA-Research/Grounded-Segment-Anything); [Grounding DINO with Stable Diffusion](demo/image_editing_with_groundingdino_stablediffusion.ipynb); [Grounding DINO with GLIGEN](demo/image_editing_with_groundingdino_gligen.ipynb) -->
57
+
58
+
59
+
60
+ <!-- Official PyTorch implementation of [Grounding DINO](https://arxiv.org/abs/2303.05499), a stronger open-set object detector. Code is available now! -->
61
+
62
+
63
+ ## :bulb: Highlight
64
+
65
+ - **Open-Set Detection.** Detect **everything** with language!
66
+ - **High Performancce.** COCO zero-shot **52.5 AP** (training without COCO data!). COCO fine-tune **63.0 AP**.
67
+ - **Flexible.** Collaboration with Stable Diffusion for Image Editting.
68
+
69
+
70
+
71
+
72
+ ## :fire: News
73
+ - **`2023/07/18`**: We release [Semantic-SAM](https://github.com/UX-Decoder/Semantic-SAM), a universal image segmentation model to enable segment and recognize anything at any desired granularity. **Code** and **checkpoint** are available!
74
+ - **`2023/06/17`**: We provide an example to evaluate Grounding DINO on COCO zero-shot performance.
75
+ - **`2023/04/15`**: Refer to [CV in the Wild Readings](https://github.com/Computer-Vision-in-the-Wild/CVinW_Readings) for those who are interested in open-set recognition!
76
+ - **`2023/04/08`**: We release [demos](demo/image_editing_with_groundingdino_gligen.ipynb) to combine [Grounding DINO](https://arxiv.org/abs/2303.05499) with [GLIGEN](https://github.com/gligen/GLIGEN) for more controllable image editings.
77
+ - **`2023/04/08`**: We release [demos](demo/image_editing_with_groundingdino_stablediffusion.ipynb) to combine [Grounding DINO](https://arxiv.org/abs/2303.05499) with [Stable Diffusion](https://github.com/Stability-AI/StableDiffusion) for image editings.
78
+ - **`2023/04/06`**: We build a new demo by marrying GroundingDINO with [Segment-Anything](https://github.com/facebookresearch/segment-anything) named **[Grounded-Segment-Anything](https://github.com/IDEA-Research/Grounded-Segment-Anything)** aims to support segmentation in GroundingDINO.
79
+ - **`2023/03/28`**: A YouTube [video](https://youtu.be/cMa77r3YrDk) about Grounding DINO and basic object detection prompt engineering. [[SkalskiP](https://github.com/SkalskiP)]
80
+ - **`2023/03/28`**: Add a [demo](https://huggingface.co/spaces/ShilongLiu/Grounding_DINO_demo) on Hugging Face Space!
81
+ - **`2023/03/27`**: Support CPU-only mode. Now the model can run on machines without GPUs.
82
+ - **`2023/03/25`**: A [demo](https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/zero-shot-object-detection-with-grounding-dino.ipynb) for Grounding DINO is available at Colab. [[SkalskiP](https://github.com/SkalskiP)]
83
+ - **`2023/03/22`**: Code is available Now!
84
+
85
+ <details open>
86
+ <summary><font size="4">
87
+ Description
88
+ </font></summary>
89
+ <a href="https://arxiv.org/abs/2303.05499">Paper</a> introduction.
90
+ <img src=".asset/hero_figure.png" alt="ODinW" width="100%">
91
+ Marrying <a href="https://github.com/IDEA-Research/GroundingDINO">Grounding DINO</a> and <a href="https://github.com/gligen/GLIGEN">GLIGEN</a>
92
+ <img src="https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/GD_GLIGEN.png" alt="gd_gligen" width="100%">
93
+ </details>
94
+
95
+ ## :star: Explanations/Tips for Grounding DINO Inputs and Outputs
96
+ - Grounding DINO accepts an `(image, text)` pair as inputs.
97
+ - It outputs `900` (by default) object boxes. Each box has similarity scores across all input words. (as shown in Figures below.)
98
+ - We defaultly choose the boxes whose highest similarities are higher than a `box_threshold`.
99
+ - We extract the words whose similarities are higher than the `text_threshold` as predicted labels.
100
+ - If you want to obtain objects of specific phrases, like the `dogs` in the sentence `two dogs with a stick.`, you can select the boxes with highest text similarities with `dogs` as final outputs.
101
+ - Note that each word can be split to **more than one** tokens with different tokenlizers. The number of words in a sentence may not equal to the number of text tokens.
102
+ - We suggest separating different category names with `.` for Grounding DINO.
103
+ ![model_explain1](.asset/model_explan1.PNG)
104
+ ![model_explain2](.asset/model_explan2.PNG)
105
+
106
+ ## :label: TODO
107
+
108
+ - [x] Release inference code and demo.
109
+ - [x] Release checkpoints.
110
+ - [x] Grounding DINO with Stable Diffusion and GLIGEN demos.
111
+ - [ ] Release training codes.
112
+
113
+ ## :hammer_and_wrench: Install
114
+
115
+ **Note:**
116
+
117
+ 0. If you have a CUDA environment, please make sure the environment variable `CUDA_HOME` is set. It will be compiled under CPU-only mode if no CUDA available.
118
+
119
+ Please make sure following the installation steps strictly, otherwise the program may produce:
120
+ ```bash
121
+ NameError: name '_C' is not defined
122
+ ```
123
+
124
+ If this happened, please reinstalled the groundingDINO by reclone the git and do all the installation steps again.
125
+
126
+ #### how to check cuda:
127
+ ```bash
128
+ echo $CUDA_HOME
129
+ ```
130
+ If it print nothing, then it means you haven't set up the path/
131
+
132
+ Run this so the environment variable will be set under current shell.
133
+ ```bash
134
+ export CUDA_HOME=/path/to/cuda-11.3
135
+ ```
136
+
137
+ Notice the version of cuda should be aligned with your CUDA runtime, for there might exists multiple cuda at the same time.
138
+
139
+ If you want to set the CUDA_HOME permanently, store it using:
140
+
141
+ ```bash
142
+ echo 'export CUDA_HOME=/path/to/cuda' >> ~/.bashrc
143
+ ```
144
+ after that, source the bashrc file and check CUDA_HOME:
145
+ ```bash
146
+ source ~/.bashrc
147
+ echo $CUDA_HOME
148
+ ```
149
+
150
+ In this example, /path/to/cuda-11.3 should be replaced with the path where your CUDA toolkit is installed. You can find this by typing **which nvcc** in your terminal:
151
+
152
+ For instance,
153
+ if the output is /usr/local/cuda/bin/nvcc, then:
154
+ ```bash
155
+ export CUDA_HOME=/usr/local/cuda
156
+ ```
157
+ **Installation:**
158
+
159
+ 1.Clone the GroundingDINO repository from GitHub.
160
+
161
+ ```bash
162
+ git clone https://github.com/IDEA-Research/GroundingDINO.git
163
+ ```
164
+
165
+ 2. Change the current directory to the GroundingDINO folder.
166
+
167
+ ```bash
168
+ cd GroundingDINO/
169
+ ```
170
+
171
+ 3. Install the required dependencies in the current directory.
172
+
173
+ ```bash
174
+ pip install -e .
175
+ ```
176
+
177
+ 4. Download pre-trained model weights.
178
+
179
+ ```bash
180
+ mkdir weights
181
+ cd weights
182
+ wget -q https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
183
+ cd ..
184
+ ```
185
+
186
+ ## :arrow_forward: Demo
187
+ Check your GPU ID (only if you're using a GPU)
188
+
189
+ ```bash
190
+ nvidia-smi
191
+ ```
192
+ Replace `{GPU ID}`, `image_you_want_to_detect.jpg`, and `"dir you want to save the output"` with appropriate values in the following command
193
+ ```bash
194
+ CUDA_VISIBLE_DEVICES={GPU ID} python demo/inference_on_a_image.py \
195
+ -c groundingdino/config/GroundingDINO_SwinT_OGC.py \
196
+ -p weights/groundingdino_swint_ogc.pth \
197
+ -i image_you_want_to_detect.jpg \
198
+ -o "dir you want to save the output" \
199
+ -t "chair"
200
+ [--cpu-only] # open it for cpu mode
201
+ ```
202
+
203
+ If you would like to specify the phrases to detect, here is a demo:
204
+ ```bash
205
+ CUDA_VISIBLE_DEVICES={GPU ID} python demo/inference_on_a_image.py \
206
+ -c groundingdino/config/GroundingDINO_SwinT_OGC.py \
207
+ -p ./groundingdino_swint_ogc.pth \
208
+ -i .asset/cat_dog.jpeg \
209
+ -o logs/1111 \
210
+ -t "There is a cat and a dog in the image ." \
211
+ --token_spans "[[[9, 10], [11, 14]], [[19, 20], [21, 24]]]"
212
+ [--cpu-only] # open it for cpu mode
213
+ ```
214
+ The token_spans specify the start and end positions of a phrases. For example, the first phrase is `[[9, 10], [11, 14]]`. `"There is a cat and a dog in the image ."[9:10] = 'a'`, `"There is a cat and a dog in the image ."[11:14] = 'cat'`. Hence it refers to the phrase `a cat` . Similarly, the `[[19, 20], [21, 24]]` refers to the phrase `a dog`.
215
+
216
+ See the `demo/inference_on_a_image.py` for more details.
217
+
218
+ **Running with Python:**
219
+
220
+ ```python
221
+ from groundingdino.util.inference import load_model, load_image, predict, annotate
222
+ import cv2
223
+
224
+ model = load_model("groundingdino/config/GroundingDINO_SwinT_OGC.py", "weights/groundingdino_swint_ogc.pth")
225
+ IMAGE_PATH = "weights/dog-3.jpeg"
226
+ TEXT_PROMPT = "chair . person . dog ."
227
+ BOX_TRESHOLD = 0.35
228
+ TEXT_TRESHOLD = 0.25
229
+
230
+ image_source, image = load_image(IMAGE_PATH)
231
+
232
+ boxes, logits, phrases = predict(
233
+ model=model,
234
+ image=image,
235
+ caption=TEXT_PROMPT,
236
+ box_threshold=BOX_TRESHOLD,
237
+ text_threshold=TEXT_TRESHOLD
238
+ )
239
+
240
+ annotated_frame = annotate(image_source=image_source, boxes=boxes, logits=logits, phrases=phrases)
241
+ cv2.imwrite("annotated_image.jpg", annotated_frame)
242
+ ```
243
+ **Web UI**
244
+
245
+ We also provide a demo code to integrate Grounding DINO with Gradio Web UI. See the file `demo/gradio_app.py` for more details.
246
+
247
+ **Notebooks**
248
+
249
+ - We release [demos](demo/image_editing_with_groundingdino_gligen.ipynb) to combine [Grounding DINO](https://arxiv.org/abs/2303.05499) with [GLIGEN](https://github.com/gligen/GLIGEN) for more controllable image editings.
250
+ - We release [demos](demo/image_editing_with_groundingdino_stablediffusion.ipynb) to combine [Grounding DINO](https://arxiv.org/abs/2303.05499) with [Stable Diffusion](https://github.com/Stability-AI/StableDiffusion) for image editings.
251
+
252
+ ## COCO Zero-shot Evaluations
253
+
254
+ We provide an example to evaluate Grounding DINO zero-shot performance on COCO. The results should be **48.5**.
255
+
256
+ ```bash
257
+ CUDA_VISIBLE_DEVICES=0 \
258
+ python demo/test_ap_on_coco.py \
259
+ -c groundingdino/config/GroundingDINO_SwinT_OGC.py \
260
+ -p weights/groundingdino_swint_ogc.pth \
261
+ --anno_path /path/to/annoataions/ie/instances_val2017.json \
262
+ --image_dir /path/to/imagedir/ie/val2017
263
+ ```
264
+
265
+
266
+ ## :luggage: Checkpoints
267
+
268
+ <!-- insert a table -->
269
+ <table>
270
+ <thead>
271
+ <tr style="text-align: right;">
272
+ <th></th>
273
+ <th>name</th>
274
+ <th>backbone</th>
275
+ <th>Data</th>
276
+ <th>box AP on COCO</th>
277
+ <th>Checkpoint</th>
278
+ <th>Config</th>
279
+ </tr>
280
+ </thead>
281
+ <tbody>
282
+ <tr>
283
+ <th>1</th>
284
+ <td>GroundingDINO-T</td>
285
+ <td>Swin-T</td>
286
+ <td>O365,GoldG,Cap4M</td>
287
+ <td>48.4 (zero-shot) / 57.2 (fine-tune)</td>
288
+ <td><a href="https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth">GitHub link</a> | <a href="https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swint_ogc.pth">HF link</a></td>
289
+ <td><a href="https://github.com/IDEA-Research/GroundingDINO/blob/main/groundingdino/config/GroundingDINO_SwinT_OGC.py">link</a></td>
290
+ </tr>
291
+ <tr>
292
+ <th>2</th>
293
+ <td>GroundingDINO-B</td>
294
+ <td>Swin-B</td>
295
+ <td>COCO,O365,GoldG,Cap4M,OpenImage,ODinW-35,RefCOCO</td>
296
+ <td>56.7 </td>
297
+ <td><a href="https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha2/groundingdino_swinb_cogcoor.pth">GitHub link</a> | <a href="https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swinb_cogcoor.pth">HF link</a>
298
+ <td><a href="https://github.com/IDEA-Research/GroundingDINO/blob/main/groundingdino/config/GroundingDINO_SwinB_cfg.py">link</a></td>
299
+ </tr>
300
+ </tbody>
301
+ </table>
302
+
303
+ ## :medal_military: Results
304
+
305
+ <details open>
306
+ <summary><font size="4">
307
+ COCO Object Detection Results
308
+ </font></summary>
309
+ <img src=".asset/COCO.png" alt="COCO" width="100%">
310
+ </details>
311
+
312
+ <details open>
313
+ <summary><font size="4">
314
+ ODinW Object Detection Results
315
+ </font></summary>
316
+ <img src=".asset/ODinW.png" alt="ODinW" width="100%">
317
+ </details>
318
+
319
+ <details open>
320
+ <summary><font size="4">
321
+ Marrying Grounding DINO with <a href="https://github.com/Stability-AI/StableDiffusion">Stable Diffusion</a> for Image Editing
322
+ </font></summary>
323
+ See our example <a href="https://github.com/IDEA-Research/GroundingDINO/blob/main/demo/image_editing_with_groundingdino_stablediffusion.ipynb">notebook</a> for more details.
324
+ <img src=".asset/GD_SD.png" alt="GD_SD" width="100%">
325
+ </details>
326
+
327
+
328
+ <details open>
329
+ <summary><font size="4">
330
+ Marrying Grounding DINO with <a href="https://github.com/gligen/GLIGEN">GLIGEN</a> for more Detailed Image Editing.
331
+ </font></summary>
332
+ See our example <a href="https://github.com/IDEA-Research/GroundingDINO/blob/main/demo/image_editing_with_groundingdino_gligen.ipynb">notebook</a> for more details.
333
+ <img src=".asset/GD_GLIGEN.png" alt="GD_GLIGEN" width="100%">
334
+ </details>
335
+
336
+ ## :sauropod: Model: Grounding DINO
337
+
338
+ Includes: a text backbone, an image backbone, a feature enhancer, a language-guided query selection, and a cross-modality decoder.
339
+
340
+ ![arch](.asset/arch.png)
341
+
342
+
343
+ ## :hearts: Acknowledgement
344
+
345
+ Our model is related to [DINO](https://github.com/IDEA-Research/DINO) and [GLIP](https://github.com/microsoft/GLIP). Thanks for their great work!
346
+
347
+ We also thank great previous work including DETR, Deformable DETR, SMCA, Conditional DETR, Anchor DETR, Dynamic DETR, DAB-DETR, DN-DETR, etc. More related work are available at [Awesome Detection Transformer](https://github.com/IDEACVR/awesome-detection-transformer). A new toolbox [detrex](https://github.com/IDEA-Research/detrex) is available as well.
348
+
349
+ Thanks [Stable Diffusion](https://github.com/Stability-AI/StableDiffusion) and [GLIGEN](https://github.com/gligen/GLIGEN) for their awesome models.
350
+
351
+
352
+ ## :black_nib: Citation
353
+
354
+ If you find our work helpful for your research, please consider citing the following BibTeX entry.
355
+
356
+ ```bibtex
357
+ @article{liu2023grounding,
358
+ title={Grounding dino: Marrying dino with grounded pre-training for open-set object detection},
359
+ author={Liu, Shilong and Zeng, Zhaoyang and Ren, Tianhe and Li, Feng and Zhang, Hao and Yang, Jie and Li, Chunyuan and Yang, Jianwei and Su, Hang and Zhu, Jun and others},
360
+ journal={arXiv preprint arXiv:2303.05499},
361
+ year={2023}
362
+ }
363
+ ```
364
+
365
+
366
+
367
+
GroundingDINO/demo/create_coco_dataset.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import typer
2
+ from groundingdino.util.inference import load_model, load_image, predict
3
+ from tqdm import tqdm
4
+ import torchvision
5
+ import torch
6
+ import fiftyone as fo
7
+
8
+
9
+ def main(
10
+ image_directory: str = 'test_grounding_dino',
11
+ text_prompt: str = 'bus, car',
12
+ box_threshold: float = 0.15,
13
+ text_threshold: float = 0.10,
14
+ export_dataset: bool = False,
15
+ view_dataset: bool = False,
16
+ export_annotated_images: bool = True,
17
+ weights_path : str = "groundingdino_swint_ogc.pth",
18
+ config_path: str = "../../GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py",
19
+ subsample: int = None,
20
+ ):
21
+
22
+ model = load_model(config_path, weights_path)
23
+
24
+ dataset = fo.Dataset.from_images_dir(image_directory)
25
+
26
+ samples = []
27
+
28
+ if subsample is not None:
29
+
30
+ if subsample < len(dataset):
31
+ dataset = dataset.take(subsample).clone()
32
+
33
+ for sample in tqdm(dataset):
34
+
35
+ image_source, image = load_image(sample.filepath)
36
+
37
+ boxes, logits, phrases = predict(
38
+ model=model,
39
+ image=image,
40
+ caption=text_prompt,
41
+ box_threshold=box_threshold,
42
+ text_threshold=text_threshold,
43
+ )
44
+
45
+ detections = []
46
+
47
+ for box, logit, phrase in zip(boxes, logits, phrases):
48
+
49
+ rel_box = torchvision.ops.box_convert(box, 'cxcywh', 'xywh')
50
+
51
+ detections.append(
52
+ fo.Detection(
53
+ label=phrase,
54
+ bounding_box=rel_box,
55
+ confidence=logit,
56
+ ))
57
+
58
+ # Store detections in a field name of your choice
59
+ sample["detections"] = fo.Detections(detections=detections)
60
+ sample.save()
61
+
62
+ # loads the voxel fiftyone UI ready for viewing the dataset.
63
+ if view_dataset:
64
+ session = fo.launch_app(dataset)
65
+ session.wait()
66
+
67
+ # exports COCO dataset ready for training
68
+ if export_dataset:
69
+ dataset.export(
70
+ 'coco_dataset',
71
+ dataset_type=fo.types.COCODetectionDataset,
72
+ )
73
+
74
+ # saves bounding boxes plotted on the input images to disk
75
+ if export_annotated_images:
76
+ dataset.draw_labels(
77
+ 'images_with_bounding_boxes',
78
+ label_fields=['detections']
79
+ )
80
+
81
+
82
+ if __name__ == '__main__':
83
+ typer.run(main)
GroundingDINO/demo/gradio_app.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ from functools import partial
3
+ import cv2
4
+ import requests
5
+ import os
6
+ from io import BytesIO
7
+ from PIL import Image
8
+ import numpy as np
9
+ from pathlib import Path
10
+
11
+
12
+ import warnings
13
+
14
+ import torch
15
+
16
+ # prepare the environment
17
+ os.system("python setup.py build develop --user")
18
+ os.system("pip install packaging==21.3")
19
+ os.system("pip install gradio")
20
+
21
+
22
+ warnings.filterwarnings("ignore")
23
+
24
+ import gradio as gr
25
+
26
+ from groundingdino.models import build_model
27
+ from groundingdino.util.slconfig import SLConfig
28
+ from groundingdino.util.utils import clean_state_dict
29
+ from groundingdino.util.inference import annotate, load_image, predict
30
+ import groundingdino.datasets.transforms as T
31
+
32
+ from huggingface_hub import hf_hub_download
33
+
34
+
35
+
36
+ # Use this command for evaluate the Grounding DINO model
37
+ config_file = "groundingdino/config/GroundingDINO_SwinT_OGC.py"
38
+ ckpt_repo_id = "ShilongLiu/GroundingDINO"
39
+ ckpt_filenmae = "groundingdino_swint_ogc.pth"
40
+
41
+
42
+ def load_model_hf(model_config_path, repo_id, filename, device='cpu'):
43
+ args = SLConfig.fromfile(model_config_path)
44
+ model = build_model(args)
45
+ args.device = device
46
+
47
+ cache_file = hf_hub_download(repo_id=repo_id, filename=filename)
48
+ checkpoint = torch.load(cache_file, map_location='cpu')
49
+ log = model.load_state_dict(clean_state_dict(checkpoint['model']), strict=False)
50
+ print("Model loaded from {} \n => {}".format(cache_file, log))
51
+ _ = model.eval()
52
+ return model
53
+
54
+ def image_transform_grounding(init_image):
55
+ transform = T.Compose([
56
+ T.RandomResize([800], max_size=1333),
57
+ T.ToTensor(),
58
+ T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
59
+ ])
60
+ image, _ = transform(init_image, None) # 3, h, w
61
+ return init_image, image
62
+
63
+ def image_transform_grounding_for_vis(init_image):
64
+ transform = T.Compose([
65
+ T.RandomResize([800], max_size=1333),
66
+ ])
67
+ image, _ = transform(init_image, None) # 3, h, w
68
+ return image
69
+
70
+ model = load_model_hf(config_file, ckpt_repo_id, ckpt_filenmae)
71
+
72
+ def run_grounding(input_image, grounding_caption, box_threshold, text_threshold):
73
+ init_image = input_image.convert("RGB")
74
+ original_size = init_image.size
75
+
76
+ _, image_tensor = image_transform_grounding(init_image)
77
+ image_pil: Image = image_transform_grounding_for_vis(init_image)
78
+
79
+ # run grounidng
80
+ boxes, logits, phrases = predict(model, image_tensor, grounding_caption, box_threshold, text_threshold, device='cpu')
81
+ annotated_frame = annotate(image_source=np.asarray(image_pil), boxes=boxes, logits=logits, phrases=phrases)
82
+ image_with_box = Image.fromarray(cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB))
83
+
84
+
85
+ return image_with_box
86
+
87
+ if __name__ == "__main__":
88
+
89
+ parser = argparse.ArgumentParser("Grounding DINO demo", add_help=True)
90
+ parser.add_argument("--debug", action="store_true", help="using debug mode")
91
+ parser.add_argument("--share", action="store_true", help="share the app")
92
+ args = parser.parse_args()
93
+
94
+ block = gr.Blocks().queue()
95
+ with block:
96
+ gr.Markdown("# [Grounding DINO](https://github.com/IDEA-Research/GroundingDINO)")
97
+ gr.Markdown("### Open-World Detection with Grounding DINO")
98
+
99
+ with gr.Row():
100
+ with gr.Column():
101
+ input_image = gr.Image(source='upload', type="pil")
102
+ grounding_caption = gr.Textbox(label="Detection Prompt")
103
+ run_button = gr.Button(label="Run")
104
+ with gr.Accordion("Advanced options", open=False):
105
+ box_threshold = gr.Slider(
106
+ label="Box Threshold", minimum=0.0, maximum=1.0, value=0.25, step=0.001
107
+ )
108
+ text_threshold = gr.Slider(
109
+ label="Text Threshold", minimum=0.0, maximum=1.0, value=0.25, step=0.001
110
+ )
111
+
112
+ with gr.Column():
113
+ gallery = gr.outputs.Image(
114
+ type="pil",
115
+ # label="grounding results"
116
+ ).style(full_width=True, full_height=True)
117
+ # gallery = gr.Gallery(label="Generated images", show_label=False).style(
118
+ # grid=[1], height="auto", container=True, full_width=True, full_height=True)
119
+
120
+ run_button.click(fn=run_grounding, inputs=[
121
+ input_image, grounding_caption, box_threshold, text_threshold], outputs=[gallery])
122
+
123
+
124
+ block.launch(server_name='0.0.0.0', server_port=7579, debug=args.debug, share=args.share)
125
+
GroundingDINO/demo/image_editing_with_groundingdino_gligen.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
GroundingDINO/demo/image_editing_with_groundingdino_stablediffusion.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
GroundingDINO/demo/inference_on_a_image.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import sys
4
+
5
+ import numpy as np
6
+ import torch
7
+ from PIL import Image, ImageDraw, ImageFont
8
+
9
+ import groundingdino.datasets.transforms as T
10
+ from groundingdino.models import build_model
11
+ from groundingdino.util import box_ops
12
+ from groundingdino.util.slconfig import SLConfig
13
+ from groundingdino.util.utils import clean_state_dict, get_phrases_from_posmap
14
+ from groundingdino.util.vl_utils import create_positive_map_from_span
15
+
16
+
17
+ def plot_boxes_to_image(image_pil, tgt):
18
+ H, W = tgt["size"]
19
+ boxes = tgt["boxes"]
20
+ labels = tgt["labels"]
21
+ assert len(boxes) == len(labels), "boxes and labels must have same length"
22
+
23
+ draw = ImageDraw.Draw(image_pil)
24
+ mask = Image.new("L", image_pil.size, 0)
25
+ mask_draw = ImageDraw.Draw(mask)
26
+
27
+ # draw boxes and masks
28
+ for box, label in zip(boxes, labels):
29
+ # from 0..1 to 0..W, 0..H
30
+ box = box * torch.Tensor([W, H, W, H])
31
+ # from xywh to xyxy
32
+ box[:2] -= box[2:] / 2
33
+ box[2:] += box[:2]
34
+ # random color
35
+ color = tuple(np.random.randint(0, 255, size=3).tolist())
36
+ # draw
37
+ x0, y0, x1, y1 = box
38
+ x0, y0, x1, y1 = int(x0), int(y0), int(x1), int(y1)
39
+
40
+ draw.rectangle([x0, y0, x1, y1], outline=color, width=6)
41
+ # draw.text((x0, y0), str(label), fill=color)
42
+
43
+ font = ImageFont.load_default()
44
+ if hasattr(font, "getbbox"):
45
+ bbox = draw.textbbox((x0, y0), str(label), font)
46
+ else:
47
+ w, h = draw.textsize(str(label), font)
48
+ bbox = (x0, y0, w + x0, y0 + h)
49
+ # bbox = draw.textbbox((x0, y0), str(label))
50
+ draw.rectangle(bbox, fill=color)
51
+ draw.text((x0, y0), str(label), fill="white")
52
+
53
+ mask_draw.rectangle([x0, y0, x1, y1], fill=255, width=6)
54
+
55
+ return image_pil, mask
56
+
57
+
58
+ def load_image(image_path):
59
+ # load image
60
+ image_pil = Image.open(image_path).convert("RGB") # load image
61
+
62
+ transform = T.Compose(
63
+ [
64
+ T.RandomResize([800], max_size=1333),
65
+ T.ToTensor(),
66
+ T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
67
+ ]
68
+ )
69
+ image, _ = transform(image_pil, None) # 3, h, w
70
+ return image_pil, image
71
+
72
+
73
+ def load_model(model_config_path, model_checkpoint_path, cpu_only=False):
74
+ args = SLConfig.fromfile(model_config_path)
75
+ args.device = "cuda" if not cpu_only else "cpu"
76
+ model = build_model(args)
77
+ checkpoint = torch.load(model_checkpoint_path, map_location="cpu")
78
+ load_res = model.load_state_dict(clean_state_dict(checkpoint["model"]), strict=False)
79
+ print(load_res)
80
+ _ = model.eval()
81
+ return model
82
+
83
+
84
+ def get_grounding_output(model, image, caption, box_threshold, text_threshold=None, with_logits=True, cpu_only=False, token_spans=None):
85
+ assert text_threshold is not None or token_spans is not None, "text_threshould and token_spans should not be None at the same time!"
86
+ caption = caption.lower()
87
+ caption = caption.strip()
88
+ if not caption.endswith("."):
89
+ caption = caption + "."
90
+ device = "cuda" if not cpu_only else "cpu"
91
+ model = model.to(device)
92
+ image = image.to(device)
93
+ with torch.no_grad():
94
+ outputs = model(image[None], captions=[caption])
95
+ logits = outputs["pred_logits"].sigmoid()[0] # (nq, 256)
96
+ boxes = outputs["pred_boxes"][0] # (nq, 4)
97
+
98
+ # filter output
99
+ if token_spans is None:
100
+ logits_filt = logits.cpu().clone()
101
+ boxes_filt = boxes.cpu().clone()
102
+ filt_mask = logits_filt.max(dim=1)[0] > box_threshold
103
+ logits_filt = logits_filt[filt_mask] # num_filt, 256
104
+ boxes_filt = boxes_filt[filt_mask] # num_filt, 4
105
+
106
+ # get phrase
107
+ tokenlizer = model.tokenizer
108
+ tokenized = tokenlizer(caption)
109
+ # build pred
110
+ pred_phrases = []
111
+ for logit, box in zip(logits_filt, boxes_filt):
112
+ pred_phrase = get_phrases_from_posmap(logit > text_threshold, tokenized, tokenlizer)
113
+ if with_logits:
114
+ pred_phrases.append(pred_phrase + f"({str(logit.max().item())[:4]})")
115
+ else:
116
+ pred_phrases.append(pred_phrase)
117
+ else:
118
+ # given-phrase mode
119
+ positive_maps = create_positive_map_from_span(
120
+ model.tokenizer(text_prompt),
121
+ token_span=token_spans
122
+ ).to(image.device) # n_phrase, 256
123
+
124
+ logits_for_phrases = positive_maps @ logits.T # n_phrase, nq
125
+ all_logits = []
126
+ all_phrases = []
127
+ all_boxes = []
128
+ for (token_span, logit_phr) in zip(token_spans, logits_for_phrases):
129
+ # get phrase
130
+ phrase = ' '.join([caption[_s:_e] for (_s, _e) in token_span])
131
+ # get mask
132
+ filt_mask = logit_phr > box_threshold
133
+ # filt box
134
+ all_boxes.append(boxes[filt_mask])
135
+ # filt logits
136
+ all_logits.append(logit_phr[filt_mask])
137
+ if with_logits:
138
+ logit_phr_num = logit_phr[filt_mask]
139
+ all_phrases.extend([phrase + f"({str(logit.item())[:4]})" for logit in logit_phr_num])
140
+ else:
141
+ all_phrases.extend([phrase for _ in range(len(filt_mask))])
142
+ boxes_filt = torch.cat(all_boxes, dim=0).cpu()
143
+ pred_phrases = all_phrases
144
+
145
+
146
+ return boxes_filt, pred_phrases
147
+
148
+
149
+ if __name__ == "__main__":
150
+
151
+ parser = argparse.ArgumentParser("Grounding DINO example", add_help=True)
152
+ parser.add_argument("--config_file", "-c", type=str, required=True, help="path to config file")
153
+ parser.add_argument(
154
+ "--checkpoint_path", "-p", type=str, required=True, help="path to checkpoint file"
155
+ )
156
+ parser.add_argument("--image_path", "-i", type=str, required=True, help="path to image file")
157
+ parser.add_argument("--text_prompt", "-t", type=str, required=True, help="text prompt")
158
+ parser.add_argument(
159
+ "--output_dir", "-o", type=str, default="outputs", required=True, help="output directory"
160
+ )
161
+
162
+ parser.add_argument("--box_threshold", type=float, default=0.3, help="box threshold")
163
+ parser.add_argument("--text_threshold", type=float, default=0.25, help="text threshold")
164
+ parser.add_argument("--token_spans", type=str, default=None, help=
165
+ "The positions of start and end positions of phrases of interest. \
166
+ For example, a caption is 'a cat and a dog', \
167
+ if you would like to detect 'cat', the token_spans should be '[[[2, 5]], ]', since 'a cat and a dog'[2:5] is 'cat'. \
168
+ if you would like to detect 'a cat', the token_spans should be '[[[0, 1], [2, 5]], ]', since 'a cat and a dog'[0:1] is 'a', and 'a cat and a dog'[2:5] is 'cat'. \
169
+ ")
170
+
171
+ parser.add_argument("--cpu-only", action="store_true", help="running on cpu only!, default=False")
172
+ args = parser.parse_args()
173
+
174
+ # cfg
175
+ config_file = args.config_file # change the path of the model config file
176
+ checkpoint_path = args.checkpoint_path # change the path of the model
177
+ image_path = args.image_path
178
+ text_prompt = args.text_prompt
179
+ output_dir = args.output_dir
180
+ box_threshold = args.box_threshold
181
+ text_threshold = args.text_threshold
182
+ token_spans = args.token_spans
183
+
184
+ # make dir
185
+ os.makedirs(output_dir, exist_ok=True)
186
+ # load image
187
+ image_pil, image = load_image(image_path)
188
+ # load model
189
+ model = load_model(config_file, checkpoint_path, cpu_only=args.cpu_only)
190
+
191
+ # visualize raw image
192
+ image_pil.save(os.path.join(output_dir, "raw_image.jpg"))
193
+
194
+ # set the text_threshold to None if token_spans is set.
195
+ if token_spans is not None:
196
+ text_threshold = None
197
+ print("Using token_spans. Set the text_threshold to None.")
198
+
199
+
200
+ # run model
201
+ boxes_filt, pred_phrases = get_grounding_output(
202
+ model, image, text_prompt, box_threshold, text_threshold, cpu_only=args.cpu_only, token_spans=eval(f"{token_spans}")
203
+ )
204
+
205
+ # visualize pred
206
+ size = image_pil.size
207
+ pred_dict = {
208
+ "boxes": boxes_filt,
209
+ "size": [size[1], size[0]], # H,W
210
+ "labels": pred_phrases,
211
+ }
212
+ # import ipdb; ipdb.set_trace()
213
+ image_with_box = plot_boxes_to_image(image_pil, pred_dict)[0]
214
+ image_with_box.save(os.path.join(output_dir, "pred.jpg"))
GroundingDINO/demo/test_ap_on_coco.py ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import sys
4
+ import time
5
+
6
+ import numpy as np
7
+ import torch
8
+ import torch.nn as nn
9
+ from torch.utils.data import DataLoader, DistributedSampler
10
+
11
+ from groundingdino.models import build_model
12
+ import groundingdino.datasets.transforms as T
13
+ from groundingdino.util import box_ops, get_tokenlizer
14
+ from groundingdino.util.misc import clean_state_dict, collate_fn
15
+ from groundingdino.util.slconfig import SLConfig
16
+
17
+ # from torchvision.datasets import CocoDetection
18
+ import torchvision
19
+
20
+ from groundingdino.util.vl_utils import build_captions_and_token_span, create_positive_map_from_span
21
+ from groundingdino.datasets.cocogrounding_eval import CocoGroundingEvaluator
22
+
23
+
24
+ def load_model(model_config_path: str, model_checkpoint_path: str, device: str = "cuda"):
25
+ args = SLConfig.fromfile(model_config_path)
26
+ args.device = device
27
+ model = build_model(args)
28
+ checkpoint = torch.load(model_checkpoint_path, map_location="cpu")
29
+ model.load_state_dict(clean_state_dict(checkpoint["model"]), strict=False)
30
+ model.eval()
31
+ return model
32
+
33
+
34
+ class CocoDetection(torchvision.datasets.CocoDetection):
35
+ def __init__(self, img_folder, ann_file, transforms):
36
+ super().__init__(img_folder, ann_file)
37
+ self._transforms = transforms
38
+
39
+ def __getitem__(self, idx):
40
+ img, target = super().__getitem__(idx) # target: list
41
+
42
+ # import ipdb; ipdb.set_trace()
43
+
44
+ w, h = img.size
45
+ boxes = [obj["bbox"] for obj in target]
46
+ boxes = torch.as_tensor(boxes, dtype=torch.float32).reshape(-1, 4)
47
+ boxes[:, 2:] += boxes[:, :2] # xywh -> xyxy
48
+ boxes[:, 0::2].clamp_(min=0, max=w)
49
+ boxes[:, 1::2].clamp_(min=0, max=h)
50
+ # filt invalid boxes/masks/keypoints
51
+ keep = (boxes[:, 3] > boxes[:, 1]) & (boxes[:, 2] > boxes[:, 0])
52
+ boxes = boxes[keep]
53
+
54
+ target_new = {}
55
+ image_id = self.ids[idx]
56
+ target_new["image_id"] = image_id
57
+ target_new["boxes"] = boxes
58
+ target_new["orig_size"] = torch.as_tensor([int(h), int(w)])
59
+
60
+ if self._transforms is not None:
61
+ img, target = self._transforms(img, target_new)
62
+
63
+ return img, target
64
+
65
+
66
+ class PostProcessCocoGrounding(nn.Module):
67
+ """ This module converts the model's output into the format expected by the coco api"""
68
+
69
+ def __init__(self, num_select=300, coco_api=None, tokenlizer=None) -> None:
70
+ super().__init__()
71
+ self.num_select = num_select
72
+
73
+ assert coco_api is not None
74
+ category_dict = coco_api.dataset['categories']
75
+ cat_list = [item['name'] for item in category_dict]
76
+ captions, cat2tokenspan = build_captions_and_token_span(cat_list, True)
77
+ tokenspanlist = [cat2tokenspan[cat] for cat in cat_list]
78
+ positive_map = create_positive_map_from_span(
79
+ tokenlizer(captions), tokenspanlist) # 80, 256. normed
80
+
81
+ id_map = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10, 10: 11, 11: 13, 12: 14, 13: 15, 14: 16, 15: 17, 16: 18, 17: 19, 18: 20, 19: 21, 20: 22, 21: 23, 22: 24, 23: 25, 24: 27, 25: 28, 26: 31, 27: 32, 28: 33, 29: 34, 30: 35, 31: 36, 32: 37, 33: 38, 34: 39, 35: 40, 36: 41, 37: 42, 38: 43, 39: 44, 40: 46,
82
+ 41: 47, 42: 48, 43: 49, 44: 50, 45: 51, 46: 52, 47: 53, 48: 54, 49: 55, 50: 56, 51: 57, 52: 58, 53: 59, 54: 60, 55: 61, 56: 62, 57: 63, 58: 64, 59: 65, 60: 67, 61: 70, 62: 72, 63: 73, 64: 74, 65: 75, 66: 76, 67: 77, 68: 78, 69: 79, 70: 80, 71: 81, 72: 82, 73: 84, 74: 85, 75: 86, 76: 87, 77: 88, 78: 89, 79: 90}
83
+
84
+ # build a mapping from label_id to pos_map
85
+ new_pos_map = torch.zeros((91, 256))
86
+ for k, v in id_map.items():
87
+ new_pos_map[v] = positive_map[k]
88
+ self.positive_map = new_pos_map
89
+
90
+ @torch.no_grad()
91
+ def forward(self, outputs, target_sizes, not_to_xyxy=False):
92
+ """ Perform the computation
93
+ Parameters:
94
+ outputs: raw outputs of the model
95
+ target_sizes: tensor of dimension [batch_size x 2] containing the size of each images of the batch
96
+ For evaluation, this must be the original image size (before any data augmentation)
97
+ For visualization, this should be the image size after data augment, but before padding
98
+ """
99
+ num_select = self.num_select
100
+ out_logits, out_bbox = outputs['pred_logits'], outputs['pred_boxes']
101
+
102
+ # pos map to logit
103
+ prob_to_token = out_logits.sigmoid() # bs, 100, 256
104
+ pos_maps = self.positive_map.to(prob_to_token.device)
105
+ # (bs, 100, 256) @ (91, 256).T -> (bs, 100, 91)
106
+ prob_to_label = prob_to_token @ pos_maps.T
107
+
108
+ # if os.environ.get('IPDB_SHILONG_DEBUG', None) == 'INFO':
109
+ # import ipdb; ipdb.set_trace()
110
+
111
+ assert len(out_logits) == len(target_sizes)
112
+ assert target_sizes.shape[1] == 2
113
+
114
+ prob = prob_to_label
115
+ topk_values, topk_indexes = torch.topk(
116
+ prob.view(out_logits.shape[0], -1), num_select, dim=1)
117
+ scores = topk_values
118
+ topk_boxes = topk_indexes // prob.shape[2]
119
+ labels = topk_indexes % prob.shape[2]
120
+
121
+ if not_to_xyxy:
122
+ boxes = out_bbox
123
+ else:
124
+ boxes = box_ops.box_cxcywh_to_xyxy(out_bbox)
125
+
126
+ boxes = torch.gather(
127
+ boxes, 1, topk_boxes.unsqueeze(-1).repeat(1, 1, 4))
128
+
129
+ # and from relative [0, 1] to absolute [0, height] coordinates
130
+ img_h, img_w = target_sizes.unbind(1)
131
+ scale_fct = torch.stack([img_w, img_h, img_w, img_h], dim=1)
132
+ boxes = boxes * scale_fct[:, None, :]
133
+
134
+ results = [{'scores': s, 'labels': l, 'boxes': b}
135
+ for s, l, b in zip(scores, labels, boxes)]
136
+
137
+ return results
138
+
139
+
140
+ def main(args):
141
+ # config
142
+ cfg = SLConfig.fromfile(args.config_file)
143
+
144
+ # build model
145
+ model = load_model(args.config_file, args.checkpoint_path)
146
+ model = model.to(args.device)
147
+ model = model.eval()
148
+
149
+ # build dataloader
150
+ transform = T.Compose(
151
+ [
152
+ T.RandomResize([800], max_size=1333),
153
+ T.ToTensor(),
154
+ T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
155
+ ]
156
+ )
157
+ dataset = CocoDetection(
158
+ args.image_dir, args.anno_path, transforms=transform)
159
+ data_loader = DataLoader(
160
+ dataset, batch_size=1, shuffle=False, num_workers=args.num_workers, collate_fn=collate_fn)
161
+
162
+ # build post processor
163
+ tokenlizer = get_tokenlizer.get_tokenlizer(cfg.text_encoder_type)
164
+ postprocessor = PostProcessCocoGrounding(
165
+ coco_api=dataset.coco, tokenlizer=tokenlizer)
166
+
167
+ # build evaluator
168
+ evaluator = CocoGroundingEvaluator(
169
+ dataset.coco, iou_types=("bbox",), useCats=True)
170
+
171
+ # build captions
172
+ category_dict = dataset.coco.dataset['categories']
173
+ cat_list = [item['name'] for item in category_dict]
174
+ caption = " . ".join(cat_list) + ' .'
175
+ print("Input text prompt:", caption)
176
+
177
+ # run inference
178
+ start = time.time()
179
+ for i, (images, targets) in enumerate(data_loader):
180
+ # get images and captions
181
+ images = images.tensors.to(args.device)
182
+ bs = images.shape[0]
183
+ input_captions = [caption] * bs
184
+
185
+ # feed to the model
186
+ outputs = model(images, captions=input_captions)
187
+
188
+ orig_target_sizes = torch.stack(
189
+ [t["orig_size"] for t in targets], dim=0).to(images.device)
190
+ results = postprocessor(outputs, orig_target_sizes)
191
+ cocogrounding_res = {
192
+ target["image_id"]: output for target, output in zip(targets, results)}
193
+ evaluator.update(cocogrounding_res)
194
+
195
+ if (i+1) % 30 == 0:
196
+ used_time = time.time() - start
197
+ eta = len(data_loader) / (i+1e-5) * used_time - used_time
198
+ print(
199
+ f"processed {i}/{len(data_loader)} images. time: {used_time:.2f}s, ETA: {eta:.2f}s")
200
+
201
+ evaluator.synchronize_between_processes()
202
+ evaluator.accumulate()
203
+ evaluator.summarize()
204
+
205
+ print("Final results:", evaluator.coco_eval["bbox"].stats.tolist())
206
+
207
+
208
+ if __name__ == "__main__":
209
+ parser = argparse.ArgumentParser(
210
+ "Grounding DINO eval on COCO", add_help=True)
211
+ # load model
212
+ parser.add_argument("--config_file", "-c", type=str,
213
+ required=True, help="path to config file")
214
+ parser.add_argument(
215
+ "--checkpoint_path", "-p", type=str, required=True, help="path to checkpoint file"
216
+ )
217
+ parser.add_argument("--device", type=str, default="cuda",
218
+ help="running device (default: cuda)")
219
+
220
+ # post processing
221
+ parser.add_argument("--num_select", type=int, default=300,
222
+ help="number of topk to select")
223
+
224
+ # coco info
225
+ parser.add_argument("--anno_path", type=str,
226
+ required=True, help="coco root")
227
+ parser.add_argument("--image_dir", type=str,
228
+ required=True, help="coco image dir")
229
+ parser.add_argument("--num_workers", type=int, default=4,
230
+ help="number of workers for dataloader")
231
+ args = parser.parse_args()
232
+
233
+ main(args)
GroundingDINO/environment.yaml ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: dino
2
+ channels:
3
+ - pytorch
4
+ - nvidia
5
+ - conda-forge
6
+ - defaults
7
+ dependencies:
8
+ - addict=2.4.0=pyhd8ed1ab_2
9
+ - aiohttp=3.8.5=py39ha55989b_0
10
+ - aiosignal=1.3.1=pyhd8ed1ab_0
11
+ - asttokens=2.0.5=pyhd3eb1b0_0
12
+ - async-timeout=4.0.3=pyhd8ed1ab_0
13
+ - attrs=23.1.0=pyh71513ae_1
14
+ - aws-c-auth=0.7.0=h6f3c987_2
15
+ - aws-c-cal=0.6.0=h6ba3258_0
16
+ - aws-c-common=0.8.23=hcfcfb64_0
17
+ - aws-c-compression=0.2.17=h420beca_1
18
+ - aws-c-event-stream=0.3.1=had47b81_1
19
+ - aws-c-http=0.7.11=h72ba615_0
20
+ - aws-c-io=0.13.28=ha35c040_0
21
+ - aws-c-mqtt=0.8.14=h4941efa_2
22
+ - aws-c-s3=0.3.13=he04eaa7_2
23
+ - aws-c-sdkutils=0.1.11=h420beca_1
24
+ - aws-checksums=0.1.16=h420beca_1
25
+ - aws-crt-cpp=0.20.3=h247a981_4
26
+ - aws-sdk-cpp=1.10.57=h1a0519f_17
27
+ - backcall=0.2.0=pyhd3eb1b0_0
28
+ - blas=2.118=mkl
29
+ - blas-devel=3.9.0=18_win64_mkl
30
+ - brotli=1.0.9=hcfcfb64_9
31
+ - brotli-bin=1.0.9=hcfcfb64_9
32
+ - brotli-python=1.0.9=py39h99910a6_9
33
+ - bzip2=1.0.8=h8ffe710_4
34
+ - c-ares=1.19.1=hcfcfb64_0
35
+ - ca-certificates=2023.08.22=haa95532_0
36
+ - certifi=2023.7.22=py39haa95532_0
37
+ - charset-normalizer=3.2.0=pyhd8ed1ab_0
38
+ - click=8.1.7=win_pyh7428d3b_0
39
+ - colorama=0.4.6=pyhd8ed1ab_0
40
+ - comm=0.1.2=py39haa95532_0
41
+ - contourpy=1.1.1=py39h1f6ef14_1
42
+ - cuda-cccl=12.2.140=0
43
+ - cuda-cudart=11.8.89=0
44
+ - cuda-cudart-dev=11.8.89=0
45
+ - cuda-cupti=11.8.87=0
46
+ - cuda-libraries=11.8.0=0
47
+ - cuda-libraries-dev=11.8.0=0
48
+ - cuda-nvrtc=11.8.89=0
49
+ - cuda-nvrtc-dev=11.8.89=0
50
+ - cuda-nvtx=11.8.86=0
51
+ - cuda-profiler-api=12.2.140=0
52
+ - cuda-runtime=11.8.0=0
53
+ - cycler=0.11.0=pyhd8ed1ab_0
54
+ - cython=3.0.0=py39h2bbff1b_0
55
+ - dataclasses=0.8=pyhc8e2a94_3
56
+ - datasets=2.14.5=pyhd8ed1ab_0
57
+ - debugpy=1.6.7=py39hd77b12b_0
58
+ - decorator=5.1.1=pyhd3eb1b0_0
59
+ - dill=0.3.7=pyhd8ed1ab_0
60
+ - exceptiongroup=1.0.4=py39haa95532_0
61
+ - executing=0.8.3=pyhd3eb1b0_0
62
+ - filelock=3.12.4=pyhd8ed1ab_0
63
+ - fonttools=4.42.1=py39ha55989b_0
64
+ - freeglut=3.2.2=h63175ca_2
65
+ - freetype=2.12.1=hdaf720e_2
66
+ - frozenlist=1.4.0=py39ha55989b_1
67
+ - fsspec=2023.6.0=pyh1a96a4e_0
68
+ - gettext=0.21.1=h5728263_0
69
+ - glib=2.78.0=h12be248_0
70
+ - glib-tools=2.78.0=h12be248_0
71
+ - gst-plugins-base=1.22.6=h001b923_1
72
+ - gstreamer=1.22.6=hb4038d2_1
73
+ - huggingface_hub=0.17.3=pyhd8ed1ab_0
74
+ - icu=70.1=h0e60522_0
75
+ - idna=3.4=pyhd8ed1ab_0
76
+ - importlib-metadata=6.8.0=pyha770c72_0
77
+ - importlib-resources=6.1.0=pyhd8ed1ab_0
78
+ - importlib_metadata=6.8.0=hd8ed1ab_0
79
+ - importlib_resources=6.1.0=pyhd8ed1ab_0
80
+ - intel-openmp=2023.2.0=h57928b3_49503
81
+ - ipykernel=6.25.0=py39h9909e9c_0
82
+ - ipython=8.15.0=py39haa95532_0
83
+ - jasper=2.0.33=hc2e4405_1
84
+ - jedi=0.18.1=py39haa95532_1
85
+ - jinja2=3.1.2=pyhd8ed1ab_1
86
+ - joblib=1.3.2=pyhd8ed1ab_0
87
+ - jpeg=9e=hcfcfb64_3
88
+ - jupyter_client=8.1.0=py39haa95532_0
89
+ - jupyter_core=5.3.0=py39haa95532_0
90
+ - kiwisolver=1.4.5=py39h1f6ef14_1
91
+ - krb5=1.20.1=heb0366b_0
92
+ - lcms2=2.14=h90d422f_0
93
+ - lerc=4.0.0=h63175ca_0
94
+ - libabseil=20230125.3=cxx17_h63175ca_0
95
+ - libarrow=12.0.1=h12e5d06_5_cpu
96
+ - libblas=3.9.0=18_win64_mkl
97
+ - libbrotlicommon=1.0.9=hcfcfb64_9
98
+ - libbrotlidec=1.0.9=hcfcfb64_9
99
+ - libbrotlienc=1.0.9=hcfcfb64_9
100
+ - libcblas=3.9.0=18_win64_mkl
101
+ - libclang=15.0.7=default_h77d9078_3
102
+ - libclang13=15.0.7=default_h77d9078_3
103
+ - libcrc32c=1.1.2=h0e60522_0
104
+ - libcublas=11.11.3.6=0
105
+ - libcublas-dev=11.11.3.6=0
106
+ - libcufft=10.9.0.58=0
107
+ - libcufft-dev=10.9.0.58=0
108
+ - libcurand=10.3.3.141=0
109
+ - libcurand-dev=10.3.3.141=0
110
+ - libcurl=8.1.2=h68f0423_0
111
+ - libcusolver=11.4.1.48=0
112
+ - libcusolver-dev=11.4.1.48=0
113
+ - libcusparse=11.7.5.86=0
114
+ - libcusparse-dev=11.7.5.86=0
115
+ - libdeflate=1.14=hcfcfb64_0
116
+ - libevent=2.1.12=h3671451_1
117
+ - libffi=3.4.2=h8ffe710_5
118
+ - libglib=2.78.0=he8f3873_0
119
+ - libgoogle-cloud=2.12.0=h00b2bdc_1
120
+ - libgrpc=1.54.3=ha177ca7_0
121
+ - libhwloc=2.9.3=default_haede6df_1009
122
+ - libiconv=1.17=h8ffe710_0
123
+ - liblapack=3.9.0=18_win64_mkl
124
+ - liblapacke=3.9.0=18_win64_mkl
125
+ - libnpp=11.8.0.86=0
126
+ - libnpp-dev=11.8.0.86=0
127
+ - libnvjpeg=11.9.0.86=0
128
+ - libnvjpeg-dev=11.9.0.86=0
129
+ - libogg=1.3.4=h8ffe710_1
130
+ - libopencv=4.5.3=py39h488c12c_8
131
+ - libpng=1.6.39=h19919ed_0
132
+ - libprotobuf=3.21.12=h12be248_2
133
+ - libsodium=1.0.18=h62dcd97_0
134
+ - libsqlite=3.43.0=hcfcfb64_0
135
+ - libssh2=1.11.0=h7dfc565_0
136
+ - libthrift=0.18.1=h06f6336_2
137
+ - libtiff=4.4.0=hc4f729c_5
138
+ - libutf8proc=2.8.0=h82a8f57_0
139
+ - libuv=1.44.2=hcfcfb64_1
140
+ - libvorbis=1.3.7=h0e60522_0
141
+ - libwebp-base=1.3.2=hcfcfb64_0
142
+ - libxcb=1.13=hcd874cb_1004
143
+ - libxml2=2.11.5=hc3477c8_1
144
+ - libzlib=1.2.13=hcfcfb64_5
145
+ - lz4-c=1.9.4=hcfcfb64_0
146
+ - m2w64-gcc-libgfortran=5.3.0=6
147
+ - m2w64-gcc-libs=5.3.0=7
148
+ - m2w64-gcc-libs-core=5.3.0=7
149
+ - m2w64-gmp=6.1.0=2
150
+ - m2w64-libwinpthread-git=5.0.0.4634.697f757=2
151
+ - markupsafe=2.1.3=py39ha55989b_1
152
+ - matplotlib-base=3.8.0=py39hf19769e_1
153
+ - matplotlib-inline=0.1.6=py39haa95532_0
154
+ - mkl=2022.1.0=h6a75c08_874
155
+ - mkl-devel=2022.1.0=h57928b3_875
156
+ - mkl-include=2022.1.0=h6a75c08_874
157
+ - mpmath=1.3.0=pyhd8ed1ab_0
158
+ - msys2-conda-epoch=20160418=1
159
+ - multidict=6.0.4=py39ha55989b_0
160
+ - multiprocess=0.70.15=py39ha55989b_1
161
+ - munkres=1.1.4=pyh9f0ad1d_0
162
+ - nest-asyncio=1.5.6=py39haa95532_0
163
+ - networkx=3.1=pyhd8ed1ab_0
164
+ - numpy=1.26.0=py39hddb5d58_0
165
+ - opencv=4.5.3=py39hcbf5309_8
166
+ - openjpeg=2.5.0=hc9384bd_1
167
+ - openssl=3.1.3=hcfcfb64_0
168
+ - orc=1.9.0=hada7b9e_1
169
+ - packaging=23.1=pyhd8ed1ab_0
170
+ - pandas=2.1.1=py39h32e6231_0
171
+ - parso=0.8.3=pyhd3eb1b0_0
172
+ - pcre2=10.40=h17e33f8_0
173
+ - pickleshare=0.7.5=pyhd3eb1b0_1003
174
+ - pillow=9.2.0=py39h595c93f_3
175
+ - pip=23.2.1=pyhd8ed1ab_0
176
+ - platformdirs=3.10.0=pyhd8ed1ab_0
177
+ - prompt-toolkit=3.0.36=py39haa95532_0
178
+ - psutil=5.9.0=py39h2bbff1b_0
179
+ - pthread-stubs=0.4=hcd874cb_1001
180
+ - pthreads-win32=2.9.1=hfa6e2cd_3
181
+ - pure_eval=0.2.2=pyhd3eb1b0_0
182
+ - py-opencv=4.5.3=py39h00e5391_8
183
+ - pyarrow=12.0.1=py39hca4e8af_5_cpu
184
+ - pycocotools=2.0.6=py39hc266a54_1
185
+ - pygments=2.15.1=py39haa95532_1
186
+ - pyparsing=3.1.1=pyhd8ed1ab_0
187
+ - pysocks=1.7.1=pyh0701188_6
188
+ - python=3.9.18=h4de0772_0_cpython
189
+ - python-dateutil=2.8.2=pyhd8ed1ab_0
190
+ - python-tzdata=2023.3=pyhd8ed1ab_0
191
+ - python-xxhash=3.3.0=py39ha55989b_1
192
+ - python_abi=3.9=4_cp39
193
+ - pytorch=2.0.1=py3.9_cuda11.8_cudnn8_0
194
+ - pytorch-cuda=11.8=h24eeafa_5
195
+ - pytorch-mutex=1.0=cuda
196
+ - pytz=2023.3.post1=pyhd8ed1ab_0
197
+ - pywin32=305=py39h2bbff1b_0
198
+ - pyyaml=6.0.1=py39ha55989b_1
199
+ - pyzmq=25.1.0=py39hd77b12b_0
200
+ - qt-main=5.15.8=h720456b_6
201
+ - re2=2023.03.02=hd4eee63_0
202
+ - regex=2023.8.8=py39ha55989b_1
203
+ - requests=2.31.0=pyhd8ed1ab_0
204
+ - sacremoses=0.0.53=pyhd8ed1ab_0
205
+ - safetensors=0.3.3=py39hf21820d_1
206
+ - setuptools=68.2.2=pyhd8ed1ab_0
207
+ - six=1.16.0=pyh6c4a22f_0
208
+ - snappy=1.1.10=hfb803bf_0
209
+ - stack_data=0.2.0=pyhd3eb1b0_0
210
+ - sympy=1.12=pyh04b8f61_3
211
+ - tbb=2021.10.0=h91493d7_1
212
+ - timm=0.9.7=pyhd8ed1ab_0
213
+ - tk=8.6.13=hcfcfb64_0
214
+ - tokenizers=0.13.3=py39hca44cb7_0
215
+ - tomli=2.0.1=pyhd8ed1ab_0
216
+ - tornado=6.3.2=py39h2bbff1b_0
217
+ - tqdm=4.66.1=pyhd8ed1ab_0
218
+ - traitlets=5.7.1=py39haa95532_0
219
+ - transformers=4.33.2=pyhd8ed1ab_0
220
+ - typing-extensions=4.8.0=hd8ed1ab_0
221
+ - typing_extensions=4.8.0=pyha770c72_0
222
+ - tzdata=2023c=h71feb2d_0
223
+ - ucrt=10.0.22621.0=h57928b3_0
224
+ - unicodedata2=15.0.0=py39ha55989b_1
225
+ - urllib3=2.0.5=pyhd8ed1ab_0
226
+ - vc=14.3=h64f974e_17
227
+ - vc14_runtime=14.36.32532=hdcecf7f_17
228
+ - vs2015_runtime=14.36.32532=h05e6639_17
229
+ - wcwidth=0.2.5=pyhd3eb1b0_0
230
+ - wheel=0.41.2=pyhd8ed1ab_0
231
+ - win_inet_pton=1.1.0=pyhd8ed1ab_6
232
+ - xorg-libxau=1.0.11=hcd874cb_0
233
+ - xorg-libxdmcp=1.1.3=hcd874cb_0
234
+ - xxhash=0.8.2=hcfcfb64_0
235
+ - xz=5.2.6=h8d14728_0
236
+ - yaml=0.2.5=h8ffe710_2
237
+ - yapf=0.40.1=pyhd8ed1ab_0
238
+ - yarl=1.9.2=py39ha55989b_0
239
+ - zeromq=4.3.4=hd77b12b_0
240
+ - zipp=3.17.0=pyhd8ed1ab_0
241
+ - zlib=1.2.13=hcfcfb64_5
242
+ - zstd=1.5.5=h12be248_0
243
+ - pip:
244
+ - opencv-python==4.8.0.76
245
+ - supervision==0.6.0
246
+ - torchaudio==2.0.2
247
+ - torchvision==0.15.2
248
+ prefix: C:\Users\Makoto\miniconda3\envs\dino
GroundingDINO/groundingdino.egg-info/PKG-INFO ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.1
2
+ Name: groundingdino
3
+ Version: 0.1.0
4
+ Summary: open-set object detector
5
+ Home-page: https://github.com/IDEA-Research/GroundingDINO
6
+ Author: International Digital Economy Academy, Shilong Liu
7
+ License: Apache License
8
+ Version 2.0, January 2004
9
+ http://www.apache.org/licenses/
10
+
11
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
12
+
13
+ 1. Definitions.
14
+
15
+ "License" shall mean the terms and conditions for use, reproduction,
16
+ and distribution as defined by Sections 1 through 9 of this document.
17
+
18
+ "Licensor" shall mean the copyright owner or entity authorized by
19
+ the copyright owner that is granting the License.
20
+
21
+ "Legal Entity" shall mean the union of the acting entity and all
22
+ other entities that control, are controlled by, or are under common
23
+ control with that entity. For the purposes of this definition,
24
+ "control" means (i) the power, direct or indirect, to cause the
25
+ direction or management of such entity, whether by contract or
26
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
27
+ outstanding shares, or (iii) beneficial ownership of such entity.
28
+
29
+ "You" (or "Your") shall mean an individual or Legal Entity
30
+ exercising permissions granted by this License.
31
+
32
+ "Source" form shall mean the preferred form for making modifications,
33
+ including but not limited to software source code, documentation
34
+ source, and configuration files.
35
+
36
+ "Object" form shall mean any form resulting from mechanical
37
+ transformation or translation of a Source form, including but
38
+ not limited to compiled object code, generated documentation,
39
+ and conversions to other media types.
40
+
41
+ "Work" shall mean the work of authorship, whether in Source or
42
+ Object form, made available under the License, as indicated by a
43
+ copyright notice that is included in or attached to the work
44
+ (an example is provided in the Appendix below).
45
+
46
+ "Derivative Works" shall mean any work, whether in Source or Object
47
+ form, that is based on (or derived from) the Work and for which the
48
+ editorial revisions, annotations, elaborations, or other modifications
49
+ represent, as a whole, an original work of authorship. For the purposes
50
+ of this License, Derivative Works shall not include works that remain
51
+ separable from, or merely link (or bind by name) to the interfaces of,
52
+ the Work and Derivative Works thereof.
53
+
54
+ "Contribution" shall mean any work of authorship, including
55
+ the original version of the Work and any modifications or additions
56
+ to that Work or Derivative Works thereof, that is intentionally
57
+ submitted to Licensor for inclusion in the Work by the copyright owner
58
+ or by an individual or Legal Entity authorized to submit on behalf of
59
+ the copyright owner. For the purposes of this definition, "submitted"
60
+ means any form of electronic, verbal, or written communication sent
61
+ to the Licensor or its representatives, including but not limited to
62
+ communication on electronic mailing lists, source code control systems,
63
+ and issue tracking systems that are managed by, or on behalf of, the
64
+ Licensor for the purpose of discussing and improving the Work, but
65
+ excluding communication that is conspicuously marked or otherwise
66
+ designated in writing by the copyright owner as "Not a Contribution."
67
+
68
+ "Contributor" shall mean Licensor and any individual or Legal Entity
69
+ on behalf of whom a Contribution has been received by Licensor and
70
+ subsequently incorporated within the Work.
71
+
72
+ 2. Grant of Copyright License. Subject to the terms and conditions of
73
+ this License, each Contributor hereby grants to You a perpetual,
74
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
75
+ copyright license to reproduce, prepare Derivative Works of,
76
+ publicly display, publicly perform, sublicense, and distribute the
77
+ Work and such Derivative Works in Source or Object form.
78
+
79
+ 3. Grant of Patent License. Subject to the terms and conditions of
80
+ this License, each Contributor hereby grants to You a perpetual,
81
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
82
+ (except as stated in this section) patent license to make, have made,
83
+ use, offer to sell, sell, import, and otherwise transfer the Work,
84
+ where such license applies only to those patent claims licensable
85
+ by such Contributor that are necessarily infringed by their
86
+ Contribution(s) alone or by combination of their Contribution(s)
87
+ with the Work to which such Contribution(s) was submitted. If You
88
+ institute patent litigation against any entity (including a
89
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
90
+ or a Contribution incorporated within the Work constitutes direct
91
+ or contributory patent infringement, then any patent licenses
92
+ granted to You under this License for that Work shall terminate
93
+ as of the date such litigation is filed.
94
+
95
+ 4. Redistribution. You may reproduce and distribute copies of the
96
+ Work or Derivative Works thereof in any medium, with or without
97
+ modifications, and in Source or Object form, provided that You
98
+ meet the following conditions:
99
+
100
+ (a) You must give any other recipients of the Work or
101
+ Derivative Works a copy of this License; and
102
+
103
+ (b) You must cause any modified files to carry prominent notices
104
+ stating that You changed the files; and
105
+
106
+ (c) You must retain, in the Source form of any Derivative Works
107
+ that You distribute, all copyright, patent, trademark, and
108
+ attribution notices from the Source form of the Work,
109
+ excluding those notices that do not pertain to any part of
110
+ the Derivative Works; and
111
+
112
+ (d) If the Work includes a "NOTICE" text file as part of its
113
+ distribution, then any Derivative Works that You distribute must
114
+ include a readable copy of the attribution notices contained
115
+ within such NOTICE file, excluding those notices that do not
116
+ pertain to any part of the Derivative Works, in at least one
117
+ of the following places: within a NOTICE text file distributed
118
+ as part of the Derivative Works; within the Source form or
119
+ documentation, if provided along with the Derivative Works; or,
120
+ within a display generated by the Derivative Works, if and
121
+ wherever such third-party notices normally appear. The contents
122
+ of the NOTICE file are for informational purposes only and
123
+ do not modify the License. You may add Your own attribution
124
+ notices within Derivative Works that You distribute, alongside
125
+ or as an addendum to the NOTICE text from the Work, provided
126
+ that such additional attribution notices cannot be construed
127
+ as modifying the License.
128
+
129
+ You may add Your own copyright statement to Your modifications and
130
+ may provide additional or different license terms and conditions
131
+ for use, reproduction, or distribution of Your modifications, or
132
+ for any such Derivative Works as a whole, provided Your use,
133
+ reproduction, and distribution of the Work otherwise complies with
134
+ the conditions stated in this License.
135
+
136
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
137
+ any Contribution intentionally submitted for inclusion in the Work
138
+ by You to the Licensor shall be under the terms and conditions of
139
+ this License, without any additional terms or conditions.
140
+ Notwithstanding the above, nothing herein shall supersede or modify
141
+ the terms of any separate license agreement you may have executed
142
+ with Licensor regarding such Contributions.
143
+
144
+ 6. Trademarks. This License does not grant permission to use the trade
145
+ names, trademarks, service marks, or product names of the Licensor,
146
+ except as required for reasonable and customary use in describing the
147
+ origin of the Work and reproducing the content of the NOTICE file.
148
+
149
+ 7. Disclaimer of Warranty. Unless required by applicable law or
150
+ agreed to in writing, Licensor provides the Work (and each
151
+ Contributor provides its Contributions) on an "AS IS" BASIS,
152
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
153
+ implied, including, without limitation, any warranties or conditions
154
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
155
+ PARTICULAR PURPOSE. You are solely responsible for determining the
156
+ appropriateness of using or redistributing the Work and assume any
157
+ risks associated with Your exercise of permissions under this License.
158
+
159
+ 8. Limitation of Liability. In no event and under no legal theory,
160
+ whether in tort (including negligence), contract, or otherwise,
161
+ unless required by applicable law (such as deliberate and grossly
162
+ negligent acts) or agreed to in writing, shall any Contributor be
163
+ liable to You for damages, including any direct, indirect, special,
164
+ incidental, or consequential damages of any character arising as a
165
+ result of this License or out of the use or inability to use the
166
+ Work (including but not limited to damages for loss of goodwill,
167
+ work stoppage, computer failure or malfunction, or any and all
168
+ other commercial damages or losses), even if such Contributor
169
+ has been advised of the possibility of such damages.
170
+
171
+ 9. Accepting Warranty or Additional Liability. While redistributing
172
+ the Work or Derivative Works thereof, You may choose to offer,
173
+ and charge a fee for, acceptance of support, warranty, indemnity,
174
+ or other liability obligations and/or rights consistent with this
175
+ License. However, in accepting such obligations, You may act only
176
+ on Your own behalf and on Your sole responsibility, not on behalf
177
+ of any other Contributor, and only if You agree to indemnify,
178
+ defend, and hold each Contributor harmless for any liability
179
+ incurred by, or claims asserted against, such Contributor by reason
180
+ of your accepting any such warranty or additional liability.
181
+
182
+ END OF TERMS AND CONDITIONS
183
+
184
+ APPENDIX: How to apply the Apache License to your work.
185
+
186
+ To apply the Apache License to your work, attach the following
187
+ boilerplate notice, with the fields enclosed by brackets "[]"
188
+ replaced with your own identifying information. (Don't include
189
+ the brackets!) The text should be enclosed in the appropriate
190
+ comment syntax for the file format. We also recommend that a
191
+ file or class name and description of purpose be included on the
192
+ same "printed page" as the copyright notice for easier
193
+ identification within third-party archives.
194
+
195
+ Copyright 2023 - present, IDEA Research.
196
+
197
+ Licensed under the Apache License, Version 2.0 (the "License");
198
+ you may not use this file except in compliance with the License.
199
+ You may obtain a copy of the License at
200
+
201
+ http://www.apache.org/licenses/LICENSE-2.0
202
+
203
+ Unless required by applicable law or agreed to in writing, software
204
+ distributed under the License is distributed on an "AS IS" BASIS,
205
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
206
+ See the License for the specific language governing permissions and
207
+ limitations under the License.
208
+
209
+ Platform: UNKNOWN
210
+ License-File: LICENSE
211
+
212
+ UNKNOWN
213
+
GroundingDINO/groundingdino.egg-info/SOURCES.txt ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ LICENSE
2
+ README.md
3
+ setup.py
4
+ groundingdino/__init__.py
5
+ groundingdino/version.py
6
+ groundingdino.egg-info/PKG-INFO
7
+ groundingdino.egg-info/SOURCES.txt
8
+ groundingdino.egg-info/dependency_links.txt
9
+ groundingdino.egg-info/requires.txt
10
+ groundingdino.egg-info/top_level.txt
11
+ groundingdino/config/GroundingDINO_SwinB_cfg.py
12
+ groundingdino/config/GroundingDINO_SwinT_OGC.py
13
+ groundingdino/config/__init__.py
14
+ groundingdino/datasets/__init__.py
15
+ groundingdino/datasets/cocogrounding_eval.py
16
+ groundingdino/datasets/transforms.py
17
+ groundingdino/models/__init__.py
18
+ groundingdino/models/registry.py
19
+ groundingdino/models/GroundingDINO/__init__.py
20
+ groundingdino/models/GroundingDINO/bertwarper.py
21
+ groundingdino/models/GroundingDINO/fuse_modules.py
22
+ groundingdino/models/GroundingDINO/groundingdino.py
23
+ groundingdino/models/GroundingDINO/ms_deform_attn.py
24
+ groundingdino/models/GroundingDINO/transformer.py
25
+ groundingdino/models/GroundingDINO/transformer_vanilla.py
26
+ groundingdino/models/GroundingDINO/utils.py
27
+ groundingdino/models/GroundingDINO/backbone/__init__.py
28
+ groundingdino/models/GroundingDINO/backbone/backbone.py
29
+ groundingdino/models/GroundingDINO/backbone/position_encoding.py
30
+ groundingdino/models/GroundingDINO/backbone/swin_transformer.py
31
+ groundingdino/util/__init__.py
32
+ groundingdino/util/box_ops.py
33
+ groundingdino/util/get_tokenlizer.py
34
+ groundingdino/util/inference.py
35
+ groundingdino/util/logger.py
36
+ groundingdino/util/misc.py
37
+ groundingdino/util/slconfig.py
38
+ groundingdino/util/slio.py
39
+ groundingdino/util/time_counter.py
40
+ groundingdino/util/utils.py
41
+ groundingdino/util/visualizer.py
42
+ groundingdino/util/vl_utils.py
GroundingDINO/groundingdino.egg-info/dependency_links.txt ADDED
@@ -0,0 +1 @@
 
 
1
+
GroundingDINO/groundingdino.egg-info/requires.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ transformers
4
+ addict
5
+ yapf
6
+ timm
7
+ numpy
8
+ opencv-python
9
+ supervision
10
+ pycocotools
GroundingDINO/groundingdino.egg-info/top_level.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ groundingdino
GroundingDINO/groundingdino/__init__.py ADDED
File without changes
GroundingDINO/groundingdino/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (167 Bytes). View file
 
GroundingDINO/groundingdino/config/GroundingDINO_SwinB_cfg.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ batch_size = 1
2
+ modelname = "groundingdino"
3
+ backbone = "swin_B_384_22k"
4
+ position_embedding = "sine"
5
+ pe_temperatureH = 20
6
+ pe_temperatureW = 20
7
+ return_interm_indices = [1, 2, 3]
8
+ backbone_freeze_keywords = None
9
+ enc_layers = 6
10
+ dec_layers = 6
11
+ pre_norm = False
12
+ dim_feedforward = 2048
13
+ hidden_dim = 256
14
+ dropout = 0.0
15
+ nheads = 8
16
+ num_queries = 900
17
+ query_dim = 4
18
+ num_patterns = 0
19
+ num_feature_levels = 4
20
+ enc_n_points = 4
21
+ dec_n_points = 4
22
+ two_stage_type = "standard"
23
+ two_stage_bbox_embed_share = False
24
+ two_stage_class_embed_share = False
25
+ transformer_activation = "relu"
26
+ dec_pred_bbox_embed_share = True
27
+ dn_box_noise_scale = 1.0
28
+ dn_label_noise_ratio = 0.5
29
+ dn_label_coef = 1.0
30
+ dn_bbox_coef = 1.0
31
+ embed_init_tgt = True
32
+ dn_labelbook_size = 2000
33
+ max_text_len = 256
34
+ text_encoder_type = "bert-base-uncased"
35
+ use_text_enhancer = True
36
+ use_fusion_layer = True
37
+ use_checkpoint = True
38
+ use_transformer_ckpt = True
39
+ use_text_cross_attention = True
40
+ text_dropout = 0.0
41
+ fusion_dropout = 0.0
42
+ fusion_droppath = 0.1
43
+ sub_sentence_present = True
GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ batch_size = 1
2
+ modelname = "groundingdino"
3
+ backbone = "swin_T_224_1k"
4
+ position_embedding = "sine"
5
+ pe_temperatureH = 20
6
+ pe_temperatureW = 20
7
+ return_interm_indices = [1, 2, 3]
8
+ backbone_freeze_keywords = None
9
+ enc_layers = 6
10
+ dec_layers = 6
11
+ pre_norm = False
12
+ dim_feedforward = 2048
13
+ hidden_dim = 256
14
+ dropout = 0.0
15
+ nheads = 8
16
+ num_queries = 900
17
+ query_dim = 4
18
+ num_patterns = 0
19
+ num_feature_levels = 4
20
+ enc_n_points = 4
21
+ dec_n_points = 4
22
+ two_stage_type = "standard"
23
+ two_stage_bbox_embed_share = False
24
+ two_stage_class_embed_share = False
25
+ transformer_activation = "relu"
26
+ dec_pred_bbox_embed_share = True
27
+ dn_box_noise_scale = 1.0
28
+ dn_label_noise_ratio = 0.5
29
+ dn_label_coef = 1.0
30
+ dn_bbox_coef = 1.0
31
+ embed_init_tgt = True
32
+ dn_labelbook_size = 2000
33
+ max_text_len = 256
34
+ text_encoder_type = "bert-base-uncased"
35
+ use_text_enhancer = True
36
+ use_fusion_layer = True
37
+ use_checkpoint = True
38
+ use_transformer_ckpt = True
39
+ use_text_cross_attention = True
40
+ text_dropout = 0.0
41
+ fusion_dropout = 0.0
42
+ fusion_droppath = 0.1
43
+ sub_sentence_present = True
GroundingDINO/groundingdino/config/__init__.py ADDED
File without changes
GroundingDINO/groundingdino/datasets/__init__.py ADDED
File without changes
GroundingDINO/groundingdino/datasets/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (176 Bytes). View file
 
GroundingDINO/groundingdino/datasets/__pycache__/transforms.cpython-39.pyc ADDED
Binary file (10.3 kB). View file
 
GroundingDINO/groundingdino/datasets/cocogrounding_eval.py ADDED
@@ -0,0 +1,269 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------
2
+ # Grounding DINO. Midified by Shilong Liu.
3
+ # url: https://github.com/IDEA-Research/GroundingDINO
4
+ # Copyright (c) 2023 IDEA. All Rights Reserved.
5
+ # Licensed under the Apache License, Version 2.0 [see LICENSE for details]
6
+ # ------------------------------------------------------------------------
7
+ # Copyright (c) Aishwarya Kamath & Nicolas Carion. Licensed under the Apache License 2.0. All Rights Reserved
8
+ # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
9
+ """
10
+ COCO evaluator that works in distributed mode.
11
+
12
+ Mostly copy-paste from https://github.com/pytorch/vision/blob/edfd5a7/references/detection/coco_eval.py
13
+ The difference is that there is less copy-pasting from pycocotools
14
+ in the end of the file, as python3 can suppress prints with contextlib
15
+ """
16
+ import contextlib
17
+ import copy
18
+ import os
19
+
20
+ import numpy as np
21
+ import pycocotools.mask as mask_util
22
+ import torch
23
+ from pycocotools.coco import COCO
24
+ from pycocotools.cocoeval import COCOeval
25
+
26
+ from groundingdino.util.misc import all_gather
27
+
28
+
29
+ class CocoGroundingEvaluator(object):
30
+ def __init__(self, coco_gt, iou_types, useCats=True):
31
+ assert isinstance(iou_types, (list, tuple))
32
+ coco_gt = copy.deepcopy(coco_gt)
33
+ self.coco_gt = coco_gt
34
+
35
+ self.iou_types = iou_types
36
+ self.coco_eval = {}
37
+ for iou_type in iou_types:
38
+ self.coco_eval[iou_type] = COCOeval(coco_gt, iouType=iou_type)
39
+ self.coco_eval[iou_type].useCats = useCats
40
+
41
+ self.img_ids = []
42
+ self.eval_imgs = {k: [] for k in iou_types}
43
+ self.useCats = useCats
44
+
45
+ def update(self, predictions):
46
+ img_ids = list(np.unique(list(predictions.keys())))
47
+ self.img_ids.extend(img_ids)
48
+
49
+ for iou_type in self.iou_types:
50
+ results = self.prepare(predictions, iou_type)
51
+
52
+ # suppress pycocotools prints
53
+ with open(os.devnull, "w") as devnull:
54
+ with contextlib.redirect_stdout(devnull):
55
+ coco_dt = COCO.loadRes(self.coco_gt, results) if results else COCO()
56
+
57
+ coco_eval = self.coco_eval[iou_type]
58
+
59
+ coco_eval.cocoDt = coco_dt
60
+ coco_eval.params.imgIds = list(img_ids)
61
+ coco_eval.params.useCats = self.useCats
62
+ img_ids, eval_imgs = evaluate(coco_eval)
63
+
64
+ self.eval_imgs[iou_type].append(eval_imgs)
65
+
66
+ def synchronize_between_processes(self):
67
+ for iou_type in self.iou_types:
68
+ self.eval_imgs[iou_type] = np.concatenate(self.eval_imgs[iou_type], 2)
69
+ create_common_coco_eval(self.coco_eval[iou_type], self.img_ids, self.eval_imgs[iou_type])
70
+
71
+ def accumulate(self):
72
+ for coco_eval in self.coco_eval.values():
73
+ coco_eval.accumulate()
74
+
75
+ def summarize(self):
76
+ for iou_type, coco_eval in self.coco_eval.items():
77
+ print("IoU metric: {}".format(iou_type))
78
+ coco_eval.summarize()
79
+
80
+ def prepare(self, predictions, iou_type):
81
+ if iou_type == "bbox":
82
+ return self.prepare_for_coco_detection(predictions)
83
+ elif iou_type == "segm":
84
+ return self.prepare_for_coco_segmentation(predictions)
85
+ elif iou_type == "keypoints":
86
+ return self.prepare_for_coco_keypoint(predictions)
87
+ else:
88
+ raise ValueError("Unknown iou type {}".format(iou_type))
89
+
90
+ def prepare_for_coco_detection(self, predictions):
91
+ coco_results = []
92
+ for original_id, prediction in predictions.items():
93
+ if len(prediction) == 0:
94
+ continue
95
+
96
+ boxes = prediction["boxes"]
97
+ boxes = convert_to_xywh(boxes).tolist()
98
+ scores = prediction["scores"].tolist()
99
+ labels = prediction["labels"].tolist()
100
+
101
+ coco_results.extend(
102
+ [
103
+ {
104
+ "image_id": original_id,
105
+ "category_id": labels[k],
106
+ "bbox": box,
107
+ "score": scores[k],
108
+ }
109
+ for k, box in enumerate(boxes)
110
+ ]
111
+ )
112
+ return coco_results
113
+
114
+ def prepare_for_coco_segmentation(self, predictions):
115
+ coco_results = []
116
+ for original_id, prediction in predictions.items():
117
+ if len(prediction) == 0:
118
+ continue
119
+
120
+ scores = prediction["scores"]
121
+ labels = prediction["labels"]
122
+ masks = prediction["masks"]
123
+
124
+ masks = masks > 0.5
125
+
126
+ scores = prediction["scores"].tolist()
127
+ labels = prediction["labels"].tolist()
128
+
129
+ rles = [
130
+ mask_util.encode(np.array(mask[0, :, :, np.newaxis], dtype=np.uint8, order="F"))[0]
131
+ for mask in masks
132
+ ]
133
+ for rle in rles:
134
+ rle["counts"] = rle["counts"].decode("utf-8")
135
+
136
+ coco_results.extend(
137
+ [
138
+ {
139
+ "image_id": original_id,
140
+ "category_id": labels[k],
141
+ "segmentation": rle,
142
+ "score": scores[k],
143
+ }
144
+ for k, rle in enumerate(rles)
145
+ ]
146
+ )
147
+ return coco_results
148
+
149
+ def prepare_for_coco_keypoint(self, predictions):
150
+ coco_results = []
151
+ for original_id, prediction in predictions.items():
152
+ if len(prediction) == 0:
153
+ continue
154
+
155
+ boxes = prediction["boxes"]
156
+ boxes = convert_to_xywh(boxes).tolist()
157
+ scores = prediction["scores"].tolist()
158
+ labels = prediction["labels"].tolist()
159
+ keypoints = prediction["keypoints"]
160
+ keypoints = keypoints.flatten(start_dim=1).tolist()
161
+
162
+ coco_results.extend(
163
+ [
164
+ {
165
+ "image_id": original_id,
166
+ "category_id": labels[k],
167
+ "keypoints": keypoint,
168
+ "score": scores[k],
169
+ }
170
+ for k, keypoint in enumerate(keypoints)
171
+ ]
172
+ )
173
+ return coco_results
174
+
175
+
176
+ def convert_to_xywh(boxes):
177
+ xmin, ymin, xmax, ymax = boxes.unbind(1)
178
+ return torch.stack((xmin, ymin, xmax - xmin, ymax - ymin), dim=1)
179
+
180
+
181
+ def merge(img_ids, eval_imgs):
182
+ all_img_ids = all_gather(img_ids)
183
+ all_eval_imgs = all_gather(eval_imgs)
184
+
185
+ merged_img_ids = []
186
+ for p in all_img_ids:
187
+ merged_img_ids.extend(p)
188
+
189
+ merged_eval_imgs = []
190
+ for p in all_eval_imgs:
191
+ merged_eval_imgs.append(p)
192
+
193
+ merged_img_ids = np.array(merged_img_ids)
194
+ merged_eval_imgs = np.concatenate(merged_eval_imgs, 2)
195
+
196
+ # keep only unique (and in sorted order) images
197
+ merged_img_ids, idx = np.unique(merged_img_ids, return_index=True)
198
+ merged_eval_imgs = merged_eval_imgs[..., idx]
199
+
200
+ return merged_img_ids, merged_eval_imgs
201
+
202
+
203
+ def create_common_coco_eval(coco_eval, img_ids, eval_imgs):
204
+ img_ids, eval_imgs = merge(img_ids, eval_imgs)
205
+ img_ids = list(img_ids)
206
+ eval_imgs = list(eval_imgs.flatten())
207
+
208
+ coco_eval.evalImgs = eval_imgs
209
+ coco_eval.params.imgIds = img_ids
210
+ coco_eval._paramsEval = copy.deepcopy(coco_eval.params)
211
+
212
+
213
+ #################################################################
214
+ # From pycocotools, just removed the prints and fixed
215
+ # a Python3 bug about unicode not defined
216
+ #################################################################
217
+
218
+
219
+ def evaluate(self):
220
+ """
221
+ Run per image evaluation on given images and store results (a list of dict) in self.evalImgs
222
+ :return: None
223
+ """
224
+ # tic = time.time()
225
+ # print('Running per image evaluation...')
226
+ p = self.params
227
+ # add backward compatibility if useSegm is specified in params
228
+ if p.useSegm is not None:
229
+ p.iouType = "segm" if p.useSegm == 1 else "bbox"
230
+ print("useSegm (deprecated) is not None. Running {} evaluation".format(p.iouType))
231
+ # print('Evaluate annotation type *{}*'.format(p.iouType))
232
+ p.imgIds = list(np.unique(p.imgIds))
233
+ if p.useCats:
234
+ p.catIds = list(np.unique(p.catIds))
235
+ p.maxDets = sorted(p.maxDets)
236
+ self.params = p
237
+
238
+ self._prepare()
239
+ # loop through images, area range, max detection number
240
+ catIds = p.catIds if p.useCats else [-1]
241
+
242
+ if p.iouType == "segm" or p.iouType == "bbox":
243
+ computeIoU = self.computeIoU
244
+ elif p.iouType == "keypoints":
245
+ computeIoU = self.computeOks
246
+ self.ious = {
247
+ (imgId, catId): computeIoU(imgId, catId)
248
+ for imgId in p.imgIds
249
+ for catId in catIds}
250
+
251
+ evaluateImg = self.evaluateImg
252
+ maxDet = p.maxDets[-1]
253
+ evalImgs = [
254
+ evaluateImg(imgId, catId, areaRng, maxDet)
255
+ for catId in catIds
256
+ for areaRng in p.areaRng
257
+ for imgId in p.imgIds
258
+ ]
259
+ # this is NOT in the pycocotools code, but could be done outside
260
+ evalImgs = np.asarray(evalImgs).reshape(len(catIds), len(p.areaRng), len(p.imgIds))
261
+ self._paramsEval = copy.deepcopy(self.params)
262
+ # toc = time.time()
263
+ # print('DONE (t={:0.2f}s).'.format(toc-tic))
264
+ return p.imgIds, evalImgs
265
+
266
+
267
+ #################################################################
268
+ # end of straight copy from pycocotools, just removing the prints
269
+ #################################################################
GroundingDINO/groundingdino/datasets/transforms.py ADDED
@@ -0,0 +1,311 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
2
+ """
3
+ Transforms and data augmentation for both image + bbox.
4
+ """
5
+ import os
6
+ import random
7
+
8
+ import PIL
9
+ import torch
10
+ import torchvision.transforms as T
11
+ import torchvision.transforms.functional as F
12
+
13
+ from groundingdino.util.box_ops import box_xyxy_to_cxcywh
14
+ from groundingdino.util.misc import interpolate
15
+
16
+
17
+ def crop(image, target, region):
18
+ cropped_image = F.crop(image, *region)
19
+
20
+ target = target.copy()
21
+ i, j, h, w = region
22
+
23
+ # should we do something wrt the original size?
24
+ target["size"] = torch.tensor([h, w])
25
+
26
+ fields = ["labels", "area", "iscrowd", "positive_map"]
27
+
28
+ if "boxes" in target:
29
+ boxes = target["boxes"]
30
+ max_size = torch.as_tensor([w, h], dtype=torch.float32)
31
+ cropped_boxes = boxes - torch.as_tensor([j, i, j, i])
32
+ cropped_boxes = torch.min(cropped_boxes.reshape(-1, 2, 2), max_size)
33
+ cropped_boxes = cropped_boxes.clamp(min=0)
34
+ area = (cropped_boxes[:, 1, :] - cropped_boxes[:, 0, :]).prod(dim=1)
35
+ target["boxes"] = cropped_boxes.reshape(-1, 4)
36
+ target["area"] = area
37
+ fields.append("boxes")
38
+
39
+ if "masks" in target:
40
+ # FIXME should we update the area here if there are no boxes?
41
+ target["masks"] = target["masks"][:, i : i + h, j : j + w]
42
+ fields.append("masks")
43
+
44
+ # remove elements for which the boxes or masks that have zero area
45
+ if "boxes" in target or "masks" in target:
46
+ # favor boxes selection when defining which elements to keep
47
+ # this is compatible with previous implementation
48
+ if "boxes" in target:
49
+ cropped_boxes = target["boxes"].reshape(-1, 2, 2)
50
+ keep = torch.all(cropped_boxes[:, 1, :] > cropped_boxes[:, 0, :], dim=1)
51
+ else:
52
+ keep = target["masks"].flatten(1).any(1)
53
+
54
+ for field in fields:
55
+ if field in target:
56
+ target[field] = target[field][keep]
57
+
58
+ if os.environ.get("IPDB_SHILONG_DEBUG", None) == "INFO":
59
+ # for debug and visualization only.
60
+ if "strings_positive" in target:
61
+ target["strings_positive"] = [
62
+ _i for _i, _j in zip(target["strings_positive"], keep) if _j
63
+ ]
64
+
65
+ return cropped_image, target
66
+
67
+
68
+ def hflip(image, target):
69
+ flipped_image = F.hflip(image)
70
+
71
+ w, h = image.size
72
+
73
+ target = target.copy()
74
+ if "boxes" in target:
75
+ boxes = target["boxes"]
76
+ boxes = boxes[:, [2, 1, 0, 3]] * torch.as_tensor([-1, 1, -1, 1]) + torch.as_tensor(
77
+ [w, 0, w, 0]
78
+ )
79
+ target["boxes"] = boxes
80
+
81
+ if "masks" in target:
82
+ target["masks"] = target["masks"].flip(-1)
83
+
84
+ return flipped_image, target
85
+
86
+
87
+ def resize(image, target, size, max_size=None):
88
+ # size can be min_size (scalar) or (w, h) tuple
89
+
90
+ def get_size_with_aspect_ratio(image_size, size, max_size=None):
91
+ w, h = image_size
92
+ if max_size is not None:
93
+ min_original_size = float(min((w, h)))
94
+ max_original_size = float(max((w, h)))
95
+ if max_original_size / min_original_size * size > max_size:
96
+ size = int(round(max_size * min_original_size / max_original_size))
97
+
98
+ if (w <= h and w == size) or (h <= w and h == size):
99
+ return (h, w)
100
+
101
+ if w < h:
102
+ ow = size
103
+ oh = int(size * h / w)
104
+ else:
105
+ oh = size
106
+ ow = int(size * w / h)
107
+
108
+ return (oh, ow)
109
+
110
+ def get_size(image_size, size, max_size=None):
111
+ if isinstance(size, (list, tuple)):
112
+ return size[::-1]
113
+ else:
114
+ return get_size_with_aspect_ratio(image_size, size, max_size)
115
+
116
+ size = get_size(image.size, size, max_size)
117
+ rescaled_image = F.resize(image, size)
118
+
119
+ if target is None:
120
+ return rescaled_image, None
121
+
122
+ ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(rescaled_image.size, image.size))
123
+ ratio_width, ratio_height = ratios
124
+
125
+ target = target.copy()
126
+ if "boxes" in target:
127
+ boxes = target["boxes"]
128
+ scaled_boxes = boxes * torch.as_tensor(
129
+ [ratio_width, ratio_height, ratio_width, ratio_height]
130
+ )
131
+ target["boxes"] = scaled_boxes
132
+
133
+ if "area" in target:
134
+ area = target["area"]
135
+ scaled_area = area * (ratio_width * ratio_height)
136
+ target["area"] = scaled_area
137
+
138
+ h, w = size
139
+ target["size"] = torch.tensor([h, w])
140
+
141
+ if "masks" in target:
142
+ target["masks"] = (
143
+ interpolate(target["masks"][:, None].float(), size, mode="nearest")[:, 0] > 0.5
144
+ )
145
+
146
+ return rescaled_image, target
147
+
148
+
149
+ def pad(image, target, padding):
150
+ # assumes that we only pad on the bottom right corners
151
+ padded_image = F.pad(image, (0, 0, padding[0], padding[1]))
152
+ if target is None:
153
+ return padded_image, None
154
+ target = target.copy()
155
+ # should we do something wrt the original size?
156
+ target["size"] = torch.tensor(padded_image.size[::-1])
157
+ if "masks" in target:
158
+ target["masks"] = torch.nn.functional.pad(target["masks"], (0, padding[0], 0, padding[1]))
159
+ return padded_image, target
160
+
161
+
162
+ class ResizeDebug(object):
163
+ def __init__(self, size):
164
+ self.size = size
165
+
166
+ def __call__(self, img, target):
167
+ return resize(img, target, self.size)
168
+
169
+
170
+ class RandomCrop(object):
171
+ def __init__(self, size):
172
+ self.size = size
173
+
174
+ def __call__(self, img, target):
175
+ region = T.RandomCrop.get_params(img, self.size)
176
+ return crop(img, target, region)
177
+
178
+
179
+ class RandomSizeCrop(object):
180
+ def __init__(self, min_size: int, max_size: int, respect_boxes: bool = False):
181
+ # respect_boxes: True to keep all boxes
182
+ # False to tolerence box filter
183
+ self.min_size = min_size
184
+ self.max_size = max_size
185
+ self.respect_boxes = respect_boxes
186
+
187
+ def __call__(self, img: PIL.Image.Image, target: dict):
188
+ init_boxes = len(target["boxes"])
189
+ max_patience = 10
190
+ for i in range(max_patience):
191
+ w = random.randint(self.min_size, min(img.width, self.max_size))
192
+ h = random.randint(self.min_size, min(img.height, self.max_size))
193
+ region = T.RandomCrop.get_params(img, [h, w])
194
+ result_img, result_target = crop(img, target, region)
195
+ if (
196
+ not self.respect_boxes
197
+ or len(result_target["boxes"]) == init_boxes
198
+ or i == max_patience - 1
199
+ ):
200
+ return result_img, result_target
201
+ return result_img, result_target
202
+
203
+
204
+ class CenterCrop(object):
205
+ def __init__(self, size):
206
+ self.size = size
207
+
208
+ def __call__(self, img, target):
209
+ image_width, image_height = img.size
210
+ crop_height, crop_width = self.size
211
+ crop_top = int(round((image_height - crop_height) / 2.0))
212
+ crop_left = int(round((image_width - crop_width) / 2.0))
213
+ return crop(img, target, (crop_top, crop_left, crop_height, crop_width))
214
+
215
+
216
+ class RandomHorizontalFlip(object):
217
+ def __init__(self, p=0.5):
218
+ self.p = p
219
+
220
+ def __call__(self, img, target):
221
+ if random.random() < self.p:
222
+ return hflip(img, target)
223
+ return img, target
224
+
225
+
226
+ class RandomResize(object):
227
+ def __init__(self, sizes, max_size=None):
228
+ assert isinstance(sizes, (list, tuple))
229
+ self.sizes = sizes
230
+ self.max_size = max_size
231
+
232
+ def __call__(self, img, target=None):
233
+ size = random.choice(self.sizes)
234
+ return resize(img, target, size, self.max_size)
235
+
236
+
237
+ class RandomPad(object):
238
+ def __init__(self, max_pad):
239
+ self.max_pad = max_pad
240
+
241
+ def __call__(self, img, target):
242
+ pad_x = random.randint(0, self.max_pad)
243
+ pad_y = random.randint(0, self.max_pad)
244
+ return pad(img, target, (pad_x, pad_y))
245
+
246
+
247
+ class RandomSelect(object):
248
+ """
249
+ Randomly selects between transforms1 and transforms2,
250
+ with probability p for transforms1 and (1 - p) for transforms2
251
+ """
252
+
253
+ def __init__(self, transforms1, transforms2, p=0.5):
254
+ self.transforms1 = transforms1
255
+ self.transforms2 = transforms2
256
+ self.p = p
257
+
258
+ def __call__(self, img, target):
259
+ if random.random() < self.p:
260
+ return self.transforms1(img, target)
261
+ return self.transforms2(img, target)
262
+
263
+
264
+ class ToTensor(object):
265
+ def __call__(self, img, target):
266
+ return F.to_tensor(img), target
267
+
268
+
269
+ class RandomErasing(object):
270
+ def __init__(self, *args, **kwargs):
271
+ self.eraser = T.RandomErasing(*args, **kwargs)
272
+
273
+ def __call__(self, img, target):
274
+ return self.eraser(img), target
275
+
276
+
277
+ class Normalize(object):
278
+ def __init__(self, mean, std):
279
+ self.mean = mean
280
+ self.std = std
281
+
282
+ def __call__(self, image, target=None):
283
+ image = F.normalize(image, mean=self.mean, std=self.std)
284
+ if target is None:
285
+ return image, None
286
+ target = target.copy()
287
+ h, w = image.shape[-2:]
288
+ if "boxes" in target:
289
+ boxes = target["boxes"]
290
+ boxes = box_xyxy_to_cxcywh(boxes)
291
+ boxes = boxes / torch.tensor([w, h, w, h], dtype=torch.float32)
292
+ target["boxes"] = boxes
293
+ return image, target
294
+
295
+
296
+ class Compose(object):
297
+ def __init__(self, transforms):
298
+ self.transforms = transforms
299
+
300
+ def __call__(self, image, target):
301
+ for t in self.transforms:
302
+ image, target = t(image, target)
303
+ return image, target
304
+
305
+ def __repr__(self):
306
+ format_string = self.__class__.__name__ + "("
307
+ for t in self.transforms:
308
+ format_string += "\n"
309
+ format_string += " {0}".format(t)
310
+ format_string += "\n)"
311
+ return format_string
GroundingDINO/groundingdino/models/GroundingDINO/__init__.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ------------------------------------------------------------------------
2
+ # Grounding DINO
3
+ # url: https://github.com/IDEA-Research/GroundingDINO
4
+ # Copyright (c) 2023 IDEA. All Rights Reserved.
5
+ # Licensed under the Apache License, Version 2.0 [see LICENSE for details]
6
+ # ------------------------------------------------------------------------
7
+ # Conditional DETR
8
+ # Copyright (c) 2021 Microsoft. All Rights Reserved.
9
+ # Licensed under the Apache License, Version 2.0 [see LICENSE for details]
10
+ # ------------------------------------------------------------------------
11
+ # Copied from DETR (https://github.com/facebookresearch/detr)
12
+ # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
13
+ # ------------------------------------------------------------------------
14
+
15
+ from .groundingdino import build_groundingdino
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (245 Bytes). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/bertwarper.cpython-39.pyc ADDED
Binary file (7.19 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/fuse_modules.cpython-39.pyc ADDED
Binary file (7.78 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/groundingdino.cpython-39.pyc ADDED
Binary file (11.2 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/ms_deform_attn.cpython-39.pyc ADDED
Binary file (11.8 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/transformer.cpython-39.pyc ADDED
Binary file (18.8 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/transformer_vanilla.cpython-39.pyc ADDED
Binary file (3.42 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/__pycache__/utils.cpython-39.pyc ADDED
Binary file (9.52 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/backbone/__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .backbone import build_backbone
GroundingDINO/groundingdino/models/GroundingDINO/backbone/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (244 Bytes). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/backbone/__pycache__/backbone.cpython-39.pyc ADDED
Binary file (6.22 kB). View file
 
GroundingDINO/groundingdino/models/GroundingDINO/backbone/__pycache__/position_encoding.cpython-39.pyc ADDED
Binary file (5.14 kB). View file