Land Parcel Extraction using Edge Detection model

  • 🔬 Data Science
  • 🥠 Deep Learning and edge detection

Introduction

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

Necessary imports

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
train_area_edgedetection_tif
Training area raster for exporting training dataImagery Layer by api_data_owner
Last Modified: January 15, 2021
0 comments, 1 views
parcel_training_polygon = gis.content.get('ac0e639d6e9b43328605683efb37ff56')
parcel_training_polygon
Parcels training data edge detection model
land parcels feature class data for edge detection modelFeature Layer Collection by api_data_owner
Last Modified: December 17, 2020
0 comments, 88 views

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.

Prepare 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
data_for_edge_detection_model
Data for edge detection modelImage Collection by api_data_owner
Last Modified: December 11, 2020
0 comments, 0 views
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)[0]))

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
data.show_batch(alpha=1)
<Figure size 576x576 with 4 Axes>

Part 1 - Model training

Load BDCN or HED edge detector model architecture

There are two available edge detection models in arcgis.learn, the HEDEdgeDetector and 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.

model.supported_backbones
['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()
<Figure size 432x288 with 1 Axes>
lr
0.007585775750291836

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.

model.fit(epochs=30, lr=lr)
epochtrain_lossvalid_lossaccuracyf1_scoredicetime
042379.25000042792.6054690.6350950.2210391.00000004:13
141170.57812540688.0664060.7028770.3390281.00000004:03
242477.42968839820.0546880.6226710.3646111.00000004:05
339409.55468837903.3046880.6637750.4250701.00000004:01
438365.43359437327.0312500.7045670.4613801.00000004:02
538277.03125036422.8203120.7746110.5197721.00000004:04
636631.90234435825.1328120.7335760.5122091.00000004:08
736634.30859435847.4062500.6926770.5033121.00000004:06
837351.02343835534.5781250.6896830.5109921.00000004:02
936165.35546934997.8554690.7384070.5007071.00000004:02
1033797.58203133773.2109380.7702780.5652181.00000004:01
1137077.93750034007.2265620.7297610.5449281.00000004:05
1233959.00781232739.1445310.7673530.5854211.00000004:08
1334267.72265632678.5820310.7696390.5777341.00000004:09
1434710.43750031665.9238280.7829000.6383681.00000004:09
1533882.78906231908.8164060.7889110.6135271.00000004:07
1633524.59375031781.9980470.8156800.6477561.00000004:10
1733885.95703131086.3203120.7814840.6350301.00000004:08
1833582.88281231189.7539060.7958340.6466551.00000004:08
1934172.04687531026.6796880.7815170.6318221.00000004:07
2032889.68359430470.0859380.7934560.6552041.00000004:05
2134317.04687530454.8281250.7882850.6505111.00000004:07
2234042.18359430179.6562500.7954860.6587111.00000004:06
2333061.48437529962.7246090.7964480.6636861.00000004:11
2431341.30859429764.1757810.7993470.6751031.00000004:04
2530202.38476629713.9589840.8046430.6752001.00000003:59
2631560.05664129654.1992190.8010090.6733631.00000004:01
2731049.18359429626.9980470.8043440.6737911.00000004:02
2832527.28710929617.6582030.8007320.6723781.00000004:02
2930877.29492229602.9238280.8009520.6728011.00000004:01

Plot losses

model.plot_losses()
<Figure size 432x288 with 1 Axes>

Visualize results

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.
model.show_results(alpha=1,thinning=False)
<Figure size 576x576 with 4 Axes>

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.

model.save("edge_model_e30")

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
test_raster_tif
test raster for edge detection modelImagery Layer by api_data_owner
Last Modified: January 18, 2021
0 comments, 0 views

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:

gis.content.get('9c8939622d954528916e0d6ffda4065d')
Geoprocessing tool for post-processing of results from edge detection deep learning model
Geoprocessing tool for post-processing of results from edge detection deep learning modelGeoprocessing Sample by api_data_owner
Last Modified: January 05, 2021
0 comments, 0 views

Post-processing workflow in model builder

Final output

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.

gis.content.get('f79515de95254eda83a0d3bc331bdcb7')
Extracted land parcels
Extracted land parcels using BDCN edge detection modelFeature Layer Collection by api_data_owner
Last Modified: January 11, 2021
0 comments, 3 views

A few subsets of results overlayed on the imagery

References

[1] 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].

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.