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.
In [ ]:
Copied!
# %pip install leafmap
# %pip install leafmap
In [ ]:
Copied!
# %pip install segment-geospatial localtileserver
# %pip install segment-geospatial localtileserver
Import libraries¶
In [ ]:
Copied!
import os
import leafmap
import torch
from samgeo import SamGeo, tms_to_geotiff
import os
import leafmap
import torch
from samgeo import SamGeo, tms_to_geotiff
Create an interactive map¶
In [ ]:
Copied!
m = leafmap.Map(center=[29.676840, -95.369222], zoom=19)
m.add_basemap('SATELLITE')
m
m = leafmap.Map(center=[29.676840, -95.369222], zoom=19)
m.add_basemap('SATELLITE')
m
Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map
In [ ]:
Copied!
if m.user_roi_bounds() is not None:
bbox = m.user_roi_bounds()
else:
bbox = [-95.3704, 29.6762, -95.368, 29.6775]
if m.user_roi_bounds() is not None:
bbox = m.user_roi_bounds()
else:
bbox = [-95.3704, 29.6762, -95.368, 29.6775]
Download map tiles¶
Download maps tiles and mosaic them into a single GeoTIFF file
In [ ]:
Copied!
image = 'satellite.tif'
tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')
image = 'satellite.tif'
tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')
In [ ]:
Copied!
# image = '/path/to/your/own/image.tif'
# image = '/path/to/your/own/image.tif'
In [ ]:
Copied!
m.add_raster(image, layer_name='Image')
m
m.add_raster(image, layer_name='Image')
m
Initialize SAM class¶
In [ ]:
Copied!
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
checkpoint = os.path.join(out_dir, 'sam_vit_h_4b8939.pth')
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
checkpoint = os.path.join(out_dir, 'sam_vit_h_4b8939.pth')
In [ ]:
Copied!
device = 'cuda' if torch.cuda.is_available() else 'cpu'
sam = SamGeo(
checkpoint=checkpoint,
model_type='vit_h',
device=device,
erosion_kernel=(3, 3),
mask_multiplier=255,
sam_kwargs=None,
)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
sam = SamGeo(
checkpoint=checkpoint,
model_type='vit_h',
device=device,
erosion_kernel=(3, 3),
mask_multiplier=255,
sam_kwargs=None,
)
Segment the image¶
In [ ]:
Copied!
mask = 'segment.tiff'
sam.generate(image, mask)
mask = 'segment.tiff'
sam.generate(image, mask)
Polygonize the raster data¶
Save the segmentation results as a GeoPackage file.
In [ ]:
Copied!
vector = 'segment.gpkg'
sam.tiff_to_gpkg(mask, vector, simplify_tolerance=None)
vector = 'segment.gpkg'
sam.tiff_to_gpkg(mask, vector, simplify_tolerance=None)
You can also save the segmentation results as any vector data format supported by GeoPandas.
In [ ]:
Copied!
shapefile = 'segment.shp'
sam.tiff_to_vector(mask, shapefile)
shapefile = 'segment.shp'
sam.tiff_to_vector(mask, shapefile)
Visualize the results¶
In [ ]:
Copied!
style = {
'color': '#3388ff',
'weight': 2,
'fillColor': '#7c4185',
'fillOpacity': 0.5,
}
m.add_vector(vector, layer_name='Vector', style=style)
m
style = {
'color': '#3388ff',
'weight': 2,
'fillColor': '#7c4185',
'fillOpacity': 0.5,
}
m.add_vector(vector, layer_name='Vector', style=style)
m
Last update:
2023-04-22
Created: 2023-04-22
Created: 2023-04-22