ArcGIS Developers
Dashboard

ArcGIS API for Python

Coastline classification using Feature Classifier

  • 🔬 Data Science
  • 🥠 Deep Learning and Object classification

Introduction

We have already seen how we can extract coastlines using Landsat-8 multispectral imagery and band ratio technique, and next, we will classify these coastlines into multiple categories. To achieve this, we can train a model that can classify a coastline as one of the different categories shown in the screenshot below:


Figure 1. Coastline categories

In this sample notebook, we will see how we can classify these coastlines in the categories mentioned in figure 1, by training a Feature Classifier model.

Necessary imports

In [1]:
import os
import glob
import zipfile
from pathlib import Path

from arcgis.gis import GIS
from arcgis.learn import prepare_data, FeatureClassifier

Connect to your GIS

In [2]:
# Connect to GIS
gis = GIS("home")

Export training data

Using ArcGIS Maritime, we imported NOAA’s Electronic Navigational Charts. The maritime data in these charts contain the Coastline Feature class with the Category of Coastline details. The Sentinel 2 imagery was downloaded from the Copernicus Open Access Hub.

Before exporting the data, we will first create grids along the coastlines that can act as a feature class while exporting the data. For this, we will use the Generate Rectangles Along Lines tool. The parameters required to run the function are:

  • Input Line Features: Coastlines (belonging to each category)
  • Output Feature Class: Output Feature class name
  • Length Along the Line: 1000
  • Length Perpendicular to the Line: 1000
  • Spatial Sort Method: Upper left

Figure 3 shows the output from the tool when run on a feature class belonging to category 3 (sandy shore).

Next, we add a category column to the feature class that can act as the class value field while exporting the data. We created a category column of the long data type named CATCOA. In this column, we set the value to be the same as the feature's coastline category using the Add and Calculate options respectively, as shown in figure 4.


Figure 4. Feature class after added category column

We then performed a similar process for each type of coastline category. Next, we exported this data in the "Labeled Tiles" metadata format for a small extent with multiple categories, using the Export Training Data For Deep Learning tool. This Export Training Data For Deep Learning tool is available in ArcGIS Pro, as well as ArcGIS Image Server.

  • Input Raster: Sentinel2 imagery
  • Input Feature class Or Classified Raster: Feature class as shown in figure 4.
  • Tile Size X & Tile Size Y: 256
  • Stride X & Stride Y: 128
  • Meta Data Format: 'Labeled Tiles' as we are training a Feature Classifier model.
  • Environments: Set optimum Cell Size, Processing Extent.


Figure 5. Export Training Data for Deep Learning tool
with arcpy.EnvManager(extent="MINOF", cellSize=10):
    arcpy.ia.ExportTrainingDataForDeepLearning("Multispectral_MTD_MSIL1C", r"D:\Coastline category\Data\Category_1", "CoastlineL_Clip_category1_Rectangles", "TIFF", 256, 256, 128, 128, "ONLY_TILES_WITH_FEATURES", "Labeled_Tiles", 0, "CATCOA", 0, None, 0, "MAP_SPACE", "PROCESS_AS_MOSAICKED_IMAGE", "NO_BLACKEN", "FIXED_SIZE")

We also created separate folders for each category to demonstrate the recently added multi-folder training support. Alternatively, you can choose the same folder each time you export the data, resulting in the newly exported images being amended in the existing folder.

We have also provided a subset of training data exported from each category. You can use this data directly to run these experiments.

In [3]:
training_data = gis.content.get('9251417cb9ab4a059eb538282f82883c')
training_data
Out[3]:
coastline_classification_using_feature_classifier
Image Collection by api_data_owner
Last Modified: May 21, 2021
0 comments, 0 views


In [4]:
filepath = training_data.download(file_name=training_data.name)
In [5]:
with zipfile.ZipFile(filepath, 'r') as zip_ref:
    zip_ref.extractall(Path(filepath).parent)
In [6]:
output_path = os.path.join(os.path.splitext(filepath)[0], "*")
In [7]:
output_path = glob.glob(output_path)

Train the model

arcgis.learn provides the ability to determine the class of each feature in the form of a FeatureClassifier model. To learn more about it's workings and use cases, see this guide - "How feature classifier works?".

Prepare data

Here, we will specify the path to our training data and a few hyperparameters.

  • path: path of the folder/list of folders containing training data.
  • batch_size: Number of images your model will train on each step inside an epoch, it directly depends on the memory of your graphic card. 128 worked for us on a 32GB GPU.
In [ ]:
# output_path = [ r'D:\Coastline_category\Data_generate_rectangles\Category_1',
#            r'D:\Coastline_category\Data_generate_rectangles\Category_2',
#            r'D:\Coastline_category\Data_generate_rectangles\Category_3',
#            r'D:\Coastline_category\Data_generate_rectangles\Category_4',
#            r'D:\Coastline_category\Data_generate_rectangles\Category_6',
#            r'D:\Coastline_category\Data_generate_rectangles\Category_7',
#            r'D:\Coastline_category\Data_generate_rectangles\Category_8',
#            r'D:\Coastline_category\Data_generate_rectangles\Category_10']
In [7]:
data = prepare_data(
    path=output_path,
    batch_size=128,
    val_split_pct=0.2
)

Visualize training data

To get a sense of what the training data looks like, the arcgis.learn.show_batch() method randomly picks a few training chips and visualizes them.

  • rows: Number of rows to visualize
In [8]:
data.show_batch(rows=5)