descriptron-sam3 2.5.1__py3-none-any.whl
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.
- descriptron_sam3/__init__.py +2 -0
- descriptron_sam3/cli.py +15 -0
- descriptron_sam3/tools/descriptron_sam3_instances.py +271 -0
- descriptron_sam3/tools/sam3_assets/README.txt +4 -0
- descriptron_sam3/tools/sam3_assets/bpe_simple_vocab_16e6.txt.gz +0 -0
- descriptron_sam3-2.5.1.dist-info/METADATA +53 -0
- descriptron_sam3-2.5.1.dist-info/RECORD +11 -0
- descriptron_sam3-2.5.1.dist-info/WHEEL +4 -0
- descriptron_sam3-2.5.1.dist-info/entry_points.txt +2 -0
- descriptron_sam3-2.5.1.dist-info/licenses/LICENSE +201 -0
- descriptron_sam3-2.5.1.dist-info/licenses/NOTICE +69 -0
descriptron_sam3/cli.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""descriptron_sam3.cli - run descriptron_sam3_instances.py (shipped in tools/, with SAM 3's text vocabulary)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import runpy
|
|
5
|
+
import sys
|
|
6
|
+
from importlib.resources import files
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main() -> None:
|
|
11
|
+
path = Path(str(files("descriptron_sam3") / "tools" / "descriptron_sam3_instances.py"))
|
|
12
|
+
if not path.is_file():
|
|
13
|
+
raise SystemExit(f"{path} is missing - was sync_sources.py run before building?")
|
|
14
|
+
sys.argv = [str(path), *sys.argv[1:]]
|
|
15
|
+
runpy.run_path(str(path), run_name="__main__")
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""descriptron_sam3_instances.py - find every instance of a structure with SAM 3 (multi-instance prediction).
|
|
3
|
+
|
|
4
|
+
Give SAM 3 a few outlined examples (for example five setae on a wing) and/or a text prompt ("seta"); it proposes
|
|
5
|
+
every matching instance in each image. The result is a COCO file Descriptron opens like any other prediction, so
|
|
6
|
+
the taxonomist deletes false ones and adds misses; corrected files can then train the Mask R-CNN detector.
|
|
7
|
+
|
|
8
|
+
Small, thin structures disappear when a whole photograph is shrunk to SAM 3's working size (1008 px), so every
|
|
9
|
+
image is cut into overlapping tiles at its own resolution, each tile is prompted, masks are mapped back to the
|
|
10
|
+
full image, and duplicates from overlapping tiles are merged (box IoU).
|
|
11
|
+
|
|
12
|
+
Prompts per tile:
|
|
13
|
+
--text "seta" text prompt (all tiles)
|
|
14
|
+
--exemplars examples.json COCO file with outlined examples of --category; each example box is a positive
|
|
15
|
+
visual prompt on the tiles that contain it (SAM 3 box prompts work within an image)
|
|
16
|
+
--boxes / --points prompts drawn in the GUI on ONE image (x0,y0,x1,y1;... and x,y,label;...): each
|
|
17
|
+
box is an example; clicked points first outline one example (SAM 3's SAM-1-style
|
|
18
|
+
interactive mode), whose box then becomes an example
|
|
19
|
+
--tile 0 no tiling: the whole image at SAM 3's working size
|
|
20
|
+
Runs in its own environment (Python 3.12, PyTorch >= 2.7): Descriptron's `sam3` conda env. The checkpoints are
|
|
21
|
+
gated: request access at https://huggingface.co/facebook/sam3 and log in (`hf auth login`) once.
|
|
22
|
+
|
|
23
|
+
python descriptron_sam3_instances.py --images wings/ --text seta --category seta --out setae_sam3.json
|
|
24
|
+
python descriptron_sam3_instances.py --images wings/ --exemplars five_setae.json --category seta \\
|
|
25
|
+
--text seta --tile 1008 --overlap 0.25 --confidence 0.5 --out setae_sam3.json
|
|
26
|
+
"""
|
|
27
|
+
import argparse, json, os, sys, time
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
import numpy as np
|
|
30
|
+
|
|
31
|
+
IMG_EXT = {".png", ".jpg", ".jpeg", ".tif", ".tiff", ".bmp"}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def parse_boxes(text):
|
|
35
|
+
"""'x0,y0,x1,y1;x0,y0,x1,y1' -> [[x0, y0, x1, y1], ...] (corners in any order)."""
|
|
36
|
+
out = []
|
|
37
|
+
for part in (text or "").split(";"):
|
|
38
|
+
if part.strip():
|
|
39
|
+
x0, y0, x1, y1 = (float(v) for v in part.split(","))
|
|
40
|
+
out.append([min(x0, x1), min(y0, y1), max(x0, x1), max(y0, y1)])
|
|
41
|
+
return out
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def parse_points(text):
|
|
45
|
+
"""'x,y,label;...' -> ([[x, y], ...], [label, ...]); label 1 = on the structure, 0 = background."""
|
|
46
|
+
pts, labs = [], []
|
|
47
|
+
for part in (text or "").split(";"):
|
|
48
|
+
if part.strip():
|
|
49
|
+
x, y, lab = part.split(","); pts.append([float(x), float(y)]); labs.append(int(float(lab)))
|
|
50
|
+
return pts, labs
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def point_window(pts, w, h, size):
|
|
54
|
+
"""a window at native resolution around clicked points: at least size x size (smaller if the image is), centred
|
|
55
|
+
on the points, inside the image. Thin structures vanish when a big photo is shrunk to SAM 3's 1008 px."""
|
|
56
|
+
xs = [p[0] for p in pts]; ys = [p[1] for p in pts]
|
|
57
|
+
x0, x1, y0, y1 = min(xs), max(xs), min(ys), max(ys)
|
|
58
|
+
ww = min(w, max(size, x1 - x0 + size // 4)); hh = min(h, max(size, y1 - y0 + size // 4))
|
|
59
|
+
cx, cy = (x0 + x1) / 2, (y0 + y1) / 2
|
|
60
|
+
left = int(min(max(0, cx - ww / 2), w - ww)); top = int(min(max(0, cy - hh / 2), h - hh))
|
|
61
|
+
return left, top, int(ww), int(hh)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def outline_points(proc, model, im, pts, labs, size):
|
|
65
|
+
"""SAM 3 point outline (SAM-1 style) on a native-resolution window; returns (full-image mask, score)."""
|
|
66
|
+
W, H = im.size
|
|
67
|
+
pos = [p for p, l in zip(pts, labs) if l == 1] or pts
|
|
68
|
+
x0, y0, ww, hh = point_window(pos, W, H, size)
|
|
69
|
+
keep = [(p, l) for p, l in zip(pts, labs) if x0 <= p[0] < x0 + ww and y0 <= p[1] < y0 + hh]
|
|
70
|
+
st = proc.set_image(im.crop((x0, y0, x0 + ww, y0 + hh)))
|
|
71
|
+
pm, ps, _ = model.predict_inst(st, point_coords=np.array([[p[0] - x0, p[1] - y0] for p, _ in keep], float),
|
|
72
|
+
point_labels=np.array([l for _, l in keep]), multimask_output=False)
|
|
73
|
+
full = np.zeros((H, W), bool)
|
|
74
|
+
m = np.squeeze(np.asarray(pm)[0])[:hh, :ww] > 0
|
|
75
|
+
if m.sum() > 0.5 * ww * hh: # the click missed the structure: SAM returns the background
|
|
76
|
+
print(f"point {pos[0]}: outline covers most of its window (click on background?) - skipped", flush=True)
|
|
77
|
+
return full, 0.0
|
|
78
|
+
full[y0:y0 + hh, x0:x0 + ww] = m
|
|
79
|
+
return full, float(np.asarray(ps).ravel()[0])
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def find_bpe_vocab():
|
|
83
|
+
"""SAM 3's text-encoder vocabulary: sam3's own copy (git install), else the copy shipped with Descriptron
|
|
84
|
+
(the sam3 0.1.4 wheel on PyPI leaves it out)."""
|
|
85
|
+
name = "bpe_simple_vocab_16e6.txt.gz"
|
|
86
|
+
cands = []
|
|
87
|
+
try:
|
|
88
|
+
import sam3
|
|
89
|
+
d = os.path.dirname(sam3.__file__)
|
|
90
|
+
cands += [os.path.join(d, "assets", name), os.path.join(d, "..", "assets", name)]
|
|
91
|
+
except Exception:
|
|
92
|
+
pass
|
|
93
|
+
here = os.path.dirname(os.path.abspath(__file__))
|
|
94
|
+
cands += [os.path.join(here, "sam3_assets", name), os.path.join(here, "..", "sam3_assets", name)]
|
|
95
|
+
for c in cands:
|
|
96
|
+
if os.path.isfile(c):
|
|
97
|
+
return os.path.abspath(c)
|
|
98
|
+
return None
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def tiles(w, h, size, overlap):
|
|
102
|
+
"""top-left corners of overlapping tiles covering a w x h image (one tile if the image is small; size 0 = one)."""
|
|
103
|
+
if size <= 0 or (w <= size and h <= size):
|
|
104
|
+
return [(0, 0, w, h)]
|
|
105
|
+
step = max(1, int(size * (1 - overlap)))
|
|
106
|
+
xs = list(range(0, max(w - size, 0) + 1, step)); ys = list(range(0, max(h - size, 0) + 1, step))
|
|
107
|
+
if xs[-1] + size < w: xs.append(w - size)
|
|
108
|
+
if ys[-1] + size < h: ys.append(h - size)
|
|
109
|
+
return [(x, y, min(size, w), min(size, h)) for y in ys for x in xs]
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def box_iou(a, b):
|
|
113
|
+
ix = max(0, min(a[2], b[2]) - max(a[0], b[0])); iy = max(0, min(a[3], b[3]) - max(a[1], b[1]))
|
|
114
|
+
inter = ix * iy; ua = (a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - inter
|
|
115
|
+
return inter / ua if ua > 0 else 0.0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def merge(dets, iou):
|
|
119
|
+
"""greedy non-maximum suppression across tiles (keeps the higher-scoring of two overlapping detections)."""
|
|
120
|
+
dets = sorted(dets, key=lambda d: -d["score"]); kept = []
|
|
121
|
+
for d in dets:
|
|
122
|
+
if all(box_iou(d["box"], k["box"]) < iou for k in kept):
|
|
123
|
+
kept.append(d)
|
|
124
|
+
return kept
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def mask_to_polygons(mask):
|
|
128
|
+
import cv2
|
|
129
|
+
cs, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
130
|
+
return [c.flatten().astype(float).tolist() for c in cs if len(c) >= 3]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def load_exemplars(path, category):
|
|
134
|
+
"""example boxes [x0, y0, x1, y1] per image file name, from a COCO file (annotations of `category`)."""
|
|
135
|
+
d = json.load(open(path)); cats = {c["id"]: c["name"] for c in d["categories"]}
|
|
136
|
+
names = {im["id"]: os.path.basename(im["file_name"]) for im in d["images"]}
|
|
137
|
+
out = {}
|
|
138
|
+
for a in d["annotations"]:
|
|
139
|
+
if category and cats.get(a["category_id"]) != category:
|
|
140
|
+
continue
|
|
141
|
+
x, y, w, h = a["bbox"]; out.setdefault(names[a["image_id"]], []).append([x, y, x + w, y + h])
|
|
142
|
+
return out
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def main(argv=None):
|
|
146
|
+
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
147
|
+
ap.add_argument("--images", required=True, help="folder of images (or one image file)")
|
|
148
|
+
ap.add_argument("--text", default=None, help="text prompt, e.g. seta")
|
|
149
|
+
ap.add_argument("--exemplars", default=None, help="COCO file with a few outlined examples (positive box prompts)")
|
|
150
|
+
ap.add_argument("--boxes", default=None, help="example boxes on a single image: x0,y0,x1,y1;...")
|
|
151
|
+
ap.add_argument("--points", default=None, help="clicked points on a single image: x,y,label;... (outline one example)")
|
|
152
|
+
ap.add_argument("--points_mode", default="example", choices=["example", "each", "examples"],
|
|
153
|
+
help="example: all points outline ONE example and SAM 3 finds all like it; each: one mask per "
|
|
154
|
+
"positive point (negative points shared), no search; examples: one outline per positive "
|
|
155
|
+
"point, then all outlines together are the examples for the search")
|
|
156
|
+
ap.add_argument("--point_window", type=int, default=1008,
|
|
157
|
+
help="clicked points are outlined on a window of this many native pixels around them (default 1008)")
|
|
158
|
+
ap.add_argument("--category", default=None, help="category name to write (and to read from --exemplars)")
|
|
159
|
+
ap.add_argument("--tile", type=int, default=1008, help="tile size in pixels (SAM 3 works at 1008; 0 = whole image, no tiling)")
|
|
160
|
+
ap.add_argument("--overlap", type=float, default=0.25, help="tile overlap fraction (default 0.25)")
|
|
161
|
+
ap.add_argument("--confidence", type=float, default=0.5, help="keep detections at or above this score")
|
|
162
|
+
ap.add_argument("--merge_iou", type=float, default=0.5, help="box IoU above which tile duplicates merge")
|
|
163
|
+
ap.add_argument("--min_area", type=int, default=4, help="drop masks smaller than this many pixels")
|
|
164
|
+
ap.add_argument("--device", default="cuda")
|
|
165
|
+
ap.add_argument("--out", required=True, help="output COCO json")
|
|
166
|
+
a = ap.parse_args(argv)
|
|
167
|
+
if not (a.text or a.exemplars or a.boxes or a.points):
|
|
168
|
+
ap.error("give --text, --exemplars, --boxes and/or --points")
|
|
169
|
+
if a.points_mode in ("each", "examples") and not a.points:
|
|
170
|
+
ap.error("--points_mode each needs --points")
|
|
171
|
+
if (a.boxes or a.points) and not Path(a.images).is_file():
|
|
172
|
+
ap.error("--boxes / --points are drawn on one image: give that image file as --images")
|
|
173
|
+
cat_name = a.category or a.text or "instance"
|
|
174
|
+
from PIL import Image
|
|
175
|
+
import torch
|
|
176
|
+
from sam3.model_builder import build_sam3_image_model
|
|
177
|
+
from sam3.model.sam3_image_processor import Sam3Processor
|
|
178
|
+
try:
|
|
179
|
+
model = build_sam3_image_model(bpe_path=find_bpe_vocab(), enable_inst_interactivity=bool(a.points))
|
|
180
|
+
except Exception as e:
|
|
181
|
+
sys.exit(f"Could not load SAM 3 ({e}).\nThe checkpoints are gated: request access at "
|
|
182
|
+
"https://huggingface.co/facebook/sam3, then run `hf auth login` once in the sam3 environment.")
|
|
183
|
+
proc = Sam3Processor(model, resolution=a.tile if a.tile > 0 else 1008, device=a.device,
|
|
184
|
+
confidence_threshold=a.confidence)
|
|
185
|
+
ex = load_exemplars(a.exemplars, a.category) if a.exemplars else {}
|
|
186
|
+
p = Path(a.images); files = [p] if p.is_file() else sorted(f for f in p.iterdir() if f.suffix.lower() in IMG_EXT)
|
|
187
|
+
coco = {"images": [], "annotations": [], "categories": [{"id": 1, "name": cat_name}],
|
|
188
|
+
"info": {"description": "SAM 3 multi-instance proposals (descriptron_sam3_instances.py); review before use",
|
|
189
|
+
"text_prompt": a.text, "exemplars": a.exemplars, "tile": a.tile, "overlap": a.overlap,
|
|
190
|
+
"confidence": a.confidence}}
|
|
191
|
+
t0 = time.time(); aid = 0
|
|
192
|
+
for k, f in enumerate(files, 1):
|
|
193
|
+
im = Image.open(f).convert("RGB"); W, H = im.size
|
|
194
|
+
coco["images"].append({"id": k, "file_name": f.name, "width": W, "height": H})
|
|
195
|
+
boxes_here = ex.get(f.name, []) + parse_boxes(a.boxes)
|
|
196
|
+
dets = []
|
|
197
|
+
if a.points and a.points_mode in ("each", "examples"): # one structure per positive click
|
|
198
|
+
pts, labs = parse_points(a.points)
|
|
199
|
+
neg = [q for q, l in zip(pts, labs) if l == 0]
|
|
200
|
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16, enabled=a.device == "cuda"):
|
|
201
|
+
for q, l in zip(pts, labs):
|
|
202
|
+
if l != 1:
|
|
203
|
+
continue
|
|
204
|
+
m, sc = outline_points(proc, model, im, [q] + neg, [1] + [0] * len(neg), a.point_window)
|
|
205
|
+
if m.sum() > 0.5 * W * H: # a click on the background returns the background
|
|
206
|
+
print(f"point {q}: mask covers most of the image (background?) - skipped", flush=True)
|
|
207
|
+
elif m.sum() >= a.min_area:
|
|
208
|
+
ys_, xs_ = np.nonzero(m)
|
|
209
|
+
dets.append({"mask": m, "score": sc,
|
|
210
|
+
"box": [float(xs_.min()), float(ys_.min()), float(xs_.max() + 1), float(ys_.max() + 1)]})
|
|
211
|
+
if a.points_mode == "each": # nothing else
|
|
212
|
+
boxes_here = []
|
|
213
|
+
tile_list = []
|
|
214
|
+
else: # "examples": every outline's box is an example, together
|
|
215
|
+
boxes_here += [d["box"] for d in dets]
|
|
216
|
+
print(f"{len(dets)} clicked outline(s) -> examples", flush=True)
|
|
217
|
+
tile_list = tiles(W, H, a.tile, a.overlap)
|
|
218
|
+
else:
|
|
219
|
+
tile_list = tiles(W, H, a.tile, a.overlap)
|
|
220
|
+
if a.points and a.points_mode == "example": # clicked points -> one outlined example (whole image) -> its box
|
|
221
|
+
pts, labs = parse_points(a.points)
|
|
222
|
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16, enabled=a.device == "cuda"):
|
|
223
|
+
m, _sc = outline_points(proc, model, im, pts, labs, a.point_window)
|
|
224
|
+
if m.any():
|
|
225
|
+
ys_, xs_ = np.nonzero(m)
|
|
226
|
+
boxes_here.append([float(xs_.min()), float(ys_.min()), float(xs_.max() + 1), float(ys_.max() + 1)])
|
|
227
|
+
print(f"points -> example box {boxes_here[-1]}", flush=True)
|
|
228
|
+
else:
|
|
229
|
+
print("points gave no example mask", flush=True)
|
|
230
|
+
for (x0, y0, tw, th) in tile_list:
|
|
231
|
+
tile = im.crop((x0, y0, x0 + tw, y0 + th))
|
|
232
|
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16, enabled=a.device == "cuda"):
|
|
233
|
+
st = proc.set_image(tile)
|
|
234
|
+
out = None
|
|
235
|
+
if a.text:
|
|
236
|
+
out = proc.set_text_prompt(prompt=a.text, state=st)
|
|
237
|
+
for (bx0, by0, bx1, by1) in boxes_here: # examples inside this tile only
|
|
238
|
+
cx0, cy0, cx1, cy1 = max(bx0, x0), max(by0, y0), min(bx1, x0 + tw), min(by1, y0 + th)
|
|
239
|
+
if cx1 - cx0 < 2 or cy1 - cy0 < 2:
|
|
240
|
+
continue
|
|
241
|
+
nb = [((cx0 + cx1) / 2 - x0) / tw, ((cy0 + cy1) / 2 - y0) / th, (cx1 - cx0) / tw, (cy1 - cy0) / th]
|
|
242
|
+
out = proc.add_geometric_prompt(box=nb, label=True, state=st)
|
|
243
|
+
if out is None:
|
|
244
|
+
continue
|
|
245
|
+
masks = out["masks"].float().cpu().numpy(); scores = out["scores"].float().cpu().numpy()
|
|
246
|
+
boxes = out["boxes"].float().cpu().numpy()
|
|
247
|
+
for m, s, b in zip(masks, scores, boxes):
|
|
248
|
+
m = np.squeeze(m) > 0.5
|
|
249
|
+
if m.sum() < a.min_area:
|
|
250
|
+
continue
|
|
251
|
+
full = np.zeros((H, W), bool); full[y0:y0 + th, x0:x0 + tw] = m[:th, :tw]
|
|
252
|
+
dets.append({"mask": full, "score": float(s),
|
|
253
|
+
"box": [float(b[0] + x0), float(b[1] + y0), float(b[2] + x0), float(b[3] + y0)]})
|
|
254
|
+
kept = dets if a.points_mode == "each" and a.points else merge(dets, a.merge_iou)
|
|
255
|
+
for dd in kept:
|
|
256
|
+
polys = mask_to_polygons(dd["mask"])
|
|
257
|
+
if not polys:
|
|
258
|
+
continue
|
|
259
|
+
ys_, xs_ = np.nonzero(dd["mask"]); aid += 1
|
|
260
|
+
coco["annotations"].append({"id": aid, "image_id": k, "category_id": 1, "segmentation": polys,
|
|
261
|
+
"area": float(dd["mask"].sum()), "iscrowd": 0, "score": dd["score"],
|
|
262
|
+
"bbox": [float(xs_.min()), float(ys_.min()), float(xs_.max() - xs_.min() + 1),
|
|
263
|
+
float(ys_.max() - ys_.min() + 1)]})
|
|
264
|
+
print(f"[{k}/{len(files)}] {f.name}: {len(kept)} instances", flush=True)
|
|
265
|
+
Path(a.out).parent.mkdir(parents=True, exist_ok=True)
|
|
266
|
+
json.dump(coco, open(a.out, "w"))
|
|
267
|
+
print(f"wrote {a.out}: {aid} instances in {len(files)} images ({time.time() - t0:.0f} s)")
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
if __name__ == "__main__":
|
|
271
|
+
main()
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
bpe_simple_vocab_16e6.txt.gz - the byte-pair-encoding vocabulary of OpenAI CLIP (MIT licence,
|
|
2
|
+
https://github.com/openai/CLIP), used by SAM 3's text encoder. The sam3 wheel on PyPI (0.1.4) does not include it,
|
|
3
|
+
so Descriptron ships this copy; descriptron_sam3_instances.py uses sam3's own copy when there is one.
|
|
4
|
+
md5 933b7abbbbde62c36f02f0e6ccde464f
|
|
Binary file
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: descriptron-sam3
|
|
3
|
+
Version: 2.5.1
|
|
4
|
+
Summary: SAM 3 for Descriptron: find every instance of a small structure (e.g. setae) from examples, clicks or a word
|
|
5
|
+
Project-URL: Homepage, https://github.com/alexrvandam/Descriptron
|
|
6
|
+
Author: Alex R. Van Dam
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
License-File: NOTICE
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Image Recognition
|
|
14
|
+
Requires-Python: >=3.12
|
|
15
|
+
Requires-Dist: einops
|
|
16
|
+
Requires-Dist: huggingface-hub
|
|
17
|
+
Requires-Dist: numpy<2,>=1.26
|
|
18
|
+
Requires-Dist: opencv-python-headless>=4.8
|
|
19
|
+
Requires-Dist: pillow>=9
|
|
20
|
+
Requires-Dist: psutil
|
|
21
|
+
Requires-Dist: pycocotools
|
|
22
|
+
Requires-Dist: sam3==0.1.4
|
|
23
|
+
Requires-Dist: setuptools<81
|
|
24
|
+
Requires-Dist: torch>=2.7
|
|
25
|
+
Requires-Dist: torchvision
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# descriptron-sam3
|
|
29
|
+
|
|
30
|
+
SAM 3 for [Descriptron](https://github.com/alexrvandam/Descriptron): outline one or a few examples of a small
|
|
31
|
+
structure (for example setae on a wing or genitalia), click on them, or type a word, and SAM 3 proposes every
|
|
32
|
+
matching instance as a COCO file. The Descriptron GUI (descriptron-gui 2.5.1 and later) runs it from the dark
|
|
33
|
+
green **Find all instances (SAM 3)** button and shows the instances together, one colour each, to be labelled in
|
|
34
|
+
one step.
|
|
35
|
+
|
|
36
|
+
SAM 3 needs Python 3.12 and PyTorch >= 2.7, so install it in its own environment. pipx does that and puts the
|
|
37
|
+
`descriptron-sam3` command on your PATH, where the GUI finds it:
|
|
38
|
+
|
|
39
|
+
pipx install --python python3.12 descriptron-sam3
|
|
40
|
+
|
|
41
|
+
The SAM 3 checkpoints are gated: request access at https://huggingface.co/facebook/sam3 (SAM License), then log in
|
|
42
|
+
once (`pipx run --spec huggingface_hub hf auth login`, or `hf auth login` in the environment). The first run
|
|
43
|
+
downloads the checkpoint (3.3 GB). A CUDA GPU is strongly recommended.
|
|
44
|
+
|
|
45
|
+
descriptron-sam3 --images wings/ --text bristle --category seta --out setae.json
|
|
46
|
+
descriptron-sam3 --images wing1.tif --boxes "1200,800,1260,1100" --out setae.json
|
|
47
|
+
descriptron-sam3 --images wing1.tif --points "1520,4040,1" --points_mode each --tile 0 --out one_seta.json
|
|
48
|
+
|
|
49
|
+
Results are proposals to review in the GUI, not finished annotations. Examples work within the image they are
|
|
50
|
+
drawn on; a word searches every image tile by tile. If the GUI cannot find the command, point it at the Python of
|
|
51
|
+
the environment that has descriptron-sam3: `DESCRIPTRON_SAM3_PYTHON=/path/to/python`.
|
|
52
|
+
|
|
53
|
+
Licence: Apache-2.0 (see NOTICE for the CLIP vocabulary file and for SAM 3's own licence).
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
descriptron_sam3/__init__.py,sha256=S88-3kIeL_TfMeFI7wAfhNgGuIxrCthFdt4uzgS5cH8,124
|
|
2
|
+
descriptron_sam3/cli.py,sha256=CNGqhoH-8QPqGBzc3Kv6gnlIoQ-odYzc6yqeDoHcsZs,561
|
|
3
|
+
descriptron_sam3/tools/descriptron_sam3_instances.py,sha256=k1nmf4nvzypgqHPGjRueYo8OjtAbCkFqpNCL1Kh5jKg,15923
|
|
4
|
+
descriptron_sam3/tools/sam3_assets/README.txt,sha256=NMGYRP3H_cqjE6GJqDlYKBSvRx2ZitufZre_rOWV9fo,348
|
|
5
|
+
descriptron_sam3/tools/sam3_assets/bpe_simple_vocab_16e6.txt.gz,sha256=kkaRrCiOVECSNhFWUq1KolD0ggPeUKnkcipuzUjWgEo,1356917
|
|
6
|
+
descriptron_sam3-2.5.1.dist-info/METADATA,sha256=f1UkuzxFtjWbguXvZXquu9BYQqhfcbrX7qtcfUcPEdM,2617
|
|
7
|
+
descriptron_sam3-2.5.1.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
8
|
+
descriptron_sam3-2.5.1.dist-info/entry_points.txt,sha256=fCu14JptpZv15TU7m065PHjRSv5dh5oZ4YvCdSZ53jc,63
|
|
9
|
+
descriptron_sam3-2.5.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
10
|
+
descriptron_sam3-2.5.1.dist-info/licenses/NOTICE,sha256=RfTMWUQnFbPv_aHlOU_HU3SlTiBwp3WsLw3yfqZG8JQ,2935
|
|
11
|
+
descriptron_sam3-2.5.1.dist-info/RECORD,,
|
|
@@ -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.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Descriptron
|
|
2
|
+
Copyright 2024-2026 Alex R. Van Dam (ORCID 0000-0002-1966-0338)
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Alex R. Van Dam
|
|
5
|
+
https://github.com/alexrvandam/Descriptron
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (see LICENSE). Under Section
|
|
8
|
+
4(d) of that licence, this NOTICE file must be included with any
|
|
9
|
+
redistribution of this software or of any work derived from it.
|
|
10
|
+
|
|
11
|
+
REQUIRED CITATION
|
|
12
|
+
-----------------
|
|
13
|
+
Anyone who uses Descriptron, or any software derived from it (including
|
|
14
|
+
descriptron-core, descriptron-vision, descriptron-gui and descriptron-mcp), in
|
|
15
|
+
work that is published, presented or distributed must cite:
|
|
16
|
+
|
|
17
|
+
1. The software (Descriptron v2), by the DOI of the version used:
|
|
18
|
+
Van Dam, A. R. (2026). Descriptron: morphology-driven species descriptions,
|
|
19
|
+
keys and delimitation for dark taxa (Version 2.1.1). Zenodo.
|
|
20
|
+
https://doi.org/10.5281/zenodo.22958109
|
|
21
|
+
(all versions: https://doi.org/10.5281/zenodo.17077224;
|
|
22
|
+
repository: https://github.com/alexrvandam/Descriptron)
|
|
23
|
+
|
|
24
|
+
2. The first Descriptron paper:
|
|
25
|
+
Van Dam, A. R. & Štarhová Serbina, L. (2026). Descriptron: Artificial
|
|
26
|
+
intelligence for automating taxonomic species descriptions with a
|
|
27
|
+
user-friendly software package. Systematic Entomology, 51(1), e70005.
|
|
28
|
+
https://doi.org/10.1111/syen.70005
|
|
29
|
+
|
|
30
|
+
3. Once it is published, the Descriptron v2 paper as well. Its reference
|
|
31
|
+
will be added here and in CITATION.cff on publication.
|
|
32
|
+
|
|
33
|
+
BibTeX:
|
|
34
|
+
|
|
35
|
+
@software{vandam_2026_descriptron_v211,
|
|
36
|
+
author = {Van Dam, Alex R.},
|
|
37
|
+
title = {Descriptron: morphology-driven species descriptions, keys and
|
|
38
|
+
delimitation for dark taxa},
|
|
39
|
+
version = {v2.1.1},
|
|
40
|
+
year = {2026},
|
|
41
|
+
publisher = {Zenodo},
|
|
42
|
+
doi = {10.5281/zenodo.22958109},
|
|
43
|
+
url = {https://doi.org/10.5281/zenodo.22958109}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@article{vandam2026descriptron,
|
|
47
|
+
author = {Van Dam, Alex R. and Štarhová Serbina, Liliya},
|
|
48
|
+
title = {Descriptron: Artificial intelligence for automating taxonomic species
|
|
49
|
+
descriptions with a user-friendly software package},
|
|
50
|
+
journal = {Systematic Entomology},
|
|
51
|
+
volume = {51},
|
|
52
|
+
number = {1},
|
|
53
|
+
pages = {e70005},
|
|
54
|
+
year = {2026},
|
|
55
|
+
doi = {10.1111/syen.70005}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
The same information is in CITATION.cff, which GitHub shows as
|
|
59
|
+
"Cite this repository".
|
|
60
|
+
|
|
61
|
+
THIRD-PARTY FILES IN descriptron-sam3
|
|
62
|
+
-------------------------------------
|
|
63
|
+
tools/sam3_assets/bpe_simple_vocab_16e6.txt.gz - the byte-pair-encoding vocabulary of OpenAI CLIP
|
|
64
|
+
(https://github.com/openai/CLIP), MIT licence, Copyright (c) 2021 OpenAI. SAM 3's text encoder needs it and the
|
|
65
|
+
sam3 0.1.4 wheel on PyPI does not include it.
|
|
66
|
+
|
|
67
|
+
SAM 3 itself (the `sam3` package, installed as a dependency) is Meta's, under the SAM License; its model
|
|
68
|
+
checkpoints are gated and are downloaded by each user from https://huggingface.co/facebook/sam3 after requesting
|
|
69
|
+
access. Neither the SAM 3 code nor its checkpoints are redistributed here.
|