{"cells":[{"cell_type":"markdown","metadata":{},"source":["## Clasificación de cobertura del suelo a partir de imágenes Sentinel-2 y con un modelo de Deep Learning pre-entrenado\n"]},{"cell_type":"markdown","metadata":{},"source":["## Carga de librerías necesarias"]},{"cell_type":"code","execution_count":1,"metadata":{"trusted":true},"outputs":[],"source":["import arcgis\n","from arcgis.gis import GIS\n","from arcgis.learn import classify_pixels\n","from arcgis.raster.functions import equal_to\n","from arcgis.raster.functions import extract_band\n","\n","\n","import pandas as pd\n","from datetime import datetime as dt\n","from ipywidgets import HBox, VBox, Label, Layout"]},{"cell_type":"markdown","metadata":{},"source":["Conexión a la organización de ArcGIS Online"]},{"cell_type":"code","execution_count":2,"metadata":{"trusted":true},"outputs":[{"name":"stderr","output_type":"stream","text":["You are logged on as liber.preventas with an administrator role, proceed with caution.\n"]}],"source":["from arcgis.gis import GIS\n","gis = GIS(\"home\")"]},{"cell_type":"markdown","metadata":{},"source":["### Acceso a la capa de datos alojada"]},{"cell_type":"code","execution_count":3,"metadata":{"trusted":true},"outputs":[{"data":{"text/html":["
\n","
\n"," \n"," \n"," \n","
\n","\n","
\n"," Pirineos_05_01_2023\n"," \n","
Imagery Layer by liber.preventas\n","
Last Modified: September 11, 2023\n","
0 comments, 6 views\n","
\n","
\n"," "],"text/plain":[""]},"execution_count":3,"metadata":{},"output_type":"execute_result"}],"source":["imagery = gis.content.get(\"e798a7289419489481856e0c362e4625\")\n","imagery"]},{"cell_type":"markdown","metadata":{},"source":["### Acceso al modelo del Living Atlas"]},{"cell_type":"code","execution_count":4,"metadata":{"trusted":true},"outputs":[{"data":{"text/html":["
\n","
\n"," \n"," \n"," \n","
\n","\n","
\n"," Land Cover Classification (Sentinel-2)\n"," \n","
Deep learning model to perform land cover classification on Sentinel-2 imagery.Deep Learning Package by esri_analytics\n","
Last Modified: January 12, 2024\n","
40 comments, 44223 views\n","
\n","
\n"," "],"text/plain":[""]},"execution_count":4,"metadata":{},"output_type":"execute_result"}],"source":["model = gis.content.get(\"afd124844ba84da69c2c533d4af10a58\")\n","model"]},{"cell_type":"markdown","metadata":{},"source":["### Previsualización de la capa en mapa"]},{"cell_type":"code","execution_count":7,"metadata":{"trusted":true},"outputs":[],"source":["map1 = gis.map()"]},{"cell_type":"code","execution_count":8,"metadata":{"trusted":true},"outputs":[{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"ef25ee5104984e2dacf34748ac4e5c21","version_major":2,"version_minor":0},"text/plain":["MapView(layout=Layout(height='400px', width='100%'))"]},"metadata":{},"output_type":"display_data"},{"data":{"text/html":["
"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/html":["
"],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map1.add_layer(imagery)\n","map1"]},{"cell_type":"markdown","metadata":{},"source":["#### Obtenemos la extensión del mapa sobre la que se aplicará el modelo\n","Podemos hacer zoom sobre el widget de mapa para definir mejor sobre qué parte de la imagen se ejecutará la clasificación de píxeles"]},{"cell_type":"code","execution_count":9,"metadata":{"trusted":true},"outputs":[{"data":{"text/plain":["{'spatialReference': {'latestWkid': 3857, 'wkid': 102100},\n"," 'xmin': 92163.25988270156,\n"," 'ymin': 5226056.078771058,\n"," 'xmax': 195506.1221241206,\n"," 'ymax': 5256630.890085087}"]},"execution_count":9,"metadata":{},"output_type":"execute_result"}],"source":["ext = map1.extent\n","ext"]},{"cell_type":"markdown","metadata":{},"source":["### Clasificación de píxeles\n","Se ejecuta la [clasificación de píxeles](https://developers.arcgis.com/python/api-reference/arcgis.learn.toc.html#classify_pixels) sobre la **capa** en cuestión pasándole el modelo como parámetro. \n"]},{"cell_type":"code","execution_count":10,"metadata":{"trusted":true},"outputs":[],"source":["classified_pirineo = classify_pixels(\n"," input_raster=imagery.layers[0],\n"," model=model,\n"," model_arguments={\n"," \"padding\": 56,\n"," \"batch_size\": 64,\n"," \"predict_background\": True,\n"," \"tile_size\": 224,\n"," \"test_time_augmentation\": True,\n"," \"merge_classes\": True,\n"," \"sentinel_imagery_level\": 1,\n"," },\n"," output_name=\"classified_output_2016\" + str(dt.now().microsecond),\n"," context={\"extent\": ext, \"processorType\": \"GPU\", \"cellSize\": 10},\n"," tiles_only=False,\n",")"]},{"cell_type":"markdown","metadata":{"trusted":true},"source":["> Ha tardado unos 18 min en ejecutarse en un ArcGIS Notebook Advanced con GPU"]},{"cell_type":"code","execution_count":11,"metadata":{"trusted":true},"outputs":[{"data":{"text/html":["
\n","
\n"," \n"," \n"," \n","
\n","\n","
\n"," classified_output_2016350506\n"," \n","
Analysis Image Service generated from ClassifyPixelsUsingDeepLearningImagery Layer by liber.preventas\n","
Last Modified: February 01, 2024\n","
0 comments, 3 views\n","
\n","
\n"," "],"text/plain":[""]},"execution_count":11,"metadata":{},"output_type":"execute_result"}],"source":["classified_pirineo"]},{"cell_type":"markdown","metadata":{},"source":["### Visualización del resultado"]},{"cell_type":"code","execution_count":12,"metadata":{"trusted":true},"outputs":[],"source":["map4 = gis.map()\n","map4.add_layer(classified_pirineo)"]},{"cell_type":"code","execution_count":13,"metadata":{"trusted":true},"outputs":[{"data":{"application/vnd.jupyter.widget-view+json":{"model_id":"d84412bad8c14b1d99779e9e3977fad9","version_major":2,"version_minor":0},"text/plain":["MapView(layout=Layout(height='400px', width='100%'))"]},"metadata":{},"output_type":"display_data"},{"data":{"text/html":["
"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/html":["
"],"text/plain":[""]},"metadata":{},"output_type":"display_data"}],"source":["map4"]},{"cell_type":"code","execution_count":null,"metadata":{"trusted":true},"outputs":[],"source":[]}],"metadata":{"esriNotebookRuntime":{"notebookRuntimeName":"ArcGIS Notebook Python 3 Advanced with GPU support","notebookRuntimeVersion":"9.0"},"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","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.18"}},"nbformat":4,"nbformat_minor":2}