|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": { |
| 7 | + "id": "W-IgodFVKrQO" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "# Segment Anything Model for Geospatial Data \n", |
| 11 | + "\n", |
| 12 | + "[](https://studiolab.sagemaker.aws/import/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)\n", |
| 13 | + "[](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", |
| 14 | + "[](https://colab.research.google.com/github/opengeos/segment-geospatial/blob/main/docs/examples/satellite.ipynb)\n", |
| 15 | + "\n", |
| 16 | + "This notebook shows how to use segment satellite imagery using the Segment Anything Model (SAM) with a few lines of code. " |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "attachments": {}, |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": { |
| 23 | + "id": "KyZq008jL64p" |
| 24 | + }, |
| 25 | + "source": [ |
| 26 | + "## Install dependencies\n", |
| 27 | + "\n", |
| 28 | + "Uncomment and run the following cell to install the required dependencies.\n" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": { |
| 35 | + "colab": { |
| 36 | + "base_uri": "https://localhost:8080/" |
| 37 | + }, |
| 38 | + "id": "L42UgbyJ6j8W", |
| 39 | + "outputId": "6924c5c8-39a0-4ac6-f114-9f1f8d102e88" |
| 40 | + }, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "# %pip install segment-geospatial leafmap localtileserver" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "attachments": {}, |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "## Import libraries" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "import os\n", |
| 61 | + "import leafmap\n", |
| 62 | + "import torch\n", |
| 63 | + "from samgeo import SamGeo, tms_to_geotiff" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "attachments": {}, |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": {}, |
| 70 | + "source": [ |
| 71 | + "## Create an interactive map" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": null, |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "m = leafmap.Map(center=[29.676840, -95.369222], zoom=19)\n", |
| 81 | + "m.add_basemap('SATELLITE')\n", |
| 82 | + "m" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "attachments": {}, |
| 87 | + "cell_type": "markdown", |
| 88 | + "metadata": {}, |
| 89 | + "source": [ |
| 90 | + "Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": null, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "if m.user_roi_bounds() is not None:\n", |
| 100 | + " bbox = m.user_roi_bounds()\n", |
| 101 | + "else:\n", |
| 102 | + " bbox = [-95.3704, 29.6762, -95.368, 29.6775]" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "attachments": {}, |
| 107 | + "cell_type": "markdown", |
| 108 | + "metadata": {}, |
| 109 | + "source": [ |
| 110 | + "## Download map tiles\n", |
| 111 | + "\n", |
| 112 | + "Download maps tiles and mosaic them into a single GeoTIFF file" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [ |
| 121 | + "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n", |
| 122 | + "if not os.path.exists(out_dir):\n", |
| 123 | + " os.makedirs(out_dir)" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "image = os.path.join(out_dir, 'satellite.tif')\n", |
| 133 | + "# image = '/path/to/your/own/image.tif'" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": null, |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": null, |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [], |
| 150 | + "source": [ |
| 151 | + "m.add_raster(image, layer_name='Image')\n", |
| 152 | + "m" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "attachments": {}, |
| 157 | + "cell_type": "markdown", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "attachments": {}, |
| 165 | + "cell_type": "markdown", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "## Initialize SAM class" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "code", |
| 173 | + "execution_count": null, |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [], |
| 176 | + "source": [ |
| 177 | + "checkpoint = os.path.join(out_dir, 'sam_vit_h_4b8939.pth')\n", |
| 178 | + "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n", |
| 179 | + "sam = SamGeo(checkpoint=checkpoint,\n", |
| 180 | + " model_type='vit_h',\n", |
| 181 | + " device=device,\n", |
| 182 | + " erosion_kernel=(3, 3),\n", |
| 183 | + " mask_multiplier=255,\n", |
| 184 | + " sam_kwargs=None)" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "attachments": {}, |
| 189 | + "cell_type": "markdown", |
| 190 | + "metadata": {}, |
| 191 | + "source": [ |
| 192 | + "## Segment the image" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "code", |
| 197 | + "execution_count": null, |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [], |
| 200 | + "source": [ |
| 201 | + "mask = 'segment.tiff'\n", |
| 202 | + "sam.generate(image, mask)" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "attachments": {}, |
| 207 | + "cell_type": "markdown", |
| 208 | + "metadata": {}, |
| 209 | + "source": [ |
| 210 | + "## Polygonize the raster data" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": null, |
| 216 | + "metadata": {}, |
| 217 | + "outputs": [], |
| 218 | + "source": [ |
| 219 | + "vector = os.path.join(out_dir, 'segment.gpkg')\n", |
| 220 | + "sam.tiff_to_gpkg(mask, vector, simplify_tolerance=None)" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "attachments": {}, |
| 225 | + "cell_type": "markdown", |
| 226 | + "metadata": {}, |
| 227 | + "source": [ |
| 228 | + "## Visualize the results" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": null, |
| 234 | + "metadata": {}, |
| 235 | + "outputs": [], |
| 236 | + "source": [ |
| 237 | + "style = {\n", |
| 238 | + " 'color': '#3388ff',\n", |
| 239 | + " 'weight': 2,\n", |
| 240 | + " 'fillColor': '#7c4185',\n", |
| 241 | + " 'fillOpacity': 0.5,\n", |
| 242 | + "}\n", |
| 243 | + "m.add_vector(vector, layer_name='Vector', style=style)\n", |
| 244 | + "m" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "attachments": {}, |
| 249 | + "cell_type": "markdown", |
| 250 | + "metadata": {}, |
| 251 | + "source": [ |
| 252 | + "" |
| 253 | + ] |
| 254 | + } |
| 255 | + ], |
| 256 | + "metadata": { |
| 257 | + "accelerator": "GPU", |
| 258 | + "colab": { |
| 259 | + "provenance": [] |
| 260 | + }, |
| 261 | + "gpuClass": "standard", |
| 262 | + "kernelspec": { |
| 263 | + "display_name": "Python 3", |
| 264 | + "name": "python3" |
| 265 | + }, |
| 266 | + "language_info": { |
| 267 | + "codemirror_mode": { |
| 268 | + "name": "ipython", |
| 269 | + "version": 3 |
| 270 | + }, |
| 271 | + "file_extension": ".py", |
| 272 | + "mimetype": "text/x-python", |
| 273 | + "name": "python", |
| 274 | + "nbconvert_exporter": "python", |
| 275 | + "pygments_lexer": "ipython3", |
| 276 | + "version": "3.9.16" |
| 277 | + } |
| 278 | + }, |
| 279 | + "nbformat": 4, |
| 280 | + "nbformat_minor": 0 |
| 281 | +} |
0 commit comments