Coastline classification using Feature Classifier¶
- 🔬 Data Science
- 🥠 Deep Learning and Object classification
Table of Contents¶
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:
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.
import os import glob import zipfile from pathlib import Path from arcgis.gis import GIS from arcgis.learn import prepare_data, FeatureClassifier
# Connect to GIS gis = GIS("home")
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
Calculate options respectively, as shown in figure 4.
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
Environments: Set optimum
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
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), "*")
output_path = glob.glob(output_path)
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?".
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 )
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