Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbaer committed Jul 8, 2023
1 parent 1cedac9 commit 717b18e
Showing 1 changed file with 102 additions and 57 deletions.
159 changes: 102 additions & 57 deletions docs/examples/satellite.ipynb
Original file line number Diff line number Diff line change
@@ -3,24 +3,24 @@
{
"cell_type": "markdown",
"metadata": {
"id": "W-IgodFVKrQO"
"id": "RcHucVwtwdFq"
},
"source": [
"# Segment Anything Model for Geospatial Data \n",
"# 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",
"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. "
"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"
"id": "8eIAGxB6wdFv"
},
"source": [
"## Install dependencies\n",
@@ -32,7 +32,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "L42UgbyJ6j8W"
"id": "yQvGXi4jwdFw"
},
"outputs": [],
"source": [
@@ -42,7 +42,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "lNdELYaIlxf5"
"id": "F0iX0ZadwdFx"
},
"source": [
"## Import libraries"
@@ -52,20 +52,19 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "q9PG2KLElxf6"
"id": "3QcJrVzWwdFy"
},
"outputs": [],
"source": [
"import os\n",
"import leafmap\n",
"import torch\n",
"from samgeo import SamGeo, tms_to_geotiff"
"from samgeo import SamGeo, tms_to_geotiff, get_basemaps"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zaed8W5llxf6"
"id": "fHJFkF0SwdFy"
},
"source": [
"## Create an interactive map"
@@ -75,19 +74,19 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YSwcy5ejlxf6"
"id": "pdxKXGCKwdFz"
},
"outputs": [],
"source": [
"m = leafmap.Map(center=[29.676840, -95.369222], zoom=19)\n",
"m.add_basemap('SATELLITE')\n",
"m.add_basemap(\"SATELLITE\")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ATSnXI81lxf7"
"id": "CA0f1HhQwdFz"
},
"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"
@@ -97,7 +96,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fSQdpisklxf7"
"id": "QKoiL_GuwdF0"
},
"outputs": [],
"source": [
@@ -110,7 +109,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "R5Y5PHSKlxf8"
"id": "6qf4n8sbwdF0"
},
"source": [
"## Download map tiles\n",
@@ -122,111 +121,157 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "iymuVrS3lxf8"
"id": "bNW9aBPiwdF1"
},
"outputs": [],
"source": [
"image = 'satellite.tif'\n",
"# image = '/path/to/your/own/image.tif'"
"image = \"satellite.tif\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9iBgzbgqwdF1"
},
"source": [
"Besides the `satellite` basemap, you can use any of the following basemaps returned by the `get_basemaps()` function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jBjqFcLMlxf8"
"id": "uZGOM5s3wdF2"
},
"outputs": [],
"source": [
"tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')"
"# get_basemaps().keys()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HNPRNDX2wdF2"
},
"source": [
"Specify the basemap as the source."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "uAPFplXJlxf9"
"id": "SWj6SzLAwdF3"
},
"outputs": [],
"source": [
"m.add_raster(image, layer_name='Image')\n",
"m"
"tms_to_geotiff(output=image, bbox=bbox, zoom=20, source=\"Satellite\", overwrite=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4kMPlM2Qlxf9"
"id": "CRljvZ6WwdF4"
},
"source": [
"![](https://i.imgur.com/KAm84IY.png)"
"You can also use your own image. Uncomment and run the following cell to use your own image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "AY0HPdsgwdF4"
},
"outputs": [],
"source": [
"# image = '/path/to/your/own/image.tif'"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Jcx82t_alxf9"
"id": "AEmHRdTawdF4"
},
"source": [
"## Initialize SAM class"
"Display the downloaded image on the map."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oY6cRdQRlxf-"
"id": "aK4JAOXYwdF5"
},
"outputs": [],
"source": [
"out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n",
"checkpoint = os.path.join(out_dir, 'sam_vit_h_4b8939.pth')"
"m.layers[-1].visible = False # turn off the basemap\n",
"m.add_raster(image, layer_name=\"Image\")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cPTbNo_dwdF5"
},
"source": [
"![](https://i.imgur.com/KAm84IY.png)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GXhgdFNLwdF5"
},
"source": [
"## Initialize SAM class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "JvQIg74Plxf-"
"id": "LkiMcOHXwdF6"
},
"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",
" model_type=\"vit_h\",\n",
" checkpoint=\"sam_vit_h_4b8939.pth\",\n",
" sam_kwargs=None,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4_Xe0A7ilxf_"
"id": "dFchj9XRwdF6"
},
"source": [
"## Segment the image"
"## Segment the image\n",
"\n",
"Set `batch=True` to segment the image in batches. This is useful for large images that cannot fit in memory."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "JYKzddIolxf_"
"id": "5ey5JBIHwdF6"
},
"outputs": [],
"source": [
"mask = 'segment.tiff'\n",
"sam.generate(image, mask)"
"mask = \"segment.tif\"\n",
"sam.generate(\n",
" image, mask, batch=True, foreground=True, erosion_kernel=(3, 3), mask_multiplier=255\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VxJ5NE-PlxgA"
"id": "NpFJ2fOfwdF7"
},
"source": [
"## Polygonize the raster data\n",
@@ -238,18 +283,18 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Albs9QVxlxgA"
"id": "uKFpl1iJwdF7"
},
"outputs": [],
"source": [
"vector = 'segment.gpkg'\n",
"vector = \"segment.gpkg\"\n",
"sam.tiff_to_gpkg(mask, vector, simplify_tolerance=None)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rM67PwyllxgA"
"id": "Bjmk5zlHwdF7"
},
"source": [
"You can also save the segmentation results as any vector data format supported by GeoPandas."
@@ -259,18 +304,18 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RItf9kBqlxgB"
"id": "judkrLYUwdF8"
},
"outputs": [],
"source": [
"shapefile = 'segment.shp'\n",
"shapefile = \"segment.shp\"\n",
"sam.tiff_to_vector(mask, shapefile)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8luRDPHSlxgB"
"id": "kmnSZFKiwdF8"
},
"source": [
"## Visualize the results"
@@ -280,24 +325,24 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FTFke8_rlxgB"
"id": "nOskR8IxwdF9"
},
"outputs": [],
"source": [
"style = {\n",
" 'color': '#3388ff',\n",
" 'weight': 2,\n",
" 'fillColor': '#7c4185',\n",
" 'fillOpacity': 0.5,\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.add_vector(vector, layer_name=\"Vector\", style=style)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3nT4s1IglxgC"
"id": "xl0340RZwdF9"
},
"source": [
"![](https://i.imgur.com/Ysq3u7E.png)"

0 comments on commit 717b18e

Please sign in to comment.