imgraft 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
imgraft-0.1.0/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 [yyyy] [name of copyright owner]
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.
imgraft-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,253 @@
1
+ Metadata-Version: 2.4
2
+ Name: imgraft
3
+ Version: 0.1.0
4
+ Summary: Semantic image dataset curation using CLIP + HDBSCAN
5
+ License: MIT
6
+ Project-URL: Homepage, https://github.com/dasarinikhil/imgraft
7
+ Project-URL: Issues, https://github.com/dasarinikhil/imgraft/issues
8
+ Keywords: clip,hdbscan,dataset-curation,computer-vision,image-deduplication,semantic-clustering,umap
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
12
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
13
+ Requires-Python: >=3.9
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: torch>=2.0.0
17
+ Requires-Dist: open-clip-torch>=2.20.0
18
+ Requires-Dist: hdbscan>=0.8.33
19
+ Requires-Dist: umap-learn>=0.5.3
20
+ Requires-Dist: numpy>=1.24.0
21
+ Requires-Dist: Pillow>=10.0.0
22
+ Requires-Dist: tqdm>=4.66.0
23
+ Requires-Dist: rich>=13.7.0
24
+ Requires-Dist: typer>=0.12.0
25
+ Requires-Dist: plotly>=5.18.0
26
+ Requires-Dist: scikit-learn>=1.3.0
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=8.0.0; extra == "dev"
29
+ Requires-Dist: pytest-cov>=5.0.0; extra == "dev"
30
+ Provides-Extra: vis
31
+ Requires-Dist: matplotlib>=3.8.0; extra == "vis"
32
+ Requires-Dist: kaleido>=0.2.1; extra == "vis"
33
+ Dynamic: license-file
34
+
35
+ # imgraft
36
+
37
+ **Semantic image dataset curation via CLIP + HDBSCAN.**
38
+
39
+ [![PyPI version](https://badge.fury.io/py/imgraft.svg)](https://pypi.org/project/imgraft/)
40
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
41
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/)
42
+
43
+ imgraft helps you clean, balance, and curate any messy image dataset — no labels, no manual annotation needed. Give it a folder of images and it returns a semantically balanced, deduplicated subset with full visualizations.
44
+
45
+ ---
46
+
47
+ ## How it works
48
+
49
+ ![imgraft pipeline](docs/pipeline.png)
50
+
51
+ Five stages — all automatic:
52
+
53
+ 1. **Embed** — every image gets a CLIP vector (ViT-B/32 or ViT-L/14)
54
+ 2. **Cluster** — HDBSCAN finds semantic groups, UMAP pre-reduces for speed
55
+ 3. **Curate** — pick a strategy: centroid, diversity, or text-query filter
56
+ 4. **Inspect** — contact sheet PNGs per cluster for visual verification
57
+ 5. **Export** — structured `core/` + `diverse/` + `noise/` folders, ready for training
58
+
59
+ ---
60
+
61
+ ## Why imgraft?
62
+
63
+ Raw image datasets are messy. Web scrapes contain near-duplicates. Crawled sets are class-imbalanced. Annotators mislabel. Training on noisy data silently destroys model performance.
64
+
65
+ imgraft solves this using **semantic similarity** — it embeds every image with CLIP, discovers visual groups with HDBSCAN, and selects the best representatives per cluster. No domain assumptions. Works on any image type.
66
+
67
+ > **Real-world impact**: On a production OCR project, an automated CLIP + HDBSCAN curation pipeline recovered model accuracy from 2% (after discovering 100K+ mislabeled images) to 90% on first retrain, reaching 95% through iteration.
68
+
69
+ ---
70
+
71
+ ## Install
72
+
73
+ ```bash
74
+ pip install imgraft
75
+ ```
76
+
77
+ For PNG visualization support:
78
+ ```bash
79
+ pip install imgraft[vis]
80
+ ```
81
+
82
+ ---
83
+
84
+ ## Quick start
85
+
86
+ ### Python API
87
+
88
+ ```python
89
+ from imgraft import Curator
90
+
91
+ curator = Curator(model="ViT-B/32") # or "ViT-L/14" for higher accuracy
92
+
93
+ result = curator.run(
94
+ image_dir="./raw_images/",
95
+ keep_ratio=0.25, # keep 25% of dataset
96
+ strategy="diversity", # centroid | diversity | text-query | drop-noise
97
+ drop_noise=True, # remove OOD/noise images
98
+ )
99
+
100
+ # ── verify clusters visually ───────────────────────────────────────────────────
101
+ result.inspect(
102
+ output_dir="./cluster_grids/", # one PNG contact sheet per cluster
103
+ n_per_side=5, # 5 centroid + 5 random thumbnails side by side
104
+ )
105
+ # open cluster_000.png, cluster_001.png etc. — visually verify before committing
106
+
107
+ # ── export structured dataset ──────────────────────────────────────────────────
108
+ result.export_clusters(
109
+ output_dir="./dataset/",
110
+ n_core=50, # N most representative images per cluster
111
+ n_diverse=50, # N most diverse images per cluster
112
+ )
113
+
114
+ # ── interactive UMAP explorer ──────────────────────────────────────────────────
115
+ result.plot("clusters.html") # hover any point to see the image
116
+
117
+ print(result.stats())
118
+ # {'total': 8211, 'kept': 1642, 'clusters': 47, 'noise': 312, ...}
119
+ ```
120
+
121
+ ### CLI
122
+
123
+ ```bash
124
+ # basic — keep 25% using diversity sampling
125
+ imgraft run ./images/ --keep 0.25 --out ./curated/
126
+
127
+ # with interactive visualization
128
+ imgraft run ./images/ --keep 0.25 --visualize --out ./curated/
129
+
130
+ # filter by text query (zero-shot, no labels needed)
131
+ imgraft run ./images/ \
132
+ --strategy text-query \
133
+ --query "a clear front-facing product photo on white background" \
134
+ --out ./curated/
135
+
136
+ # drop noise/OOD images only, keep everything else
137
+ imgraft run ./images/ --strategy drop-noise --out ./cleaned/
138
+
139
+ # inspect dataset structure before curating
140
+ imgraft info ./images/
141
+ ```
142
+
143
+ ---
144
+
145
+ ## Cluster inspection
146
+
147
+ Before exporting your dataset, visually verify what each cluster contains:
148
+
149
+ ```python
150
+ result.inspect(output_dir="./cluster_grids/", n_per_side=5)
151
+ ```
152
+
153
+ Each PNG contact sheet shows **two sides**:
154
+
155
+ | Left | Right |
156
+ |---|---|
157
+ | Core — closest to cluster centroid | Random sample from the cluster |
158
+
159
+ Lets you spot bad clusters at a glance — if `cluster_003` is all blurry images or mislabeled examples, you know to drop it before training.
160
+
161
+ ---
162
+
163
+ ## Structured export
164
+
165
+ ```python
166
+ result.export_clusters(
167
+ output_dir="./dataset/",
168
+ n_core=50,
169
+ n_diverse=50,
170
+ )
171
+ ```
172
+
173
+ Output layout:
174
+
175
+ ```
176
+ dataset/
177
+ cluster_000/
178
+ core/ ← centroid-closest (most representative)
179
+ diverse/ ← furthest-point sampled (max variety)
180
+ cluster_001/
181
+ core/
182
+ diverse/
183
+ ...
184
+ noise/ ← all HDBSCAN outliers, isolated
185
+ export_summary.json
186
+ ```
187
+
188
+ `core/` is best for training classifiers. `diverse/` maximises variety and removes near-duplicates. `noise/` gives a clean view of OOD images to review or discard.
189
+
190
+ ---
191
+
192
+ ## Curation strategies
193
+
194
+ | Strategy | Best for | How it works |
195
+ |---|---|---|
196
+ | `diversity` | Removing near-duplicates, max variety | Greedy furthest-point sampling per cluster |
197
+ | `centroid` | Balanced, representative subsets | Keeps images closest to each cluster center |
198
+ | `text-query` | Domain-specific filtering, no labels | CLIP zero-shot similarity to a text prompt |
199
+ | `drop-noise` | Quick clean without size reduction | Removes HDBSCAN outliers only |
200
+
201
+ ---
202
+
203
+ ## Works on any domain
204
+
205
+ - **Web-scraped datasets** — deduplicate crawled images, remove OOD noise
206
+ - **Medical imaging** — balance X-ray / pathology / dermoscopy class distributions
207
+ - **Satellite / aerial** — curate geospatial image sets by region and content type
208
+ - **E-commerce / products** — deduplicate product catalogs by visual similarity
209
+ - **Industrial / manufacturing** — balance defect vs. normal in inspection datasets
210
+ - **Document / form images** — group by layout type, sample representative subset
211
+ - **General ML training prep** — quality control before sending to annotation
212
+
213
+ ---
214
+
215
+ ## Backbone options
216
+
217
+ | Model | Embedding dim | Speed | Quality |
218
+ |---|---|---|---|
219
+ | `ViT-B/32` | 512 | ⚡ Fast | Good |
220
+ | `ViT-B/16` | 512 | Medium | Better |
221
+ | `ViT-L/14` | 768 | Slower | High |
222
+ | `ViT-H/14` | 1024 | Slow | Highest |
223
+
224
+ Switch with `--model ViT-L/14` or `Curator(model="ViT-L/14")`.
225
+
226
+ ---
227
+
228
+ ## Embedding cache
229
+
230
+ Re-embedding large folders is slow. Cache embeddings to disk so reruns skip it:
231
+
232
+ ```bash
233
+ imgraft run ./images/ --cache ./.imgraft_cache/ --keep 0.25
234
+ ```
235
+
236
+ ---
237
+
238
+ ## Visualizations
239
+
240
+ **Interactive HTML** (`result.plot("clusters.html")`):
241
+ - UMAP scatter colored by cluster
242
+ - Hover any point → see the image thumbnail
243
+ - Kept images at full opacity, dropped faded
244
+
245
+ **Cluster grids** (`result.inspect("./grids/")`):
246
+ - One PNG per cluster — centroid sample vs random sample side by side
247
+ - Visual verification before committing to training
248
+
249
+ ---
250
+
251
+ ## License
252
+
253
+ MIT
@@ -0,0 +1,219 @@
1
+ # imgraft
2
+
3
+ **Semantic image dataset curation via CLIP + HDBSCAN.**
4
+
5
+ [![PyPI version](https://badge.fury.io/py/imgraft.svg)](https://pypi.org/project/imgraft/)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
7
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/)
8
+
9
+ imgraft helps you clean, balance, and curate any messy image dataset — no labels, no manual annotation needed. Give it a folder of images and it returns a semantically balanced, deduplicated subset with full visualizations.
10
+
11
+ ---
12
+
13
+ ## How it works
14
+
15
+ ![imgraft pipeline](docs/pipeline.png)
16
+
17
+ Five stages — all automatic:
18
+
19
+ 1. **Embed** — every image gets a CLIP vector (ViT-B/32 or ViT-L/14)
20
+ 2. **Cluster** — HDBSCAN finds semantic groups, UMAP pre-reduces for speed
21
+ 3. **Curate** — pick a strategy: centroid, diversity, or text-query filter
22
+ 4. **Inspect** — contact sheet PNGs per cluster for visual verification
23
+ 5. **Export** — structured `core/` + `diverse/` + `noise/` folders, ready for training
24
+
25
+ ---
26
+
27
+ ## Why imgraft?
28
+
29
+ Raw image datasets are messy. Web scrapes contain near-duplicates. Crawled sets are class-imbalanced. Annotators mislabel. Training on noisy data silently destroys model performance.
30
+
31
+ imgraft solves this using **semantic similarity** — it embeds every image with CLIP, discovers visual groups with HDBSCAN, and selects the best representatives per cluster. No domain assumptions. Works on any image type.
32
+
33
+ > **Real-world impact**: On a production OCR project, an automated CLIP + HDBSCAN curation pipeline recovered model accuracy from 2% (after discovering 100K+ mislabeled images) to 90% on first retrain, reaching 95% through iteration.
34
+
35
+ ---
36
+
37
+ ## Install
38
+
39
+ ```bash
40
+ pip install imgraft
41
+ ```
42
+
43
+ For PNG visualization support:
44
+ ```bash
45
+ pip install imgraft[vis]
46
+ ```
47
+
48
+ ---
49
+
50
+ ## Quick start
51
+
52
+ ### Python API
53
+
54
+ ```python
55
+ from imgraft import Curator
56
+
57
+ curator = Curator(model="ViT-B/32") # or "ViT-L/14" for higher accuracy
58
+
59
+ result = curator.run(
60
+ image_dir="./raw_images/",
61
+ keep_ratio=0.25, # keep 25% of dataset
62
+ strategy="diversity", # centroid | diversity | text-query | drop-noise
63
+ drop_noise=True, # remove OOD/noise images
64
+ )
65
+
66
+ # ── verify clusters visually ───────────────────────────────────────────────────
67
+ result.inspect(
68
+ output_dir="./cluster_grids/", # one PNG contact sheet per cluster
69
+ n_per_side=5, # 5 centroid + 5 random thumbnails side by side
70
+ )
71
+ # open cluster_000.png, cluster_001.png etc. — visually verify before committing
72
+
73
+ # ── export structured dataset ──────────────────────────────────────────────────
74
+ result.export_clusters(
75
+ output_dir="./dataset/",
76
+ n_core=50, # N most representative images per cluster
77
+ n_diverse=50, # N most diverse images per cluster
78
+ )
79
+
80
+ # ── interactive UMAP explorer ──────────────────────────────────────────────────
81
+ result.plot("clusters.html") # hover any point to see the image
82
+
83
+ print(result.stats())
84
+ # {'total': 8211, 'kept': 1642, 'clusters': 47, 'noise': 312, ...}
85
+ ```
86
+
87
+ ### CLI
88
+
89
+ ```bash
90
+ # basic — keep 25% using diversity sampling
91
+ imgraft run ./images/ --keep 0.25 --out ./curated/
92
+
93
+ # with interactive visualization
94
+ imgraft run ./images/ --keep 0.25 --visualize --out ./curated/
95
+
96
+ # filter by text query (zero-shot, no labels needed)
97
+ imgraft run ./images/ \
98
+ --strategy text-query \
99
+ --query "a clear front-facing product photo on white background" \
100
+ --out ./curated/
101
+
102
+ # drop noise/OOD images only, keep everything else
103
+ imgraft run ./images/ --strategy drop-noise --out ./cleaned/
104
+
105
+ # inspect dataset structure before curating
106
+ imgraft info ./images/
107
+ ```
108
+
109
+ ---
110
+
111
+ ## Cluster inspection
112
+
113
+ Before exporting your dataset, visually verify what each cluster contains:
114
+
115
+ ```python
116
+ result.inspect(output_dir="./cluster_grids/", n_per_side=5)
117
+ ```
118
+
119
+ Each PNG contact sheet shows **two sides**:
120
+
121
+ | Left | Right |
122
+ |---|---|
123
+ | Core — closest to cluster centroid | Random sample from the cluster |
124
+
125
+ Lets you spot bad clusters at a glance — if `cluster_003` is all blurry images or mislabeled examples, you know to drop it before training.
126
+
127
+ ---
128
+
129
+ ## Structured export
130
+
131
+ ```python
132
+ result.export_clusters(
133
+ output_dir="./dataset/",
134
+ n_core=50,
135
+ n_diverse=50,
136
+ )
137
+ ```
138
+
139
+ Output layout:
140
+
141
+ ```
142
+ dataset/
143
+ cluster_000/
144
+ core/ ← centroid-closest (most representative)
145
+ diverse/ ← furthest-point sampled (max variety)
146
+ cluster_001/
147
+ core/
148
+ diverse/
149
+ ...
150
+ noise/ ← all HDBSCAN outliers, isolated
151
+ export_summary.json
152
+ ```
153
+
154
+ `core/` is best for training classifiers. `diverse/` maximises variety and removes near-duplicates. `noise/` gives a clean view of OOD images to review or discard.
155
+
156
+ ---
157
+
158
+ ## Curation strategies
159
+
160
+ | Strategy | Best for | How it works |
161
+ |---|---|---|
162
+ | `diversity` | Removing near-duplicates, max variety | Greedy furthest-point sampling per cluster |
163
+ | `centroid` | Balanced, representative subsets | Keeps images closest to each cluster center |
164
+ | `text-query` | Domain-specific filtering, no labels | CLIP zero-shot similarity to a text prompt |
165
+ | `drop-noise` | Quick clean without size reduction | Removes HDBSCAN outliers only |
166
+
167
+ ---
168
+
169
+ ## Works on any domain
170
+
171
+ - **Web-scraped datasets** — deduplicate crawled images, remove OOD noise
172
+ - **Medical imaging** — balance X-ray / pathology / dermoscopy class distributions
173
+ - **Satellite / aerial** — curate geospatial image sets by region and content type
174
+ - **E-commerce / products** — deduplicate product catalogs by visual similarity
175
+ - **Industrial / manufacturing** — balance defect vs. normal in inspection datasets
176
+ - **Document / form images** — group by layout type, sample representative subset
177
+ - **General ML training prep** — quality control before sending to annotation
178
+
179
+ ---
180
+
181
+ ## Backbone options
182
+
183
+ | Model | Embedding dim | Speed | Quality |
184
+ |---|---|---|---|
185
+ | `ViT-B/32` | 512 | ⚡ Fast | Good |
186
+ | `ViT-B/16` | 512 | Medium | Better |
187
+ | `ViT-L/14` | 768 | Slower | High |
188
+ | `ViT-H/14` | 1024 | Slow | Highest |
189
+
190
+ Switch with `--model ViT-L/14` or `Curator(model="ViT-L/14")`.
191
+
192
+ ---
193
+
194
+ ## Embedding cache
195
+
196
+ Re-embedding large folders is slow. Cache embeddings to disk so reruns skip it:
197
+
198
+ ```bash
199
+ imgraft run ./images/ --cache ./.imgraft_cache/ --keep 0.25
200
+ ```
201
+
202
+ ---
203
+
204
+ ## Visualizations
205
+
206
+ **Interactive HTML** (`result.plot("clusters.html")`):
207
+ - UMAP scatter colored by cluster
208
+ - Hover any point → see the image thumbnail
209
+ - Kept images at full opacity, dropped faded
210
+
211
+ **Cluster grids** (`result.inspect("./grids/")`):
212
+ - One PNG per cluster — centroid sample vs random sample side by side
213
+ - Visual verification before committing to training
214
+
215
+ ---
216
+
217
+ ## License
218
+
219
+ MIT
@@ -0,0 +1,253 @@
1
+ Metadata-Version: 2.4
2
+ Name: imgraft
3
+ Version: 0.1.0
4
+ Summary: Semantic image dataset curation using CLIP + HDBSCAN
5
+ License: MIT
6
+ Project-URL: Homepage, https://github.com/dasarinikhil/imgraft
7
+ Project-URL: Issues, https://github.com/dasarinikhil/imgraft/issues
8
+ Keywords: clip,hdbscan,dataset-curation,computer-vision,image-deduplication,semantic-clustering,umap
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
12
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
13
+ Requires-Python: >=3.9
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: torch>=2.0.0
17
+ Requires-Dist: open-clip-torch>=2.20.0
18
+ Requires-Dist: hdbscan>=0.8.33
19
+ Requires-Dist: umap-learn>=0.5.3
20
+ Requires-Dist: numpy>=1.24.0
21
+ Requires-Dist: Pillow>=10.0.0
22
+ Requires-Dist: tqdm>=4.66.0
23
+ Requires-Dist: rich>=13.7.0
24
+ Requires-Dist: typer>=0.12.0
25
+ Requires-Dist: plotly>=5.18.0
26
+ Requires-Dist: scikit-learn>=1.3.0
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=8.0.0; extra == "dev"
29
+ Requires-Dist: pytest-cov>=5.0.0; extra == "dev"
30
+ Provides-Extra: vis
31
+ Requires-Dist: matplotlib>=3.8.0; extra == "vis"
32
+ Requires-Dist: kaleido>=0.2.1; extra == "vis"
33
+ Dynamic: license-file
34
+
35
+ # imgraft
36
+
37
+ **Semantic image dataset curation via CLIP + HDBSCAN.**
38
+
39
+ [![PyPI version](https://badge.fury.io/py/imgraft.svg)](https://pypi.org/project/imgraft/)
40
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
41
+ [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/)
42
+
43
+ imgraft helps you clean, balance, and curate any messy image dataset — no labels, no manual annotation needed. Give it a folder of images and it returns a semantically balanced, deduplicated subset with full visualizations.
44
+
45
+ ---
46
+
47
+ ## How it works
48
+
49
+ ![imgraft pipeline](docs/pipeline.png)
50
+
51
+ Five stages — all automatic:
52
+
53
+ 1. **Embed** — every image gets a CLIP vector (ViT-B/32 or ViT-L/14)
54
+ 2. **Cluster** — HDBSCAN finds semantic groups, UMAP pre-reduces for speed
55
+ 3. **Curate** — pick a strategy: centroid, diversity, or text-query filter
56
+ 4. **Inspect** — contact sheet PNGs per cluster for visual verification
57
+ 5. **Export** — structured `core/` + `diverse/` + `noise/` folders, ready for training
58
+
59
+ ---
60
+
61
+ ## Why imgraft?
62
+
63
+ Raw image datasets are messy. Web scrapes contain near-duplicates. Crawled sets are class-imbalanced. Annotators mislabel. Training on noisy data silently destroys model performance.
64
+
65
+ imgraft solves this using **semantic similarity** — it embeds every image with CLIP, discovers visual groups with HDBSCAN, and selects the best representatives per cluster. No domain assumptions. Works on any image type.
66
+
67
+ > **Real-world impact**: On a production OCR project, an automated CLIP + HDBSCAN curation pipeline recovered model accuracy from 2% (after discovering 100K+ mislabeled images) to 90% on first retrain, reaching 95% through iteration.
68
+
69
+ ---
70
+
71
+ ## Install
72
+
73
+ ```bash
74
+ pip install imgraft
75
+ ```
76
+
77
+ For PNG visualization support:
78
+ ```bash
79
+ pip install imgraft[vis]
80
+ ```
81
+
82
+ ---
83
+
84
+ ## Quick start
85
+
86
+ ### Python API
87
+
88
+ ```python
89
+ from imgraft import Curator
90
+
91
+ curator = Curator(model="ViT-B/32") # or "ViT-L/14" for higher accuracy
92
+
93
+ result = curator.run(
94
+ image_dir="./raw_images/",
95
+ keep_ratio=0.25, # keep 25% of dataset
96
+ strategy="diversity", # centroid | diversity | text-query | drop-noise
97
+ drop_noise=True, # remove OOD/noise images
98
+ )
99
+
100
+ # ── verify clusters visually ───────────────────────────────────────────────────
101
+ result.inspect(
102
+ output_dir="./cluster_grids/", # one PNG contact sheet per cluster
103
+ n_per_side=5, # 5 centroid + 5 random thumbnails side by side
104
+ )
105
+ # open cluster_000.png, cluster_001.png etc. — visually verify before committing
106
+
107
+ # ── export structured dataset ──────────────────────────────────────────────────
108
+ result.export_clusters(
109
+ output_dir="./dataset/",
110
+ n_core=50, # N most representative images per cluster
111
+ n_diverse=50, # N most diverse images per cluster
112
+ )
113
+
114
+ # ── interactive UMAP explorer ──────────────────────────────────────────────────
115
+ result.plot("clusters.html") # hover any point to see the image
116
+
117
+ print(result.stats())
118
+ # {'total': 8211, 'kept': 1642, 'clusters': 47, 'noise': 312, ...}
119
+ ```
120
+
121
+ ### CLI
122
+
123
+ ```bash
124
+ # basic — keep 25% using diversity sampling
125
+ imgraft run ./images/ --keep 0.25 --out ./curated/
126
+
127
+ # with interactive visualization
128
+ imgraft run ./images/ --keep 0.25 --visualize --out ./curated/
129
+
130
+ # filter by text query (zero-shot, no labels needed)
131
+ imgraft run ./images/ \
132
+ --strategy text-query \
133
+ --query "a clear front-facing product photo on white background" \
134
+ --out ./curated/
135
+
136
+ # drop noise/OOD images only, keep everything else
137
+ imgraft run ./images/ --strategy drop-noise --out ./cleaned/
138
+
139
+ # inspect dataset structure before curating
140
+ imgraft info ./images/
141
+ ```
142
+
143
+ ---
144
+
145
+ ## Cluster inspection
146
+
147
+ Before exporting your dataset, visually verify what each cluster contains:
148
+
149
+ ```python
150
+ result.inspect(output_dir="./cluster_grids/", n_per_side=5)
151
+ ```
152
+
153
+ Each PNG contact sheet shows **two sides**:
154
+
155
+ | Left | Right |
156
+ |---|---|
157
+ | Core — closest to cluster centroid | Random sample from the cluster |
158
+
159
+ Lets you spot bad clusters at a glance — if `cluster_003` is all blurry images or mislabeled examples, you know to drop it before training.
160
+
161
+ ---
162
+
163
+ ## Structured export
164
+
165
+ ```python
166
+ result.export_clusters(
167
+ output_dir="./dataset/",
168
+ n_core=50,
169
+ n_diverse=50,
170
+ )
171
+ ```
172
+
173
+ Output layout:
174
+
175
+ ```
176
+ dataset/
177
+ cluster_000/
178
+ core/ ← centroid-closest (most representative)
179
+ diverse/ ← furthest-point sampled (max variety)
180
+ cluster_001/
181
+ core/
182
+ diverse/
183
+ ...
184
+ noise/ ← all HDBSCAN outliers, isolated
185
+ export_summary.json
186
+ ```
187
+
188
+ `core/` is best for training classifiers. `diverse/` maximises variety and removes near-duplicates. `noise/` gives a clean view of OOD images to review or discard.
189
+
190
+ ---
191
+
192
+ ## Curation strategies
193
+
194
+ | Strategy | Best for | How it works |
195
+ |---|---|---|
196
+ | `diversity` | Removing near-duplicates, max variety | Greedy furthest-point sampling per cluster |
197
+ | `centroid` | Balanced, representative subsets | Keeps images closest to each cluster center |
198
+ | `text-query` | Domain-specific filtering, no labels | CLIP zero-shot similarity to a text prompt |
199
+ | `drop-noise` | Quick clean without size reduction | Removes HDBSCAN outliers only |
200
+
201
+ ---
202
+
203
+ ## Works on any domain
204
+
205
+ - **Web-scraped datasets** — deduplicate crawled images, remove OOD noise
206
+ - **Medical imaging** — balance X-ray / pathology / dermoscopy class distributions
207
+ - **Satellite / aerial** — curate geospatial image sets by region and content type
208
+ - **E-commerce / products** — deduplicate product catalogs by visual similarity
209
+ - **Industrial / manufacturing** — balance defect vs. normal in inspection datasets
210
+ - **Document / form images** — group by layout type, sample representative subset
211
+ - **General ML training prep** — quality control before sending to annotation
212
+
213
+ ---
214
+
215
+ ## Backbone options
216
+
217
+ | Model | Embedding dim | Speed | Quality |
218
+ |---|---|---|---|
219
+ | `ViT-B/32` | 512 | ⚡ Fast | Good |
220
+ | `ViT-B/16` | 512 | Medium | Better |
221
+ | `ViT-L/14` | 768 | Slower | High |
222
+ | `ViT-H/14` | 1024 | Slow | Highest |
223
+
224
+ Switch with `--model ViT-L/14` or `Curator(model="ViT-L/14")`.
225
+
226
+ ---
227
+
228
+ ## Embedding cache
229
+
230
+ Re-embedding large folders is slow. Cache embeddings to disk so reruns skip it:
231
+
232
+ ```bash
233
+ imgraft run ./images/ --cache ./.imgraft_cache/ --keep 0.25
234
+ ```
235
+
236
+ ---
237
+
238
+ ## Visualizations
239
+
240
+ **Interactive HTML** (`result.plot("clusters.html")`):
241
+ - UMAP scatter colored by cluster
242
+ - Hover any point → see the image thumbnail
243
+ - Kept images at full opacity, dropped faded
244
+
245
+ **Cluster grids** (`result.inspect("./grids/")`):
246
+ - One PNG per cluster — centroid sample vs random sample side by side
247
+ - Visual verification before committing to training
248
+
249
+ ---
250
+
251
+ ## License
252
+
253
+ MIT
@@ -0,0 +1,9 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ imgraft.egg-info/PKG-INFO
5
+ imgraft.egg-info/SOURCES.txt
6
+ imgraft.egg-info/dependency_links.txt
7
+ imgraft.egg-info/entry_points.txt
8
+ imgraft.egg-info/requires.txt
9
+ imgraft.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ imgraft = imgraft.cli:app
@@ -0,0 +1,19 @@
1
+ torch>=2.0.0
2
+ open-clip-torch>=2.20.0
3
+ hdbscan>=0.8.33
4
+ umap-learn>=0.5.3
5
+ numpy>=1.24.0
6
+ Pillow>=10.0.0
7
+ tqdm>=4.66.0
8
+ rich>=13.7.0
9
+ typer>=0.12.0
10
+ plotly>=5.18.0
11
+ scikit-learn>=1.3.0
12
+
13
+ [dev]
14
+ pytest>=8.0.0
15
+ pytest-cov>=5.0.0
16
+
17
+ [vis]
18
+ matplotlib>=3.8.0
19
+ kaleido>=0.2.1
@@ -0,0 +1,50 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "imgraft"
7
+ version = "0.1.0"
8
+ description = "Semantic image dataset curation using CLIP + HDBSCAN"
9
+ readme = "README.md"
10
+ license = { text = "MIT" }
11
+ requires-python = ">=3.9"
12
+ keywords = [
13
+ "clip", "hdbscan", "dataset-curation", "computer-vision",
14
+ "image-deduplication", "semantic-clustering", "umap"
15
+ ]
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "License :: OSI Approved :: MIT License",
19
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
20
+ "Topic :: Scientific/Engineering :: Image Processing",
21
+ ]
22
+
23
+ dependencies = [
24
+ "torch>=2.0.0",
25
+ "open-clip-torch>=2.20.0",
26
+ "hdbscan>=0.8.33",
27
+ "umap-learn>=0.5.3",
28
+ "numpy>=1.24.0",
29
+ "Pillow>=10.0.0",
30
+ "tqdm>=4.66.0",
31
+ "rich>=13.7.0",
32
+ "typer>=0.12.0",
33
+ "plotly>=5.18.0",
34
+ "scikit-learn>=1.3.0",
35
+ ]
36
+
37
+ [project.optional-dependencies]
38
+ dev = ["pytest>=8.0.0", "pytest-cov>=5.0.0"]
39
+ vis = ["matplotlib>=3.8.0", "kaleido>=0.2.1"] # static PNG export
40
+
41
+ [project.urls]
42
+ Homepage = "https://github.com/dasarinikhil/imgraft"
43
+ Issues = "https://github.com/dasarinikhil/imgraft/issues"
44
+
45
+ [project.scripts]
46
+ imgraft = "imgraft.cli:app"
47
+
48
+ [tool.setuptools.packages.find]
49
+ where = ["."]
50
+ include = ["imgraft*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+