Segment Anything Model for Geospatial Data¶
This notebook shows how to use segment satellite imagery using the Segment Anything Model (SAM) with a few lines of code.
Make sure you use GPU runtime for this notebook. For Google Colab, go to Runtime -> Change runtime type and select GPU as the hardware accelerator.
Install dependencies¶
Uncomment and run the following cell to install the required dependencies.
1 | |
Import libraries¶
1 2 3 | |
Create an interactive map¶
1 2 3 | |
Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map
1 2 3 4 | |
Download map tiles¶
Download maps tiles and mosaic them into a single GeoTIFF file
1 | |
Besides the satellite basemap, you can use any of the following basemaps returned by the get_basemaps() function:
1 | |
Specify the basemap as the source.
1 | |
You can also use your own image. Uncomment and run the following cell to use your own image.
1 | |
Display the downloaded image on the map.
1 2 3 | |

Initialize SAM class¶
1 2 3 4 5 | |
Segment the image¶
Set batch=True to segment the image in batches. This is useful for large images that cannot fit in memory.
1 2 3 4 | |
Polygonize the raster data¶
Save the segmentation results as a GeoPackage file.
1 2 | |
You can also save the segmentation results as any vector data format supported by GeoPandas.
1 2 | |
Visualize the results¶
1 2 3 4 5 6 7 8 | |
