Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbaer committed Apr 21, 2023
1 parent 83cb6d3 commit e3460ba
Showing 1 changed file with 332 additions and 0 deletions.
332 changes: 332 additions & 0 deletions docs/examples/satellite.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "W-IgodFVKrQO"
},
"source": [
"# Segment Anything Model for Geospatial Data \n",
"\n",
"[![image](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)\n",
"[![image](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/opengeos/segment-geospatial&urlpath=lab/tree/segment-geospatial/docs/examples/satellite.ipynb&branch=main)\n",
"[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)\n",
"\n",
"This notebook shows how to use segment satellite imagery using the Segment Anything Model (SAM) with a few lines of code. \n",
"\n",
"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. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KyZq008jL64p"
},
"source": [
"## Install dependencies\n",
"\n",
"Uncomment and run the following cell to install the required dependencies.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "L42UgbyJ6j8W"
},
"outputs": [],
"source": [
"# %pip install segment-geospatial leafmap localtileserver"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lNdELYaIlxf5"
},
"source": [
"## Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "q9PG2KLElxf6"
},
"outputs": [],
"source": [
"import os\n",
"import leafmap\n",
"import torch\n",
"from samgeo import SamGeo, tms_to_geotiff"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zaed8W5llxf6"
},
"source": [
"## Create an interactive map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YSwcy5ejlxf6"
},
"outputs": [],
"source": [
"m = leafmap.Map(center=[29.676840, -95.369222], zoom=19)\n",
"m.add_basemap('SATELLITE')\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ATSnXI81lxf7"
},
"source": [
"Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fSQdpisklxf7"
},
"outputs": [],
"source": [
"if m.user_roi_bounds() is not None:\n",
" bbox = m.user_roi_bounds()\n",
"else:\n",
" bbox = [-95.3704, 29.6762, -95.368, 29.6775]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "R5Y5PHSKlxf8"
},
"source": [
"## Download map tiles\n",
"\n",
"Download maps tiles and mosaic them into a single GeoTIFF file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "iymuVrS3lxf8"
},
"outputs": [],
"source": [
"image = 'satellite.tif'\n",
"# image = '/path/to/your/own/image.tif'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jBjqFcLMlxf8"
},
"outputs": [],
"source": [
"tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "uAPFplXJlxf9"
},
"outputs": [],
"source": [
"m.add_raster(image, layer_name='Image')\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4kMPlM2Qlxf9"
},
"source": [
"![](https://i.imgur.com/KAm84IY.png)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Jcx82t_alxf9"
},
"source": [
"## Initialize SAM class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oY6cRdQRlxf-"
},
"outputs": [],
"source": [
"out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n",
"checkpoint = os.path.join(out_dir, 'sam_vit_h_4b8939.pth')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "JvQIg74Plxf-"
},
"outputs": [],
"source": [
"device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
"sam = SamGeo(\n",
" checkpoint=checkpoint,\n",
" model_type='vit_h',\n",
" device=device,\n",
" erosion_kernel=(3, 3),\n",
" mask_multiplier=255,\n",
" sam_kwargs=None,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4_Xe0A7ilxf_"
},
"source": [
"## Segment the image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "JYKzddIolxf_"
},
"outputs": [],
"source": [
"mask = 'segment.tiff'\n",
"sam.generate(image, mask)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VxJ5NE-PlxgA"
},
"source": [
"## Polygonize the raster data\n",
"\n",
"Save the segmentation results as a GeoPackage file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Albs9QVxlxgA"
},
"outputs": [],
"source": [
"vector = 'segment.gpkg'\n",
"sam.tiff_to_gpkg(mask, vector, simplify_tolerance=None)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rM67PwyllxgA"
},
"source": [
"You can also save the segmentation results as any vector data format supported by GeoPandas."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RItf9kBqlxgB"
},
"outputs": [],
"source": [
"shapefile = 'segment.shp'\n",
"sam.tiff_to_vector(mask, shapefile)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8luRDPHSlxgB"
},
"source": [
"## Visualize the results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FTFke8_rlxgB"
},
"outputs": [],
"source": [
"style = {\n",
" 'color': '#3388ff',\n",
" 'weight': 2,\n",
" 'fillColor': '#7c4185',\n",
" 'fillOpacity': 0.5,\n",
"}\n",
"m.add_vector(vector, layer_name='Vector', style=style)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3nT4s1IglxgC"
},
"source": [
"![](https://i.imgur.com/Ysq3u7E.png)"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.16"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit e3460ba

Please sign in to comment.