Skip to content

Download notebook

Batch Segmentation for Remote Sensing Imagery with SAM 3

image

This notebook demonstrates how to do batch segmentation for remote sensing imagery with SAM 3.

Installation

First, make sure you have the required dependencies installed:

1
# %pip install "segment-geospatial[samgeo3]"

Import Libraries

1
2
import leafmap
from samgeo import SamGeo3, download_file

Download Sample Data

Let's download sample satellite images covering the University of California, Berkeley, for testing:

1
2
3
4
5
image_paths = []
for i in range(1, 5):
    url = f"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/uc_berkeley_{i}.tif"
    image_path = download_file(url)
    image_paths.append(image_path)
1
2
3
4
m = leafmap.Map()
for i, image_path in enumerate(image_paths):
    m.add_raster(image_path, layer_name=f"image_{i + 1}")
m

Request access to SAM3

To use SAM3, you need to request access by filling out this form on Hugging Face: https://huggingface.co/facebook/sam3

Once you have access, uncomment the following code block and run it.

1
2
# from huggingface_hub import login
# login()

Initialize SAM3

When initializing SAM3, you can choose the backend from "meta", or "transformers".

1
sam3 = SamGeo3(backend="meta", device=None, checkpoint_path=None, load_from_HF=True)

Set the image batch

1
sam3.set_image_batch(image_paths)

Generate masks with text prompt

Generate masks for all images with a text prompt

1
sam3.generate_masks_batch("building", min_size=100)
1
2
3
4
# Access results for each image
for i, result in enumerate(sam3.batch_results):
    print(f"Image {i + 1}: Found {len(result['masks'])} objects")
    # result contains: masks, boxes, scores, image, source

Show results

1
2
# Visualize all annotations in a grid
sam3.show_anns_batch(ncols=2, show_bbox=True, show_score=True)

Save results

1
sam3.show_anns_batch(output_dir="output/annotations/", prefix="ann", dpi=300)
1
2
3
4
# Save all masks to disk
saved_files = sam3.save_masks_batch(
    output_dir="output/", prefix="building_mask", unique=True
)