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

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

# 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.

training_data = gis.content.get('9251417cb9ab4a059eb538282f82883c')
training_data
coastline_classification_using_feature_classifier
Image Collection by api_data_owner
Last Modified: May 21, 2021
0 comments, 0 views
filepath = training_data.download(file_name=training_data.name)
with zipfile.ZipFile(filepath, 'r') as zip_ref:
    zip_ref.extractall(Path(filepath).parent)
output_path = os.path.join(os.path.splitext(filepath)[0], "*")
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.
# 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']
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
data.show_batch(rows=5)
<Figure size 1440x1440 with 25 Axes>

Load model architecture

model = FeatureClassifier(data, oversample=True)

Find an optimal learning rate

Learning rate is one of the most important hyperparameters in model training. ArcGIS API for Python provides a learning rate finder that automatically chooses the optimal learning rate for you.

lr = model.lr_find()
<Figure size 432x288 with 1 Axes>

Fit the model

We will train the model for a few epochs with the learning rate we have found. For the sake of time, we can start with 20 epochs.

model.fit(20, lr=lr)
epochtrain_lossvalid_lossaccuracytime
01.5664780.9478360.68830208:48
10.7982080.6077870.79904708:53
20.4917630.4471510.85258508:46
30.3285220.3603770.86872008:50
40.2407040.3456370.88118808:46
50.1942540.2857880.90209008:44
60.1596340.2831500.91455808:40
70.1510400.2574860.91602508:43
80.1248500.2249450.92115908:47
90.1051060.2681560.90979108:39
100.1058180.2031830.93106008:38
110.0907490.2162770.92922608:42
120.0807340.1836240.93546008:41
130.0759790.2129690.93032608:39
140.0717080.2376910.92299208:44
150.0622230.2320740.92812608:46
160.0522890.2274010.92922608:41
170.0530270.2225160.92959308:39
180.0540640.2349730.92849308:42
190.0585550.2331810.92922608:41

Here, with only 20 epochs, we can see reasonable results — both training and validation losses have gone down considerably, indicating that the model is learning to classify coastlines.

Visualize results in validation set

It is a good practice to see the results of the model viz-a-viz ground truth. The code below picks random samples and shows us ground truth and model predictions, side by side. This enables us to preview the results of the model within the notebook.

model.show_results(rows=4)
<Figure size 576x1152 with 8 Axes>

Accuracy assessment

arcgis.learn provides plot_confusion_matrix() that plots a confusion matrix of the model predictions to evaluate its accuracy.

model.plot_confusion_matrix()
<Figure size 414x414 with 1 Axes>

The confusion matrix validates that the trained model is learning to classify coastlines. The diagonal numbers show the number of chips correctly classified to the respective categories. The results are good for all but category 2. By looking at the row for category 2, we can see that there are very few chips in the validation set of our data (5 in total). As such, we can increase the number of chips either by increasing the value of the val_split_pct parameter in prepare_data() or by exporting more data for that particular category. We may need to re-train the model if we add more data to it.

Save the model

Now, we will save the model that we trained as a 'Deep Learning Package' ('.dlpk' format). A Deep Learning package is the standard format used to deploy deep learning models on the ArcGIS platform.

We will use the save() method to save the trained model. By default, it will be saved to the 'models' sub-folder within our training data folder.

model.save('model-20e_8classes')
Computing model metrics...
WindowsPath('D:/Coastline_category/models/model-20e_8classes')

Model inference

In order for us to perform inferencing in ArcGIS Pro, we need to create a feature class along the coastlines using the Generate Rectangles Along Lines tool, as shown in figure 2, for an area that is not already seen by the model.

Now, we will use the Classify Objects Using Deep Learning tool for inferencing the results. The parameters required to run the function are:

  • Input Raster: Sentinel2 imagery
  • Input Features: Output from the Generate Rectangles Along coastlines tool.
  • Output CLassified Objects Feature Class: Output feature class.
  • Model Definition: The model that we trained.
  • Class Label Field: Feild name that will contain the detected class number.
  • Environments: Set optimum Cell Size, Processing Extent and Processor Type.

Figure 6. Classify Objects Using Deep Learning tool

Results

We selected an unseen (by the model) sandy shoreline (category 3) and generated the required rectangles along it using the Generate Rectangles Along Lines tool. We then used our model for classification. Below are the results that we got.

You can observe in figure 9 that two rectangles got misclassified into category 7 (mangrove), and that the rest were classified correctly as belonging to category 3. Further training of the model could produce even more accurate results.

Conclusion

In this notebook, we demonstrated how to use the FeatureClassifier model from the ArcGIS API for Python to classify coastlines into multiple categories.

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