Skip to content

Download notebook

Interactive Segmentation with SAM3

image

This notebook demonstrates how to segment remote sensing images interactively using the Segment Anything Model 3 (SAM3).

Installation

First, make sure you have the required dependencies installed:

1
# %pip install "segment-geospatial[samgeo3]"
1
# %pip install transformers==5.0.0rc0

Import Libraries

1
2
import leafmap
from samgeo import SamGeo3, download_file

Download Sample Data

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

1
2
url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/uc_berkeley.tif"
image_path = download_file(url)
1
2
3
m = leafmap.Map()
m.add_raster(image_path, layer_name="Satellite image")
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
2
3
sam3 = SamGeo3(
    backend="transformers", device=None, checkpoint_path=None, load_from_HF=True
)

Set the image

You can set the image by either passing the image path or the image URL.

1
sam3.set_image(image_path)

Generate masks with text prompt

1
sam3.generate_masks(prompt="building")
1
sam3.save_masks("masks.tif")

Show the results

1
sam3.show_anns()

annotation

1
sam3.show_masks()

Interactive segmentation

Enter a text prompt or draw a rectangle on the map, then click on the Segment button to perform instance segmentation.

1
sam3.show_map(height="700px", min_size=10)

Segmentation by text prompts.

image

Segmentation by bounding boxes.