File size: 8,251 Bytes
3a303bb
 
a83bb1b
3a303bb
 
 
a83bb1b
3a303bb
a83bb1b
3a303bb
a83bb1b
 
 
 
 
 
 
 
 
 
 
 
 
 
3a303bb
 
 
 
 
 
 
 
 
 
 
 
a83bb1b
d2479e9
8f6b9e6
 
d2479e9
322df93
4d94265
8f6b9e6
d2479e9
a83bb1b
3a303bb
a83bb1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a303bb
 
a83bb1b
 
 
 
 
 
3a303bb
 
a83bb1b
 
 
 
 
 
 
 
 
 
3a303bb
 
 
 
 
a83bb1b
3a303bb
 
 
a83bb1b
3a303bb
a83bb1b
3a303bb
 
 
 
 
 
 
 
a83bb1b
 
3a303bb
 
 
 
 
 
 
 
 
a83bb1b
3a303bb
a83bb1b
 
 
 
 
 
 
 
 
 
 
3a303bb
a83bb1b
3a303bb
 
 
 
a83bb1b
 
 
 
 
3a303bb
 
a83bb1b
 
 
3a303bb
a83bb1b
3a303bb
 
 
 
 
a83bb1b
 
 
 
 
3a303bb
 
 
 
 
 
 
a83bb1b
3a303bb
a83bb1b
3a303bb
a83bb1b
3a303bb
 
 
 
 
 
 
 
 
a83bb1b
3a303bb
 
 
 
 
 
a83bb1b
3a303bb
 
 
 
 
 
 
 
 
 
 
a83bb1b
3a303bb
a83bb1b
3a303bb
a83bb1b
 
 
3a303bb
a83bb1b
 
3a303bb
 
a83bb1b
3a303bb
a83bb1b
3a303bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d94265
 
 
 
3a303bb
 
 
4d94265
 
 
 
3a303bb
 
 
a83bb1b
3a303bb
 
 
a83bb1b
 
3a303bb
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
---
viewer: false
tags: [uv-script, computer-vision, object-detection, image-segmentation, sam3, image-processing, hf-jobs]
license: apache-2.0
---

# SAM3 Vision Scripts

Detect and segment objects in images using Meta's **SAM3** (Segment Anything Model 3) with text prompts. Process HuggingFace datasets with zero-shot detection and segmentation using natural language descriptions.

| Script | What it does | Output |
|--------|-------------|--------|
| `detect-objects.py` | Object detection with bounding boxes | `objects` column with bbox, category, score |
| `segment-objects.py` | Pixel-level segmentation masks | Segmentation maps or per-instance masks |

Browse results interactively: **[SAM3 Results Browser](https://huggingface.co/spaces/uv-scripts/sam3-detection-browser)**

---

## Object Detection (`detect-objects.py`)

Detect objects and output bounding boxes in HuggingFace object detection format.

### Quick Start

**Requires GPU.** Use HuggingFace Jobs for cloud execution:

```bash
hf jobs uv run --flavor a100-large \
    -s HF_TOKEN=HF_TOKEN \
    https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \
    input-dataset \
    output-dataset \
    --class-name photograph
```

### Example Output

<div style="max-width: 400px;">
<img src="./example-detection.png" alt="Example Detection" style="width: 100%; height: auto;"/>

_Photograph detected in a historical newspaper with bounding box and confidence score. Generated from [davanstrien/newspapers-image-predictions](https://huggingface.co/datasets/davanstrien/newspapers-image-predictions)._

</div>

### Arguments

**Required:**

- `input_dataset` - Input HF dataset ID
- `output_dataset` - Output HF dataset ID
- `--class-name` - Object class to detect (e.g., `"photograph"`, `"animal"`, `"table"`)

**Common options:**

- `--confidence-threshold FLOAT` - Min confidence (default: 0.5)
- `--batch-size INT` - Batch size (default: 4)
- `--max-samples INT` - Limit samples for testing
- `--image-column STR` - Image column name (default: "image")
- `--private` - Make output private

<details>
<summary>All options</summary>

```
--mask-threshold FLOAT       Mask generation threshold (default: 0.5)
--split STR                  Dataset split (default: "train")
--shuffle                    Shuffle before processing
--model STR                  Model ID (default: "facebook/sam3")
--dtype STR                  Precision: float32|float16|bfloat16
--hf-token STR               HF token (or use HF_TOKEN env var)
```

</details>

### Output Format

Adds `objects` column with ClassLabel-based detections:

```python
{
    "objects": [
        {
            "bbox": [x, y, width, height],
            "category": 0,  # Always 0 for single class
            "score": 0.87
        }
    ]
}
```

---

## Image Segmentation (`segment-objects.py`)

Produce pixel-level segmentation masks for objects matching a text prompt. Two output formats available.

### Quick Start

```bash
hf jobs uv run --flavor a100-large \
    -s HF_TOKEN=HF_TOKEN \
    https://huggingface.co/datasets/uv-scripts/sam3/raw/main/segment-objects.py \
    input-dataset \
    output-dataset \
    --class-name deer
```

### Example Output

<div style="max-width: 400px;">
<img src="./example-segmentation.png" alt="Example Segmentation" style="width: 100%; height: auto;"/>

_Deer segmented in a wildlife camera trap image with pixel-level mask and bounding box. Generated from [davanstrien/ena24-detection](https://huggingface.co/datasets/davanstrien/ena24-detection)._

</div>

### Arguments

**Required:**

- `input_dataset` - Input HF dataset ID
- `output_dataset` - Output HF dataset ID
- `--class-name` - Object class to segment (e.g., `"deer"`, `"animal"`, `"table"`)

**Common options:**

- `--output-format` - `semantic-mask` (default) or `instance-masks`
- `--confidence-threshold FLOAT` - Min confidence (default: 0.5)
- `--include-boxes` - Also output bounding boxes
- `--batch-size INT` - Batch size (default: 4)
- `--max-samples INT` - Limit samples for testing
- `--private` - Make output private

<details>
<summary>All options</summary>

```
--mask-threshold FLOAT       Mask binarization threshold (default: 0.5)
--image-column STR           Image column name (default: "image")
--split STR                  Dataset split (default: "train")
--shuffle                    Shuffle before processing
--model STR                  Model ID (default: "facebook/sam3")
--dtype STR                  Precision: float32|float16|bfloat16
--hf-token STR               HF token (or use HF_TOKEN env var)
```

</details>

### Output Formats

**Semantic mask** (`--output-format semantic-mask`, default):
- Adds `segmentation_map` column: single image per sample where pixel value = instance ID (0 = background)
- More compact, viewable in the HF dataset viewer
- Also adds `num_instances` and `scores` columns

**Instance masks** (`--output-format instance-masks`):
- Adds `segmentation_masks` column: list of binary mask images (one per detected instance)
- Also adds `scores` and `category` columns
- Best for extracting individual objects or creating training data

### Example

Segment deer in wildlife camera trap images:

```bash
hf jobs uv run --flavor a100-large \
    -s HF_TOKEN=HF_TOKEN \
    https://huggingface.co/datasets/uv-scripts/sam3/raw/main/segment-objects.py \
    davanstrien/ena24-detection \
    my-username/wildlife-segmented \
    --class-name deer \
    --include-boxes
```

---

## HuggingFace Jobs Examples

### Historical Newspapers

```bash
hf jobs uv run --flavor a100-large \
    -s HF_TOKEN=HF_TOKEN \
    https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \
    davanstrien/newspapers-with-images-after-photography \
    my-username/newspapers-detected \
    --class-name photograph \
    --confidence-threshold 0.6 \
    --batch-size 8
```

### Wildlife Camera Traps

```bash
hf jobs uv run --flavor a100-large \
    -s HF_TOKEN=HF_TOKEN \
    https://huggingface.co/datasets/uv-scripts/sam3/raw/main/segment-objects.py \
    wildlife-images \
    wildlife-segmented \
    --class-name animal \
    --include-boxes
```

### Quick Testing

Test on a small subset before full run:

```bash
hf jobs uv run --flavor a100-large \
    -s HF_TOKEN=HF_TOKEN \
    https://huggingface.co/datasets/uv-scripts/sam3/raw/main/segment-objects.py \
    large-dataset \
    test-output \
    --class-name object \
    --max-samples 20
```

### GPU Flavors

```bash
# L4 (cost-effective)
--flavor l4x1

# A100 (fastest)
--flavor a100
```

See [HF Jobs pricing](https://huggingface.co/pricing#spaces-compute).

## Local Execution

If you have a CUDA GPU locally:

```bash
# Detection
uv run detect-objects.py INPUT OUTPUT --class-name CLASSNAME

# Segmentation
uv run segment-objects.py INPUT OUTPUT --class-name CLASSNAME
```

## Multiple Object Types

Run the script multiple times with different `--class-name` values:

```bash
hf jobs uv run ... --class-name photograph
hf jobs uv run ... --class-name illustration
```

## Performance

| GPU | Batch Size | ~Images/sec |
| --- | ---------- | ----------- |
| L4  | 4-8        | 2-4         |
| A10 | 8-16       | 4-6         |

_Varies by image size and detection complexity_

## Common Use Cases

- **Documents:** `--class-name table` or `--class-name figure`
- **Newspapers:** `--class-name photograph` or `--class-name illustration`
- **Wildlife:** `--class-name animal` or `--class-name bird`
- **Products:** `--class-name product` or `--class-name label`

## Troubleshooting

- **No CUDA:** Use HF Jobs (see examples above)
- **OOM errors:** Reduce `--batch-size`
- **Few detections:** Lower `--confidence-threshold` or try different class descriptions
- **Wrong column:** Use `--image-column your_column_name`

## About SAM3

[SAM3](https://huggingface.co/facebook/sam3) is Meta's zero-shot vision model. Describe any object in natural language and it will detect and segment it — no training required.

## See Also

- **[SAM3 Results Browser](https://huggingface.co/spaces/uv-scripts/sam3-detection-browser)** - Browse detection and segmentation results interactively
- More UV scripts at [huggingface.co/uv-scripts](https://huggingface.co/uv-scripts)

## License

Apache 2.0