- 🔬 Data Science
- 🥠 Deep Learning and edge detection
High-resolution remote sensing images provide useful spatial information for plot delineation; however, manual processing is time-consuming. Automatically extracting visible cadastral boundaries combined with (legal) adjudication and incorporation of local knowledge from human operators offers the potential to improve current cadastral mapping approaches in terms of time, cost, accuracy, and acceptance.
This sample shows how
ArcGIS API for Python can be used to train a deep learning edge detection model to extract parcels from satellite imagery and thus more efficient approaches for cadastral mapping.
In this workflow we will basically have three steps.
- Export training data
- Train a model
- Deploy model and extract land parcels
import os, zipfile from pathlib import Path import arcgis from arcgis import GIS from arcgis.learn import BDCNEdgeDetector, HEDEdgeDetector, prepare_data
Connect to your GIS
gis = GIS("home") ent_gis = GIS('https://pythonapi.playground.esri.com/portal')
Export land parcel boundaries data
Training data can be exported by using the
Export Training Data For Deep Learning tool available in ArcGIS Pro as well as ArcGIS Image Server. The training data consisted of polyline buffered feature class with a 'class' attribute. For this example, we prepared training data in
Classified Tiles format using a
chip_size of 400px and
cell_size of 20cm in ArcGIS Pro.
Input Raster: Esri World Imagery
Input Feature Class or Classified Raster: buffered polyline with class attribute
Class Value Field: field in the attributes containing class
Tile Size X & Tile Size Y: 400
Stride X & Stride Y: 128
Reference System: Map space
Meta Data Format: Classified tiles
Environments: Set optimum Cell Size, Processing Extent
Raster and parcels data used for exporting the training dataset are provided below
training_area_raster = ent_gis.content.get('8202ffe4fcaf4ba9bfefe2154f98e7b8') training_area_raster
parcel_training_polygon = gis.content.get('ac0e639d6e9b43328605683efb37ff56') parcel_training_polygon
arcpy.ia.ExportTrainingDataForDeepLearning("Imagery", r"C:\sample\Data\Training Data 400px 20cm", "land_parcels_training_buffered.shp", "TIFF", 400, 400, 128, 128, "ONLY_TILES_WITH_FEATURES", "Classified_Tiles", 0, None, 0, 0, "MAP_SPACE", "NO_BLACKEN", "Fixed_Size")
This will create all the necessary files needed for the next step in the 'Output Folder', and we will now call it our training data.
Alternatively, we have provided a subset of training data containing a samples below and the parcel training polygon used for exporting data. You can use the data directly to run the experiments.
training_data = gis.content.get('ab003694c99f4484a2d30df53f8b4d03') training_data
filepath = training_data.download(file_name=training_data.name)
import zipfile with zipfile.ZipFile(filepath, 'r') as zip_ref: zip_ref.extractall(Path(filepath).parent)
data_path = Path(os.path.join(os.path.splitext(filepath)))
We would specify the path to our training data and a few parameters.
path: path of folder containing training data.
chip_size: Same as per specified while exporting training data
batch_size: No of images your model will train on each step inside an epoch, it directly depends on the memory of your graphic card.
data = prepare_data(data_path, batch_size=2)
Visualize a few samples from your training data
The code below shows a few samples of our data with the same symbology as in ArcGIS Pro.
rows: No of rows we want to see the results for.
alpha: controls the opacity of labels(Classified imagery) over the drone imagery
Part 1 - Model training
Load BDCN or HED edge detector model architecture
There are two available edge detection models in
BDCNEdgeDetector. If backbone is not specified, by default this model will be loaded on a pretrained ResNet type backbone.
#model = HEDEdgeDetector(data, backbone="vgg19") #or model = BDCNEdgeDetector(data, backbone="vgg19")
List of supported backbones, that could be used during training.
['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19', 'vgg19_bn']
Tuning for optimal learning rate
Optimization in deep learning is all about tuning 'hyperparameters'. In this step, we will find an 'optimum learning rate' for our model on the training data. Learning rate is a very important parameter, while training our model it will see the training data several times and adjust itself (the weights of the network). Too high learning rate will lead to the convergence of our model to a suboptimal solution and too low learning can slow down the convergence of our model. We can use the lr_find() method to find an optimum learning rate at which can train a robust model fast enough.
# Find Learning Rate lr = model.lr_find()
Fit the model on the data
To start with let us first train the model for 30 epochs. One epoch means the model will see the complete training set once and so on. If you feel the results are not satisfactory we can train it further for more number of epochs.
The code below will pick a few random samples and show us ground truth and respective model predictions side by side. This allows us to preview the results of your model in the notebook itself, once satisfied we can save the model and use it further in our workflow.
We have few parameters for visualization.
alpha: controls the opacity of predicted edges. Set to 1.
thinning: Its a post-processsing parameters, which thins or skeletonizes the predicted edges. We will be using our own pre-processing workflow build in ArcGIS pro hence, will set it to False. As per results, this could also be set to True/False during inferencing in pro.
Save the model
We would now save the model which we just trained as a
Deep Learning Package or
.dlpk format. Deep Learning package is the standard format used to deploy deep learning models on the ArcGIS platform. For this sample, we will be using this model in ArcGIS Pro to extract land parcels.
We will use the
save() method to save the model and by default, it will be saved to a folder 'models' inside our training data folder.
We can observe some room for further improvement, so we trained the model for further 70 epochs. The deployment and results from the model trained for 100 epochs are shown upcoming section.
Part 2 - Deploying model, and extraction of parcels in imagery at scale
The deep learning package saved in the previous step can be used to extract classified raster using the
Classify Pixels Using Deep Learning tool. Further, the classified raster is regularised and finally converted to a vector Polygon layer. The post-processing steps use advanced ArcGIS geoprocessing tools to remove unwanted artifacts in the output.
As the model has been trained on a limited amount of data, it is expected to only work well in nearby areas and similar geographies.
Generate a classified raster using classify pixels using deep learning tool
We will use the saved model to detect objects using the
Classify pixels Using Deep Learning tool available in ArcGIS Pro as well as ArcGIS Image Server. For this sample, we will use world imagery to delineate boundaries.
Raster used for testing the model is provided below
test_area_raster = ent_gis.content.get('481f2ba520274f0fabfe470d2c8def39') test_area_raster
out_classified_raster = arcpy.ia.ClassifyPixelsUsingDeepLearning("Imagery", r"C:\Esri_project\edge_detection_ready_to_use_model\models\edge_model_e30\edge_model_e100.emd", "padding 56;batch_size 4;thinning False"); out_classified_raster.save(r"C:\sample\sample.gdb\edge_detected")
Output of this tool will be in form of a 'classified raster' containing both background and edges. For better visualization, the predicted raster is provided as a web map https://pythonapi.playground.esri.com/portal/home/webmap/viewer.html?webmap=35560c83171f4742847cdeffb813bad9&extent=-77.5235,39.0508,-77.4729,39.0712
A subset of predictions by trained model
Post-processing workflow using modelbuilder
As postprocessing workflow below involves geoprocessing tools and the parameters set in these tools need to be experimented with to get optimum results. We would use model builder to build this workflow for us and which will enable us to iteratively change the parameters in this workflow. The
Input raster should be a predicted reclassified raster with 0-255 range pixel value.
Tools used and important parameters:
Input raster: Predicted reclassified raster with 0-255 range pixel value. where 0:background and 255:edge.
Expand: Expands specified zones of a raster by a specified number of cells. default value set for number of cells to 3.
Raster to polygon: Vectorizes raster to polygon based on cell values, we will keep the 'Simplify Polygons' and 'Create Multipart Features' options unchecked.
Select by attribute: Adds, updates, or removes a selection based on an attribute query. We try to filter out polygons with smaller area.
Simplify polygon: Simplifies polygon outlines by removing relatively extraneous vertices while preserving essential shape. Simplification tolerance was set to 0.1.
Eliminate polygon parts: Creates a new output feature class containing the features from the input polygons with some parts or holes of a specified size deleted. 'Condition' was set to 'area'.
Polygon to centerline: Creates centerlines from polygon features.
Simplify line: Simplifies lines by removing relatively extraneous vertices while preserving essential shape. Simplification tolerance was set to 4.
Trim line: Removes portions of a line that extend a specified distance past a line intersection (dangles). Dangle length was set to 100 and delete short features.
Feature to polygon: Creates a feature class containing polygons generated from areas enclosed by input line or polygon features.
Regularize building footprints: This tool will produce the final finished results by shaping them to how actual buildings look. 'Method' for regularisation would be 'Right Angles and Diagonal' and the parameters 'tolerance', 'Densification' and 'Precision' are '1.5', '1', '0.25' and '2' respectively.
Simplify building: Simplifies the boundary or footprint of building polygons while maintaining their essential shape and size.
Attached below is the toolbox:
Post-processing workflow in model builder
The final output will be in form of a feature class. A link to the web map is provided for better visualization of extracted parcels. Provided is a map for better visualization https://arcg.is/1u4LSX0.
A few subsets of results overlayed on the imagery
 He, Jianzhong, Shiliang Zhang, Ming Yang, Yanhu Shan, and Tiejun Huang. "Bi-directional cascade network for perceptual edge detection.", 2019; [https://arxiv.org/abs/1902.10903 arXiv:1902.10903v1].