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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
viewer: false
|
| 3 |
+
tags: [uv-script, computer-vision, object-detection, sam3, image-processing]
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
# SAM3 Object Detection
|
| 8 |
+
|
| 9 |
+
Detect objects in images using Meta's **SAM3** (Segment Anything Model 3) with text prompts. Process HuggingFace datasets with zero-shot object detection using natural language descriptions.
|
| 10 |
+
|
| 11 |
+
## Quick Start
|
| 12 |
+
|
| 13 |
+
**Requires GPU.** Use HuggingFace Jobs for cloud execution:
|
| 14 |
+
|
| 15 |
+
```bash
|
| 16 |
+
hf jobs uv run --flavor a100-large \
|
| 17 |
+
-s HF_TOKEN=HF_TOKEN \
|
| 18 |
+
https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \
|
| 19 |
+
input-dataset \
|
| 20 |
+
output-dataset \
|
| 21 |
+
--class-name photograph
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
## Local Execution
|
| 25 |
+
|
| 26 |
+
If you have a CUDA GPU locally:
|
| 27 |
+
|
| 28 |
+
```bash
|
| 29 |
+
uv run detect-objects.py INPUT OUTPUT --class-name CLASSNAME
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## Arguments
|
| 33 |
+
|
| 34 |
+
**Required:**
|
| 35 |
+
|
| 36 |
+
- `input_dataset` - Input HF dataset ID
|
| 37 |
+
- `output_dataset` - Output HF dataset ID
|
| 38 |
+
- `--class-name` - Object class to detect (e.g., `"photograph"`, `"animal"`, `"table"`)
|
| 39 |
+
|
| 40 |
+
**Common options:**
|
| 41 |
+
|
| 42 |
+
- `--confidence-threshold FLOAT` - Min confidence (default: 0.5)
|
| 43 |
+
- `--batch-size INT` - Batch size (default: 4)
|
| 44 |
+
- `--max-samples INT` - Limit samples for testing
|
| 45 |
+
- `--image-column STR` - Image column name (default: "image")
|
| 46 |
+
- `--private` - Make output private
|
| 47 |
+
|
| 48 |
+
<details>
|
| 49 |
+
<summary>All options</summary>
|
| 50 |
+
|
| 51 |
+
```
|
| 52 |
+
--mask-threshold FLOAT Mask generation threshold (default: 0.5)
|
| 53 |
+
--split STR Dataset split (default: "train")
|
| 54 |
+
--shuffle Shuffle before processing
|
| 55 |
+
--model STR Model ID (default: "facebook/sam3")
|
| 56 |
+
--dtype STR Precision: float32|float16|bfloat16
|
| 57 |
+
--hf-token STR HF token (or use HF_TOKEN env var)
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
</details>
|
| 61 |
+
|
| 62 |
+
## HuggingFace Jobs Examples
|
| 63 |
+
|
| 64 |
+
### Historical Newspapers
|
| 65 |
+
|
| 66 |
+
Detect photographs in historical newspaper scans:
|
| 67 |
+
|
| 68 |
+
```bash
|
| 69 |
+
hf jobs uv run --flavor a100-large \
|
| 70 |
+
-s HF_TOKEN=HF_TOKEN \
|
| 71 |
+
https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \
|
| 72 |
+
davanstrien/newspapers-with-images-after-photography \
|
| 73 |
+
my-username/newspapers-detected \
|
| 74 |
+
--class-name photograph \
|
| 75 |
+
--confidence-threshold 0.6 \
|
| 76 |
+
--batch-size 8
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
### Document Tables
|
| 80 |
+
|
| 81 |
+
Extract tables from document scans:
|
| 82 |
+
|
| 83 |
+
```bash
|
| 84 |
+
hf jobs uv run --flavor a100-large \
|
| 85 |
+
-s HF_TOKEN=HF_TOKEN \
|
| 86 |
+
https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \
|
| 87 |
+
my-documents \
|
| 88 |
+
documents-with-tables \
|
| 89 |
+
--class-name table
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
### Wildlife Camera Traps
|
| 93 |
+
|
| 94 |
+
Detect animals in camera trap images:
|
| 95 |
+
|
| 96 |
+
```bash
|
| 97 |
+
hf jobs uv run --flavor a100-large \
|
| 98 |
+
-s HF_TOKEN=HF_TOKEN \
|
| 99 |
+
https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \
|
| 100 |
+
wildlife-images \
|
| 101 |
+
wildlife-detections \
|
| 102 |
+
--class-name animal \
|
| 103 |
+
--confidence-threshold 0.5
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
### Quick Testing
|
| 107 |
+
|
| 108 |
+
Test on a small subset before full run:
|
| 109 |
+
|
| 110 |
+
```bash
|
| 111 |
+
hf jobs uv run --flavor a100-large \
|
| 112 |
+
-s HF_TOKEN=HF_TOKEN \
|
| 113 |
+
https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \
|
| 114 |
+
large-dataset \
|
| 115 |
+
test-output \
|
| 116 |
+
--class-name object \
|
| 117 |
+
--max-samples 20
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
### Using Different GPU Flavors
|
| 121 |
+
|
| 122 |
+
```bash
|
| 123 |
+
# L4 (cost-effective)
|
| 124 |
+
--flavor l4x1
|
| 125 |
+
|
| 126 |
+
# A100 (fastest)
|
| 127 |
+
--flavor a100
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
See [HF Jobs pricing](https://huggingface.co/pricing#spaces-compute).
|
| 131 |
+
|
| 132 |
+
## Output Format
|
| 133 |
+
|
| 134 |
+
Adds `objects` column with ClassLabel-based detections:
|
| 135 |
+
|
| 136 |
+
```python
|
| 137 |
+
{
|
| 138 |
+
"objects": [
|
| 139 |
+
{
|
| 140 |
+
"bbox": [x, y, width, height],
|
| 141 |
+
"category": 0, # Always 0 for single class
|
| 142 |
+
"score": 0.87
|
| 143 |
+
}
|
| 144 |
+
]
|
| 145 |
+
}
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
Load and use:
|
| 149 |
+
|
| 150 |
+
```python
|
| 151 |
+
from datasets import load_dataset
|
| 152 |
+
|
| 153 |
+
ds = load_dataset("username/output", split="train")
|
| 154 |
+
|
| 155 |
+
# ClassLabel feature preserves your class name
|
| 156 |
+
class_name = ds.features["objects"].feature["category"].names[0]
|
| 157 |
+
print(f"Detected class: {class_name}")
|
| 158 |
+
|
| 159 |
+
for sample in ds:
|
| 160 |
+
for obj in sample["objects"]:
|
| 161 |
+
print(f"{class_name}: {obj['score']:.2f} at {obj['bbox']}")
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
## Detecting Multiple Object Types
|
| 165 |
+
|
| 166 |
+
To detect multiple object types, run the script multiple times with different `--class-name` values:
|
| 167 |
+
|
| 168 |
+
```bash
|
| 169 |
+
# Detect photographs
|
| 170 |
+
hf jobs uv run ... --class-name photograph
|
| 171 |
+
|
| 172 |
+
# Detect illustrations
|
| 173 |
+
hf jobs uv run ... --class-name illustration
|
| 174 |
+
|
| 175 |
+
# Merge results as needed
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
## Performance
|
| 179 |
+
|
| 180 |
+
| GPU | Batch Size | ~Images/sec |
|
| 181 |
+
| --- | ---------- | ----------- |
|
| 182 |
+
| L4 | 4-8 | 2-4 |
|
| 183 |
+
| A10 | 8-16 | 4-6 |
|
| 184 |
+
|
| 185 |
+
_Varies by image size and detection complexity_
|
| 186 |
+
|
| 187 |
+
## Common Use Cases
|
| 188 |
+
|
| 189 |
+
**Documents:** `--class-name table` or `--class-name figure`
|
| 190 |
+
**Newspapers:** `--class-name photograph` or `--class-name illustration`
|
| 191 |
+
**Wildlife:** `--class-name animal` or `--class-name bird`
|
| 192 |
+
**Products:** `--class-name product` or `--class-name label`
|
| 193 |
+
|
| 194 |
+
## Troubleshooting
|
| 195 |
+
|
| 196 |
+
**No CUDA:** Use HF Jobs (see examples above)
|
| 197 |
+
**OOM errors:** Reduce `--batch-size`
|
| 198 |
+
**Few detections:** Lower `--confidence-threshold` or try different class descriptions
|
| 199 |
+
**Wrong column:** Use `--image-column your_column_name`
|
| 200 |
+
|
| 201 |
+
## About SAM3
|
| 202 |
+
|
| 203 |
+
[SAM3](https://huggingface.co/facebook/sam3) is Meta's zero-shot vision model. Describe any object in natural language and it will detect it—no training required.
|
| 204 |
+
|
| 205 |
+
**Note:** This script uses transformers from git (SAM3 not yet in stable release).
|
| 206 |
+
|
| 207 |
+
## See Also
|
| 208 |
+
|
| 209 |
+
More UV scripts at [huggingface.co/uv-scripts](https://huggingface.co/uv-scripts):
|
| 210 |
+
|
| 211 |
+
- **dataset-creation** - Create HF datasets from files
|
| 212 |
+
- **vllm** - Fast LLM inference
|
| 213 |
+
- **ocr** - Document OCR
|
| 214 |
+
|
| 215 |
+
## License
|
| 216 |
+
|
| 217 |
+
Apache 2.0
|