File size: 12,493 Bytes
213c8b6 1ab0327 213c8b6 db8245e 213c8b6 db8245e 213c8b6 db8245e 213c8b6 db8245e 213c8b6 db8245e 213c8b6 db8245e 213c8b6 db8245e 213c8b6 a9a3a95 213c8b6 a9a3a95 213c8b6 a9a3a95 213c8b6 a9a3a95 213c8b6 a9a3a95 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 db8245e 213c8b6 db8245e 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 db8245e 213c8b6 85c3e70 213c8b6 db8245e 213c8b6 7ecd895 213c8b6 7ecd895 85c3e70 189463a 213c8b6 7ecd895 213c8b6 7ecd895 213c8b6 67f6a39 2def010 67f6a39 2def010 67f6a39 213c8b6 85c3e70 213c8b6 67f6a39 213c8b6 2def010 db8245e 213c8b6 2def010 213c8b6 a9a3a95 213c8b6 67f6a39 213c8b6 db8245e 213c8b6 db8245e 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 2def010 cc71572 2def010 cc71572 213c8b6 db8245e 213c8b6 85c3e70 213c8b6 0be3fcd 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 85c3e70 213c8b6 |
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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "transformers@git+https://github.com/huggingface/transformers.git@1fba72361e8e0e865d569f7cd15e5aa50b41ac9a",
# "datasets",
# "huggingface-hub",
# "pillow",
# "tqdm",
# "torchvision",
# "accelerate",
# ]
# ///
"""
Detect objects in images using Meta's SAM3 (Segment Anything Model 3).
This script processes images from a HuggingFace dataset and detects a single object
type based on a text prompt, outputting bounding boxes in HuggingFace object detection format.
Examples:
# Detect photographs in historical newspapers
uv run detect-objects.py \\
davanstrien/newspapers-with-images-after-photography \\
my-username/newspapers-detected \\
--class-name photograph
# Detect animals in camera trap images
uv run detect-objects.py \\
wildlife-images \\
wildlife-detected \\
--class-name animal \\
--confidence-threshold 0.6
# Test on small subset
uv run detect-objects.py input output \\
--class-name table \\
--max-samples 10
# Run on HF Jobs with L4 GPU
hf jobs uv run --flavor l4x1 \\
-s HF_TOKEN=$HF_TOKEN \\
https://huggingface.co/datasets/uv-scripts/sam3/raw/main/detect-objects.py \\
input-dataset output-dataset \\
--class-name photograph \\
--confidence-threshold 0.5
Performance:
- L4 GPU: ~2-4 images/sec (depending on image size and batch size)
- Memory: ~8-12 GB VRAM
- Recommended batch size: 4-8 for L4, 8-16 for A10
Note: To detect multiple object types, run the script multiple times with different
--class-name values and merge the results.
"""
import argparse
import logging
import os
import sys
from typing import Any, Dict, List
import torch
from datasets import ClassLabel, Dataset, Features, Sequence, Value, load_dataset
from datasets import Image as ImageFeature
from huggingface_hub import HfApi, login
from PIL import Image
from tqdm.auto import tqdm
from transformers import Sam3Model, Sam3Processor
os.environ["HF_XET_HIGH_PERFORMANCE"] = "1"
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
)
logger = logging.getLogger(__name__)
# GPU availability check
if not torch.cuda.is_available():
logger.error("β CUDA is not available. This script requires a GPU.")
logger.error("For local testing, ensure you have a CUDA-capable GPU.")
logger.error("For cloud execution, use HF Jobs with --flavor l4x1 or similar.")
sys.exit(1)
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Detect objects in images using SAM3",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
# Required arguments
parser.add_argument(
"input_dataset", help="Input HuggingFace dataset ID (e.g., 'username/dataset')"
)
parser.add_argument(
"output_dataset", help="Output HuggingFace dataset ID (e.g., 'username/output')"
)
# Object detection configuration
parser.add_argument(
"--class-name",
required=True,
help="Object class to detect (e.g., 'photograph', 'animal', 'table')",
)
parser.add_argument(
"--confidence-threshold",
type=float,
default=0.5,
help="Minimum confidence score for detections (default: 0.5)",
)
parser.add_argument(
"--mask-threshold",
type=float,
default=0.5,
help="Threshold for mask generation (default: 0.5)",
)
# Dataset configuration
parser.add_argument(
"--image-column",
default="image",
help="Name of the column containing images (default: 'image')",
)
parser.add_argument(
"--split", default="train", help="Dataset split to process (default: 'train')"
)
parser.add_argument(
"--max-samples",
type=int,
default=None,
help="Maximum number of samples to process (for testing)",
)
parser.add_argument(
"--shuffle", action="store_true", help="Shuffle dataset before processing"
)
# Processing configuration
parser.add_argument(
"--batch-size",
type=int,
default=4,
help="Batch size for processing (default: 4)",
)
parser.add_argument(
"--model",
default="facebook/sam3",
help="SAM3 model ID (default: 'facebook/sam3')",
)
parser.add_argument(
"--dtype",
default="bfloat16",
choices=["float32", "float16", "bfloat16"],
help="Model precision (default: 'bfloat16')",
)
# Output configuration
parser.add_argument(
"--private", action="store_true", help="Make output dataset private"
)
parser.add_argument(
"--hf-token",
default=None,
help="HuggingFace token (default: uses HF_TOKEN env var or cached token)",
)
return parser.parse_args()
def load_and_validate_dataset(
dataset_id: str,
split: str,
image_column: str,
max_samples: int = None,
shuffle: bool = False,
hf_token: str = None,
) -> Dataset:
"""Load dataset and validate it has the required image column."""
logger.info(f"π Loading dataset: {dataset_id} (split: {split})")
try:
dataset = load_dataset(dataset_id, split=split, token=hf_token)
except Exception as e:
logger.error(f"Failed to load dataset '{dataset_id}': {e}")
sys.exit(1)
# Validate image column exists
if image_column not in dataset.column_names:
logger.error(f"Column '{image_column}' not found in dataset")
logger.error(f"Available columns: {dataset.column_names}")
sys.exit(1)
# Shuffle if requested
if shuffle:
logger.info("π Shuffling dataset")
dataset = dataset.shuffle()
# Limit samples if requested
if max_samples is not None:
logger.info(f"π’ Limiting to {max_samples} samples")
dataset = dataset.select(range(min(max_samples, len(dataset))))
logger.info(f"β
Loaded {len(dataset)} samples")
return dataset
def process_batch(
batch: Dict[str, List[Any]],
image_column: str,
class_name: str,
processor: Sam3Processor,
model: Sam3Model,
confidence_threshold: float,
mask_threshold: float,
) -> Dict[str, List[List[Dict[str, Any]]]]:
"""Process a batch of images and return detections for a single class."""
images = batch[image_column]
# Convert to PIL Images and ensure RGB
pil_images = []
for img in images:
if isinstance(img, str):
img = Image.open(img)
if img.mode == "L":
img = img.convert("RGB")
elif img.mode != "RGB":
img = img.convert("RGB")
pil_images.append(img)
# Process batch through model
try:
inputs = processor(
images=pil_images,
text=[class_name] * len(pil_images), # Same prompt for all images
return_tensors="pt",
).to(model.device, dtype=model.dtype)
with torch.no_grad():
outputs = model(**inputs)
# Post-process outputs using original_sizes from processor
results = processor.post_process_instance_segmentation(
outputs,
threshold=confidence_threshold,
mask_threshold=mask_threshold,
target_sizes=inputs.get("original_sizes").tolist(),
)
except Exception as e:
logger.warning(f"β οΈ Failed to process batch: {e}")
# Return empty detections for all images in batch
return {
"objects": [
{"bbox": [], "category": [], "score": []}
for _ in range(len(pil_images))
]
}
# Convert to HuggingFace object detection format (dict-of-lists per image)
batch_objects = []
for result in results:
boxes = result.get("boxes", torch.tensor([]))
scores = result.get("scores", torch.tensor([]))
# Handle empty results
if len(boxes) == 0:
batch_objects.append({"bbox": [], "category": [], "score": []})
continue
# Build lists for this image
image_bboxes = []
image_categories = []
image_scores = []
for box, score in zip(boxes.cpu().numpy(), scores.cpu().numpy()):
x1, y1, x2, y2 = box
width = x2 - x1
height = y2 - y1
image_bboxes.append([float(x1), float(y1), float(width), float(height)])
image_categories.append(0) # Single class, always index 0
image_scores.append(float(score))
batch_objects.append(
{
"bbox": image_bboxes,
"category": image_categories,
"score": image_scores,
}
)
return {"objects": batch_objects}
def main():
args = parse_args()
class_name = args.class_name.strip()
if not class_name:
logger.error("β Invalid --class-name argument. Provide a class name.")
sys.exit(1)
logger.info("π SAM3 Object Detection")
logger.info(f" Input: {args.input_dataset}")
logger.info(f" Output: {args.output_dataset}")
logger.info(f" Class: {class_name}")
logger.info(f" Confidence threshold: {args.confidence_threshold}")
logger.info(f" Batch size: {args.batch_size}")
# Authentication
if args.hf_token:
login(token=args.hf_token)
elif os.getenv("HF_TOKEN"):
login(token=os.getenv("HF_TOKEN"))
# Load dataset
dataset = load_and_validate_dataset(
args.input_dataset,
args.split,
args.image_column,
args.max_samples,
args.shuffle,
args.hf_token,
)
# Load model
logger.info(f"π€ Loading SAM3 model: {args.model}")
try:
processor = Sam3Processor.from_pretrained(args.model)
model = Sam3Model.from_pretrained(
args.model, torch_dtype=getattr(torch, args.dtype), device_map="auto"
)
logger.info(f"β
Model loaded on {model.device}")
except Exception as e:
logger.error(f"β Failed to load model: {e}")
logger.error("Ensure the model exists and you have access permissions")
sys.exit(1)
# Define output schema before processing (dict-of-lists format for object detection)
logger.info("π Creating output schema...")
new_features = dataset.features.copy()
new_features["objects"] = {
"bbox": Sequence(Sequence(Value("float32"), length=4)),
"category": Sequence(ClassLabel(names=[class_name])),
"score": Sequence(Value("float32")),
}
# Process dataset with explicit output features
logger.info("π Processing images...")
processed_dataset = dataset.map(
lambda batch: process_batch(
batch,
args.image_column,
class_name,
processor,
model,
args.confidence_threshold,
args.mask_threshold,
),
batched=True,
batch_size=args.batch_size,
features=new_features,
desc="Detecting objects",
)
# Calculate statistics
total_detections = sum(len(objs) for objs in processed_dataset["objects"])
images_with_detections = sum(len(objs) > 0 for objs in processed_dataset["objects"])
logger.info("β
Detection complete!")
logger.info(f" Total detections: {total_detections}")
logger.info(
f" Images with detections: {images_with_detections}/{len(processed_dataset)}"
)
logger.info(
f" Average detections per image: {total_detections / len(processed_dataset):.2f}"
)
# Push to hub
logger.info(f"π€ Pushing to HuggingFace Hub: {args.output_dataset}")
try:
processed_dataset.push_to_hub(args.output_dataset, private=args.private)
logger.info(
f"β
Dataset available at: https://huggingface.co/datasets/{args.output_dataset}"
)
except Exception as e:
logger.error(f"β Failed to push to hub: {e}")
logger.info("πΎ Saving locally as backup...")
processed_dataset.save_to_disk("./output_dataset")
logger.info("β
Saved to ./output_dataset")
sys.exit(1)
if __name__ == "__main__":
main()
|